NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

How to retrieve detached @ManyToOne entities with Fetch Join relationship

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
Jack Bush
Posted via mailing list.





PostPosted: Fri Sep 12, 2008 12:58 pm    Post subject: How to retrieve detached @ManyToOne entities with Fetch Join relationship Reply with quote

Hi All,

I am having difficulty retrieving all the Zipcode, Zipname records which have successfully been deployed on Glassfish v2r2, JDK1.6.0_06, MySQL 5.0, Netbeans 6.1 on Windows XP platform. Below are the relevant EJBs snippets:

package domain;
@Entity
@Table(name="ZIPCODE")
public class Zipcode implements Serializable {

@Id
@Column(name="ID")
private int id;
public int getId() {
return id;
}
.....
@OneToMany(cascade={CascadeType.ALL}, mappedBy="zipcode", fetch=FetchType.EAGER, targetEntity=Zipname.class)
private Collection<Zipname> zipnames = new ArrayList<Zipname>();
public Collection<Zipname> getNames() {
return zipnames;
}
.....

package domain;
@Entity
@Table(name="ZIPNAME")
public class Zipname implements Serializable {
@Id
@Column(name="ID")
private int id;
public void setId(int id) {
this.id = id;
}
.....

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="ZIPCODE_ID")
private Zipcode zipcode;
public Zipcode getZipcode() {
return zipcode;
}
......

package ejb;
@Stateless
public class ZipcodeBean implements ZipcodeRemote {

@PersistenceContext(unitName="GeographicalDB") private EntityManager manager;

public void createZipcode(Zipcode zipcode) {
manager.persist(zipcode);
}

public Zipcode findZipcode(int pKey) {
Zipcode zipcode = manager.find(Zipcode.class, pKey);
zipcode.getNames().size();
return zipcode;
}


public List fetchZipcodesWithRelationships() {
List list = manager.createQuery("SELECT zc FROM Zipcode zc LEFT JOIN FETCH zc.zipnames").getResultList();
for (Object obj : list) {
Zipcode zipcode = (Zipcode)obj;
zipcode.getNames().size();
}
return list;
}

@Stateless
public class ZipnameBean implements ZipnameRemote {

@PersistenceContext(unitName="GeographicalDB") private EntityManager manager;

public void createZipname(Zipname zipname) {
manager.persist(zipname);
}

public Zipname findZipname(int pKey) {
Zipname zipname = manager.find(Zipname.class, pKey);
zipname.getZipcode().getNames().size();
return zipname;
}


public List fetchZipnamesWithRelationships() {
List list = manager.createQuery("FROM Zipname zipname").getResultList();
for (Object obj : list) {
Zipname zipname = (Zipname)obj;
zipname.getZipcode().getNames().size();
}
return list;
}

public class ZipcodeApplicationClient {
@EJB
private static ZipcodeRemote zipcodebean;
private static ZipnameRemote zipnamebean;


public static void main(String[] args) {
try {
Zipcode zipcode_1 = new Zipcode();
zipcode_1.setcode(9001);
Zipname zipname_1 = new Zipname();
zipname_1.setName("Harbour Cove");
zipcode_1.getNames().add(zipname_1);
.......
zipname_1.setZipcode(zipcode_1);
// zipnamebean.createZipname(zipname_1);
zipcodebean.createZipcode(zipcode_1);
-----------------------------------------------------------------
//fetch detached entity without relationship
Zipcode zipcode_2 = zipcodebean.findZipcode(0);
System.out.println("zipcode_2.getId(): " + zipcode_2.getId());
System.out.println("zipcode_2.getCode(): " + zipcode_2.getCode());
Iterator iterator = zipcode_2.getNames().iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next().toString());
}
-----------------------------------------------------------------
// output of fetching detached entity without relationship
zipcode_2.getId(): 0
zipcode_2.getCode(): 2010
domain.ZipName@107e9a8 ([email]domain.ZipName@107e9a8[/email])
-----------------------------------------------------------------
//fetch detached entity with relationship
List list = zipcodebean.fetchZipcodesWithRelationships();
-----------------------------------------------------------------
// output of fetching detached entity with relationship
-----------------------------------------------------------------------------------------------------------------------------------
//fetch detached entity with relationship
List list = zipcodebean.fetchZipcodesWithRelationships();//line 76
for
(Object obj : list) {
Zipcode zipcode_2 = (Zipcode)obj;
System.out.println("zipcode_2.getId(): " + zipcode_2.getId());
System.out.println("zipcode_2..getCode(): " + zipcode_2.getCode());
System.out.println("\tNumber of names: " + zipcode_2.getNames().size());
Iterator iterator = zipcode_2.getNames().iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
------------------------------------------------------------------
// output of fetching detached entity with relationship
------------------------------------------------------------------
zipcode_2.getId(): 0
zipcode_2.getCode(): 9001
Number of names: 1
domain.Zipname@8bf3ec ([email]domain.Zipname@8bf3ec[/email])
-------------------------------------------------------

( i ) As a result, please advice on how to utilise both fetchZipcodesWithRelationships() & fetchZipnamesWithRelationships() in ZipcodeApplicationClient() to retrieve collections of Zipcodes & Zipnames correctly?
(ii) I like to utilise the zipnamebean.createZipnamez(zipname_1) to properly create this entity.
(iii) I don't understand why the FETCH JOIN Query is necessary when both entity were setup with FetchType.EAGER?

I have tried various approaches and googled without success.

This question has been posted on http://forums.sun.com/thread..jspa?threadID=5330670&tstart=0 and http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=004489&p=1 earlier in the hope of a quicker response.
Your guidances would be very much appreciated.
Many thanks,
Jack



Make the switch to the world's best email. [url=http://au.rd.yahoo.com/mail/taglines/au/mail/default/*http://au.yahoo.com/y7mail]Get Yahoo!7 Mail[/url].
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo