-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: find(Class,id) and JPQL get different object (JPA)
PostPosted: Tue Feb 26, 2013 12:20 pm 
Newbie

Joined: Tue Feb 26, 2013 11:50 am
Posts: 2
I used Hibernate 4.1.10.Final as the JPA provider, Spring and Spring MVC.

There are two entities.
Code:
@Entity
@Table(name = "a")
public class A{
@Id
private String id;

[color=#808040]
@OneToMany(mappedBy = "a", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<B> bs;
[/color]
....
}

@Entity
@Table(name = "b")
public class B{
@Id
private String id;

[color=#808040]
@ManyToOne
@JoinColumn(name = "fk_a_id")
private A a;
[/color]
....
}


I need to get an A and it's bs, so i using find(A.class,id) of EntityManager.
Code:
A a1 =em.find(A.class, id);
a1.getBs().size();

Result is: the size of bs is zero. It means that there is no associated B.
But i'm sure that there are many associated Bs in the database and the data have been load from database while checking console.

While i used Query to get that :
Code:
Query query = em.createQuery("SELECT a FROM A AS a WHERE a.id = ?1",A.class);
query.setParameter(1, id);
A a= (A) query.getSingleResult();
a.getBs().size(); // = 22

This time the size is 22.

What's wrong?


Top
 Profile  
 
 Post subject: Re: find(Class,id) and JPQL get different object (JPA)
PostPosted: Wed Feb 27, 2013 2:10 am 
Beginner
Beginner

Joined: Wed Feb 06, 2013 2:43 am
Posts: 46
Can you please try this in your OneToMany mapping :

private Set<B> b = new HashSet<B>(0); // Declaration

//Getter and setter
@OneToMany
@JoinColumn(name = "b")
public Set<B> getB() {
return a;
}
public void setB(final Set<B> b) {
this.b = b;
}

_________________
Thanks,
Ajit Singh
ajits@mindfiresolutions.com
www.mindfiresolutions.com


Top
 Profile  
 
 Post subject: Re: find(Class,id) and JPQL get different object (JPA)
PostPosted: Wed Feb 27, 2013 9:57 pm 
Newbie

Joined: Tue Feb 26, 2013 11:50 am
Posts: 2
Ajit Singh wrote:
Can you please try this in your OneToMany mapping :

private Set<B> b = new HashSet<B>(0); // Declaration

//Getter and setter
@OneToMany
@JoinColumn(name = "b")
public Set<B> getB() {
return a;
}
public void setB(final Set<B> b) {
this.b = b;
}


Thanks for your answer.
With this way, it also can not get bs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.