JPA RollbackException但不在单元测试中 [英] JPA RollbackException but not in unit test
问题描述
我有一个java项目,其中包含一组单元测试,可以执行简单的更新,使用JPA2进行删除。单元测试运行没有问题,我可以验证数据库中的更改 - 一切都很好。我尝试在处理程序(Smartfox扩展)中复制/粘贴此相同的函数 - 我收到回滚异常。
I have a java project with a collection of unit tests that perform simple updates, deletes using JPA2. The unit tests run without a problem, and I can verify the changes in the database - all good. I attempt to copy/paste this same function in a handler (Smartfox Extension) - I recieve a rollback exception.
列'levelid'不能为空。
Column 'levelid' cannot be null.
寻找有关原因的建议。我可以从此扩展(GetModelHandler)中执行数据读取,但尝试设置数据不起作用。这完全令人费解。
Looking for suggestions as to why this might be. I can perform data reads from within this extension ( GetModelHandler ) but trying to set data does not work. It's completely baffling.
所以总结 -
这有效...
@Test
public void Save()
{
LevelDAO dao = new LevelDAO();
List levels = dao.findAll();
int i = levels.size();
Level l = new Level();
l.setName("test");
Layer y = new Layer();
y.setLayername("layer2");
EntityManagerHelper.beginTransaction();
dao.save(l);
EntityManagerHelper.commit();
}
此失败并带有回滚异常
public class SetModelHandler extends BaseClientRequestHandler
{
@Override
public void handleClientRequest(User sender, ISFSObject params)
{
LevelDAO dao = new LevelDAO();
List levels = dao.findAll();
int i = levels.size();
Level l = new Level();
l.setName("test");
Layer y = new Layer();
y.setLayername("layer2");
EntityManagerHelper.beginTransaction();
dao.save(l);
EntityManagerHelper.commit();
}
}
Level和Layer类有一个分别为OneToMany和ManyToOne属性。
The Level and Layer class have a OneToMany and ManyToOne attribute respectively.
任何想法。
更新
这是架构
Level
--------
levelid (int) PK
name (varchar)
Layer
--------
layerid (int) 11 PK
layername (varchar) 100
levelid (int)
Foreign Key Name:Level.levelid ,
On Delete: no action,
On Update: no action
当我改变时
EntityManagerHelper.beginTransaction();
dao.update(l);
EntityManagerHelper.commit();
到
EntityManagerFactory factory = Persistence.createEntityManagerFactory("bwmodel");
EntityManager entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
dao.update(l);
entityManager.persist(l);
entityManager.getTransaction().commit();
这是执行保存但不是更新?我在这里遗漏了一些明显的东西。
This performs a save but not an update ? I'm missing something obvious here.
推荐答案
我能看到的最可能的问题是不同的数据库定义。测试EJB通常使用即时生成的内存数据库。而在实际生产中,您使用的是可能强制执行约束的真实数据库。
The most likely problem I can see would be different database definitions. Testing EJBs often use an in-memory database that is generated on the fly. Whereas in actual production you are using a real database which is probably enforcing constraints.
尝试为levelid值指定值或更改数据库架构。
Try assigning the levelid value a value or changing the database schema.
这篇关于JPA RollbackException但不在单元测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!