Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - Problems with @onetomany

 
everson santos
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
everson santos wrote:
User.java:


Role.java:



Why is the "roles" in User and "user" in Role marked as transient?
 
everson santos
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thanks for the update.

-Cameron McKenzie
 
Jowel Poquiz
Greenhorn
Posts: 5
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic