I'm trying build a table with Hibernate + Spring and testing it with JUnit.
Here is my class model:
@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;
When I'm trying to testing it using JUnit:
@Test
public void testGetAll() {
List<Score> scores = service.getScores();
assertEquals(1, scores.size());
}
It shows this error (actually I tried with all the testing like Save, Update, Delete, Get A Record and same error occur):
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)
I think the problem with the private Date dateExam;
the thing that I confuse is the column dateExam
I set the default to NULL
but why it said SQLErrorException
. Before this, I already build 4 more tables but without Date
as the column type and without error. I try to changed it to private java.sql.Timestamp dateExam
but error still occur.
Anyone can give me solution for this? I'm very appreciated with your help. Thanks...
Update
I try to remove the @Id
and set the nullable = true
but still the same...
Update 30/04
I got the problem, when JUnit testing I try to load the first data from xml file:
@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);
}
And the score.xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<user
user_name="crazenezz"
first_name="craze"
/>
<score
user_name="crazenezz"
date_exam="2012-04-30"
percent="100"
/>
</dataset>
The question now, what is the true format for the date date_exam
here using java.util.Date
if we initialize the value inside the xml file? Because I tried before to set the default value in the model private Date dateExam = new Date()
the error still occur?