我正在尝试使用 Hibernate + Spring 构建一个表并使用 JUnit 对其进行测试。
这是我的班级模型:
@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;
当我尝试使用 JUnit 对其进行测试时:
@Test
public void testGetAll() {
List<Score> scores = service.getScores();
assertEquals(1, scores.size());
}
它显示了这个错误(实际上我尝试了所有测试,如保存、更新、删除、获取记录并发生相同的错误):
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)
我认为private Date dateExam;
我混淆的问题是dateExam
我设置默认值的列,NULL
但它为什么这么说SQLErrorException
。在此之前,我已经构建了另外 4 个表,但没有Date
作为列类型并且没有错误。我尝试将其更改为,private java.sql.Timestamp dateExam
但仍然出现错误。
任何人都可以给我解决这个问题?我非常感谢您的帮助。谢谢...
更新
我尝试删除@Id
并设置nullable = true
但仍然相同......
更新 30/04
我遇到了问题,当 JUnit 测试时,我尝试从 xml 文件加载第一个数据:
@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);
}
还有 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>
现在的问题是,如果我们在 xml 文件中初始化值,这里使用的日期的真正格式是什么date_exam
java.util.Date
?因为我之前尝试在模型中设置默认值private Date dateExam = new Date()
仍然出现错误?