开发者

Hibernate, EHCache, Read-Write cache, adding item to a list

开发者 https://www.devze.com 2022-12-23 00:28 出处:网络
I have an entity that has a collection in it.The collection is a OneToMany unidirectional relationship storing who viewed a particular file.The problem I am having is that after I load the entity and

I have an entity that has a collection in it. The collection is a OneToMany unidirectional relationship storing who viewed a particular file. The problem I am having is that after I load the entity and try to update the collection, I don't get any errors, but the collection is never updated:

Entity:

@OneToMany(cascade = CascadeType.PERSIST)
@JoinTable
开发者_高级运维protected List<User> users;

File Servlet

@In
private EntityQuery<File> File_findById;

...

File file = File_findById(fileId);
file.getUsers().add(user);
session.update(file);

Even though I call session.update(file) and I see stuff in hibernate logs, I don't see anything in the database indicating that it was saved.

Walter


Use property access instead of field access because of it enables Automatic dirty checking by Hibernate and use @Cascade(CascadeType.SAVE_UPDATE) instead

@OneToMany
@JoinTable
@Cascade(CascadeType.SAVE_UPDATE)
public List<User> getUsers() {
    return this.users;
}

regards,


Why do you cascade the persist (create) operation only if you also want to cascade merge / update?

With EJB3 / JPA annotations, you may want to add another cascading type:

  • CascadeType.MERGE

Or maybe even:

  • CascadeType.ALL (which also covers Hibernate specific operations like save-update, lock)

If you want to use Hibernate extensions, you may want to use the @Cascade annotation to cascade:

  • SAVE_UPDATE
0

精彩评论

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

关注公众号