简体   繁体   English

双向多对多JPA

[英]Bi-directional Many to Many JPA

I having a hard time with JPA hopefully someone can help me. 我在JPA遇到困难,希望有人可以帮助我。

I have 3 tables: 我有3张桌子:

  • Rol (CPE_ROL) 角色(CPE_ROL)
  • TipoUsuario (GTV_TIPOUSU) TipoUsuario(GTV_TIPOUSU)
  • RolTipoUsuario (CPE_ROLTUS - Join Table) RolTipoUsuario(CPE_ROLTUS-联接表)

Rol.java Rol.java

@JoinTable(name = "CPE_ROLTUS", joinColumns = {
    @JoinColumn(name = "CPE_ROLTUS_TIPOUSU_ID", referencedColumnName = "GTV_TIPOUSU_ID")}, inverseJoinColumns = {
    @JoinColumn(name = "CPE_ROLTUS_ROL_ID", referencedColumnName = "CPE_ROL_ID")})
@ManyToMany(fetch = FetchType.LAZY, cascade={CascadeType.REFRESH})
private List<TipoUsuario> tipoUsuarioList;

TipoUsuario.java TipoUsuario.java

@ManyToMany(mappedBy = "tipoUsuarioList", fetch = FetchType.LAZY, cascade={CascadeType.REFRESH})


private List<Rol> rolesDefault;

For some reason rolesDefault is never filled up, I wondering if I'm missing something. 由于某些原因,RoleDefault从未被填充,我想知道是否丢失了某些东西。

Thanks in advance. 提前致谢。

Daniel 丹尼尔

My guess is when you create the objects you are not setting both sides of the relationship. 我的猜测是,当您创建对象时,您并没有设置关系的双方。 You must maintain bi-directional relationships in JPA. 您必须在JPA中维护双向关系。 When you add to one side, also add to the other. 当添加到一侧时,也要添加到另一侧。

See, http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side 参见http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_update__after_updating_the_other_side

You most likely have caching enabled, or are using the same EntityManager, so when reading you get objects from the cache. 您很可能已启用缓存,或者正在使用相同的EntityManager,因此在读取时会从缓存中获取对象。 You could also disable the shared cache, or refresh the object, but fixing your persist code is best. 您也可以禁用共享缓存,或刷新对象,但是最好修复持久性代码。

Otherwise, enable logging on finest and see what SQL is executed. 否则,启用最好的登录并查看执行了什么SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM