| Author |
Hibernate - Problems with @onetomany
|
everson santos
Ranch Hand
Joined: Jul 11, 2009
Posts: 50
|
|
Hi! I think my mapping doesn't work, because when I did the query the fields from child table didn't load, my list getRoles came empty.
user table:
id login password activated
1 xx xx 1
role table:
id nome id_user
1 ROLE_ADMIN 1
2 ROLE_USER 1
User.java:
Role.java:
My select from hibernate:
My find method:
I got this approach from:
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association
2.2.5.3.2.1. Bidirectional
Since many to one are (almost) always the owner side of a bidirectional relationship in the EJB3 spec, the one to many association is annotated by @OneToMany( mappedBy=... )
@Entity
public class Troop {
@OneToMany(mappedBy="troop")
public Set<Soldier> getSoldiers() {
...
}
@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk")
public Troop getTroop() {
...
}
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
The get(0) confuses me a bit?
Are you getting the User to return, without the associated objects? I'm wondering what SQL gets generated in the logs.
If all else fails, just hardcode a value for the loginName and make sure that's not the weak link. Here's an example from my website of a little HQL that hardcodes a value but does return a result:
It's a very simple approach that uses Annotations and a simple session.createQuery(hql) approach.
Tutorial on HQL and Hibernate with JPA Annotations
Any exception being thrown?
-Cameron McKenzie
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
Rahul Babbar
Ranch Hand
Joined: Jun 28, 2008
Posts: 210
|
|
everson santos wrote:
User.java:
Role.java:
Why is the "roles" in User and "user" in Role marked as transient?
|
 |
everson santos
Ranch Hand
Joined: Jul 11, 2009
Posts: 50
|
|
Rahul Babbar wrote:
everson santos wrote:
User.java:
Role.java:
Why is the "roles" in User and "user" in Role marked as transient?
I did @transient because I don't have theses atributes in my tables(user and role). I think is correct isn't ?
|
 |
Rahul Babbar
Ranch Hand
Joined: Jun 28, 2008
Posts: 210
|
|
everson santos wrote:
I did @transient because I don't have theses atributes in my tables(user and role). I think is correct isn't ?
I admit, i am not very sure...
Your "Roles" did not load because you have not set the "lazy" attribute to false....The default for One to many (from User to roles) is true.
As a result, when you load the User, your roles are not loaded.
You will need to set the "lazy" attribute to "false".
|
 |
everson santos
Ranch Hand
Joined: Jul 11, 2009
Posts: 50
|
|
Cameron Wallace McKenzie wrote:The get(0) confuses me a bit?
Are you getting the User to return, without the associated objects? I'm wondering what SQL gets generated in the logs.
If all else fails, just hardcode a value for the loginName and make sure that's not the weak link. Here's an example from my website of a little HQL that hardcodes a value but does return a result:
It's a very simple approach that uses Annotations and a simple session.createQuery(hql) approach.
Tutorial on HQL and Hibernate with JPA Annotations
Any exception being thrown?
-Cameron McKenzie
I got the return here:
SecureDao.java
Any exception being thrown?
nothing
I'll go check your suggest...
thanks
|
 |
everson santos
Ranch Hand
Joined: Jul 11, 2009
Posts: 50
|
|
Rahul Babbar wrote:
everson santos wrote:
I did @transient because I don't have theses atributes in my tables(user and role). I think is correct isn't ?
I admit, i am not very sure...
Your "Roles" did not load because you have not set the "lazy" attribute to false....The default for One to many (from User to roles) is true.
As a result, when you load the User, your roles are not loaded.
You will need to set the "lazy" attribute to "false".
You were sure.
I erased @transient and marked fetch=FetchType.EAGER.
Working fine now.
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Awesome! Thanks for the update.
-Cameron McKenzie
|
 |
Jowel Poquiz
Greenhorn
Joined: Apr 25, 2011
Posts: 4
|
|
I have also stumbled on this problem.In addition, here is the explanation on why removing the @Transient is necessary. Inverse Fields vs Persistent Fields here
|
OCPJP6, OCE WCD
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Hibernate - Problems with @onetomany
|
|
|