开发者

JPA RollbackException but not in unit test

开发者 https://www.devze.com 2023-04-11 08:10 出处:网络
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

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.

Column 'levelid' cannot be null.

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.

So in summary -

This works...

@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();
}

This fails with rollback exception

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();
}

}

The Level and Layer class have a OneToMany and ManyToOne attribute respectively.

Any ideas appeciated.

Update

Here's the schema

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

When I changed

EntityManagerHelper.beginTransaction();
dao.update(l);
EntityManagerHelper.commit();

to

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.


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.

Try assigning the levelid value a value or changing the database schema.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号