@在HSQL数据库中生成
假设在数据库表中有一个名为 RECORD_ID 的非键列,它是在数据库中自动生成/排序的。
现在,在上表的实体中,就在 RECORD_ID 字段上方,我使用 hibernate 注释 @Generate 来指示 hibernate DB 将负责将值插入到该特定字段中。它看起来如下所示:
@Column(name = "RECORD_ID", unique = true, nullable = true, insertable = false, updatable = false)
@Generated(GenerationTime.INSERT)
private Long recordId;
我运行我的代码并且工作正常。数据库正在按预期插入值。
现在,我使用 HSQL 编写一个单元测试用例,以确保我的逻辑完好无损。 该逻辑有一个根据 RECORD_ID 获取记录的查询。
现在,当我创建固定装置并调用测试时,当我保存固定装置时,HSQL 将不会为记录的 RECORD_ID 生成值。它将其保留为空。但我打算测试的逻辑是基于通过 RECORD_ID 获取,这在这里是不可能的。
它在 HSQL DB 中保持为空。因此我无法完成单元测试!
请提供一些解决此问题或任何解决方法的线索。
请找到下面的单元测试代码
@Test
public void testProductCancelDAL() {
savePrerequisites();
Logic myLogic = new Logic();
/* Logic fetches the reservation record based on RECORD_ID */
assertNotNull(myLogic.invoke());
}
public void savePrerequisites() {
//Stroing product type
Product product = ProductFixture.createProduct();
sessionFactory.getCurrentSession().save(product);
//Storing Itinerary
Itinerary itn = ItnFixture.create();
sessionFactory.getCurrentSession().save(itn);
Reservation res = ReservationFixture.create();
sessionFactory.getCurrentSession().save(res);
sessionFactory.getCurrentSession().flush();
}
Assume that in a database table there is a non-key column called RECORD_ID which is auto-generated/sequenced in database.
Now in my entity for the table above, just above the field for RECORD_ID, I use the hibernate annotation @Generated to indicate to hibernate that DB will take responsibility for inserting value into that particular field. It looks like the following:
@Column(name = "RECORD_ID", unique = true, nullable = true, insertable = false, updatable = false)
@Generated(GenerationTime.INSERT)
private Long recordId;
I run my code and it works fine. DB is inserting value as expected.
Now, I write a unit-test case with HSQL to ensure my logic is intact.
The logic has a query which fetches the record based on RECORD_ID.
Now when i create fixture and invoke my test, HSQL will not generate value for the record's RECORD_ID when I save my fixture. It leaves it as null. But the logic that I intend to test is based on fetching by RECORD_ID which isn't possible here.
It just remains null in HSQL DB. And hence I am not able to complete unit testing!
Kindly throw some light on solving this issue or any workaround.
Please find the unit-test code below
@Test
public void testProductCancelDAL() {
savePrerequisites();
Logic myLogic = new Logic();
/* Logic fetches the reservation record based on RECORD_ID */
assertNotNull(myLogic.invoke());
}
public void savePrerequisites() {
//Stroing product type
Product product = ProductFixture.createProduct();
sessionFactory.getCurrentSession().save(product);
//Storing Itinerary
Itinerary itn = ItnFixture.create();
sessionFactory.getCurrentSession().save(itn);
Reservation res = ReservationFixture.create();
sessionFactory.getCurrentSession().save(res);
sessionFactory.getCurrentSession().flush();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在评论中所说的内容,您似乎正在测试一些代码,这些代码依赖于生成 RECORD_ID 的数据库中的触发器,但是您在不存在该触发器的内存数据库上测试它。显然,这行不通。您需要创建与生产数据库中相同的触发器。请参阅http://hsqldb.org/doc/2.0/guide/triggers-chapt。 html 在 HSQLDB 数据库中创建触发器。
Based on what you said in the comments, it appears that you're testing some code which depends on a trigger in the database generating the RECORD_ID, but that you test this on an in-memory database where this trigger doesn't exist. This, obviously, won't work. You need to create the same trigger as in your production database. See http://hsqldb.org/doc/2.0/guide/triggers-chapt.html to create triggers in a HSQLDB database.
在您的情况下,RECORD_ID 不是主键吗?
在这种情况下,您应该将 @GenerateValue 与 @Id 注释一起使用。
数据库生成值的问题是插入后必须重新读取记录(它不适用于 AUTOINCRMENT 列,其中最后生成的 id 可以通过专用指令读取)。
In your case, isn't RECORD_ID the primary key?
In such case, you should use @GeneratedValue with @Id annotation.
The problem with database-generated values is that record must be re-read after insert (it does not apply to AUTOINCREMENT columns, where last generated id can be read by dedicated instruction).