Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

@ManyToMany relation table empty (newbie)

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate 3

I have two entities User and Role which holds a bidirectional relationship.
Basically I want to create as many roles as I want, without the need to add any user to it, but every time a new user entity has to become persistent it must belong at least to a role.

User.class

...
// many-to-many bidirectional
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "users")
@Column(nullable = false)
private List<Role> roles = new ArrayList<Role>();
....


Role.class

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="USR_ROLE_FK")
private List<User> users = new ArrayList<User>();


I can create new role and users, but the problem is on the USR_ROLE_FK table which is empty, so not consistent relationship between entities.
On the client side, before to pass the new user entity to persist, I first retrieve the detached role entity, then add it to the new user and finally passed to the ejb.
Personally I think I am missing something about the cascading property, but after several attempts and fails I am stuck.

I need your help, at least some resources with practical examples.
Thanks in advance
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically I want to create as many roles as I want, without the need to add any user to it, but every time a new user entity has to become persistent it must belong at least to a role.



Hmmm.....I'm not sure how you'll enforce this at the JPA level. I'm not saying you can't, I'm just saying that in my limited knowledge, I don't know how I'd do it.

What about doing it at the Java level?

So, use has a constructor that takes a role. It has only one constructor, so it can't be created without a role:



Now, if the role is null, you throw an exception:



Is it feasible?

Not sure if this fits your need. Perhaps it's at least a talking point.

-Cameron McKenzie
 
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An observation:

- You have put mappedBy on User side, that means the role side is the owning side. I think it's better to make the User side as the owning side. Because the roles are attached or detached to users and not the other way round.

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Move the owner doesn't make any difference, I think that in a biderectional relationship I have to manage the relation by myself.
Something like this, before to persist new entities.

Role existRole = (Role)em.find(Role.class, id);
em.persist(newUsr);
newUsr.getRoles().add(existRole);
existRole.getUsers().add(newUsr);

//it shouldn't be necessary, but...
em.merge(existRole);
em.merge(newUser);

And at this point I'd add the Transaction required annotation as well

Not sure it works, but apparently let Hibernate to manager bidirectional relationship doesn't work as expected.
Thanks in advance
 
The City calls upon her steadfast protectors. Now for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic