2

I having a hard time with JPA hopefully someone can help me.

I have 3 tables:

  • Rol (CPE_ROL)
  • TipoUsuario (GTV_TIPOUSU)
  • RolTipoUsuario (CPE_ROLTUS - Join Table)

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

@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.

Thanks in advance.

Daniel

zkropotkine
  • 337
  • 2
  • 5
  • 12

1 Answers1

5

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. 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

You most likely have caching enabled, or are using the same EntityManager, so when reading you get objects from the cache. 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.

James
  • 17,965
  • 11
  • 91
  • 146
  • Yeah, I'm using the same EntityManager... I finished doing this with a nativeQuery. I know is not the best solution, but it was the easiest and faster that I found. – zkropotkine Mar 18 '11 at 17:54