开发者

jpa entitymanager contains always returns false

I have a simple test case where I save an object and then test if the object is present in db. but the contains method always returns false. The object is saved in the database

 User entity = new User();
    entity.setName("test");
    User result = ObjectManager.save(entity);

    boolean exists = ObjectManager.contains(result);

    assertTrue(exists);

@Override
public bool开发者_如何学编程ean contains(T obj) throws DataStoreException {
    try {
        return entityManager.contains(obj);
    } catch (Exception e) {
        throw new DataStoreException(e);
    }
}


According to the EntityManager documentation:

Check if the instance is a managed entity instance belonging to the current persistence context.

So it does not check if its in database. May be you have detached the entity in the save method or somehow its not in the current persistence context.

Use find or get to verify existence in db.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜