title: Set default Date in Hibernate and MySQL using @Before and xml file
body: <p>I'm trying build a table with Hibernate + Spring and testing it with JUnit.</p>
  
  <p>Here is my class model:</p>
  
  <pre><code>@Id
  @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinColumn(name = "user_name")
  private User user;
  
  @Id
  @Column(name = "date_exam")
  private Date dateExam;
  
  @Column
  private Float percent;
  </code></pre>
  
  <p>When I'm trying to testing it using JUnit:</p>
  
  <pre><code>@Test
  public void testGetAll() {
      List&lt;Score&gt; scores = service.getScores();
      assertEquals(1, scores.size());
  }
  </code></pre>
  
  <p>It shows this error (actually I tried with all the testing like Save, Update, Delete, Get A Record and same error occur):</p>
  
  <pre><code>java.sql.SQLException: Field 'dateExam' doesn't have a default value
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
  at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
  at org.dbunit.database.statement.SimplePreparedStatement.addBatch(SimplePreparedStatement.java:80)
  at org.dbunit.database.statement.AutomaticPreparedBatchStatement.addBatch(AutomaticPreparedBatchStatement.java:70)
  at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:195)
  at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
  at test.backend.config.dao.ScoreDaoTest.resetDatabase(ScoreDaoTest.java:45)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
  at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
  at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
  at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>I think the problem with the <code>private Date dateExam;</code> the thing that I confuse is the column <code>dateExam</code> I set the default to <code>NULL</code> but why it said <code>SQLErrorException</code>. Before this, I already build 4 more tables but without <code>Date</code> as the column type and without error. I try to changed it to <code>private java.sql.Timestamp dateExam</code> but error still occur.</p>
  
  <p>Anyone can give me solution for this?
  I'm very appreciated with your help. Thanks...</p>
  
  <p><strong>Update</strong></p>
  
  <p>I try to remove the <code>@Id</code> and set the <code>nullable = true</code> but still the same...</p>
  
  <p><strong>Update 30/04</strong></p>
  
  <p>I got the problem, when JUnit testing I try to load the first data from xml file:</p>
  
  <pre><code>@Before
  public void resetDatabase() throws Exception {
      IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File(
              "fixtures/score.xml"));
      final Connection conn = dtSource.getConnection();
      DatabaseOperation.CLEAN_INSERT.execute(new DatabaseConnection(conn),
              dataSet);
  }
  </code></pre>
  
  <p>And the score.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;dataset&gt;
  &lt;user 
      user_name="crazenezz" 
      first_name="craze" 
  /&gt;
  &lt;score 
      user_name="crazenezz" 
      date_exam="2012-04-30" 
      percent="100" 
  /&gt;
  &lt;/dataset&gt;
  </code></pre>
  
  <p>The question now, <strong>what is the true format for the date <code>date_exam</code> here using <code>java.util.Date</code> if we initialize the value inside the xml file</strong>? Because I tried before to set the default value in the model <code>private Date dateExam = new Date()</code> the error still occur?</p>