GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes How to retrieve detached @ManyToOne entities with relationship    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to retrieve detached @ManyToOne entities with relationship    " Watch "How to retrieve detached @ManyToOne entities with relationship    " New topic
Author

How to retrieve detached @ManyToOne entities with relationship

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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:



( i ) As a result, please advice on how to utilise both fetchZipcodesWithRelationships() & fetchZipnamesWithRelationships() in ZipcodeApplicationClient() to retrieve collections of Zipcodes & Zipnames without encountering this issue?
(ii) I also like to utilise the zipnamebean.createZipnamez(zipname_1) to properly create this entity.

The local firewall has been de-activated. All components reside on the same host.

I have tried various approaches and googled without success.

This post has also been subscribed on http://forums.sun.com/thread.jspa?threadID=5330670 forum.

Your guidances would be very much appreciated.

Many thanks,

Jack
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

One thing that I notices is that your Query strings don't have a "SELECT" portion. In HQL that is fine, but when using JPA-QL instead, it requires that you have the Select portion of your query.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Mark,

I have updated createQuery statement as you have rightly pointed out to use "SELECT zc FROM Zipcode zc LEFT JOIN FETCH zc.zipnames" yet the outcomes is back to where we started with (ie. without the need to use createQuery with LEFT JOIN FETCH).

The remainder of ZipcodeApplicationClient() that retrieves all the records using fetch with relationship are as follows:



This is a step forward as it no longer throws an exception.

I would like to use the fetchZipnamesWithRelationships() within fetchZipcodesWithRelationships(), only if it is necessary despite having used the FETCH JOIN Query in fetchZipcodesWithRelationships() already.

Any more ideas?

Thanks,

Jack
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I am sorry but I am a little confused. What is the actual problem. Is it that you do not get the correct data? If so, how is the data incorrect?

Mark
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Mark,

This record was added with the following entries in the ZipcodeApplicationClient:



Thanks,

Jack
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

When trying to add multiple Zipnames with the following additional statements at the beginning of ZipcodeApplicationClient:


Any ideas on why this occur?

Thanks,

Jack
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

OK, I am still confused, as I am not seeing the problem here.

But this is what I am taking from it

You have a client, who is connecting to a server in a different JVM. On the client you create these POJO objects, then you have Remote EJBs being called. On the Remote EJB, you have coupled your business logic and your Data Access object with EJB3 and your Stateless Session Bean. My first recommendation would be to decouple that code. Create a DAO class that uses the EntityManager to do the querying and CRUD.

Anyway, besides that point.

So the client creates this object, outside of an EntityManager, you send it to the server, and expect the server side EntityManager to persist it. Well that object is not being managed by the EntityManager at that point, it is considered the detached state. You will need to merge() the detached object into the EntityManager to allow it to manage it and then therefore after, persist it.

Mark
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Mark,

I have by-passed the use of these ZipcodeBean and ZipnameBean with the following simplified ZipcodeApplicationClient. This will eliminate the possibility of referencing detached Zipcode or Zipname objects:


However, another type of deployment error message is produced:

Initial deploying GeographicalBean to C:\Documents and Settings\Jack\GeographicalBean\dist\gfdeploy
Completed initial distribution of GeographicalBean
Start registering the project's server resources
Finished registering server resources
moduleID=GeographicalBean
deployment started : 0%
deployment finished : 100%
Deploying application in domain completed successfully
Trying to create reference for application in target server completed successfully
Trying to start application in target server completed successfully
WARNING:
JDO76614: Deployment encountered SQL Exceptions:
JDO76609: Got SQLException executing statement "CREATE TABLE ZIPCODE (ID INTEGER NOT NULL, DIALING_CODE VARCHAR(255), TIME_ZONE VARCHAR(255), CODE INTEGER, NEAREST_AIRPORT VARCHAR(255), LONGITUDE VARCHAR(255), NEAREST_TRAIN_STATION VARCHAR(255), NEAREST_URBAN_CENTRE VARCHAR(255), HOTELS VARCHAR(255), LATITUDE VARCHAR(255), NEARBY_FEATURES VARCHAR(255), COUNCIL VARCHAR(255), LOCALITY VARCHAR(255), PRIMARY KEY (ID))": com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'zipcode' already exists
JDO76609: Got SQLException executing statement "CREATE TABLE ZIPNAME (ID INTEGER NOT NULL, NAME VARCHAR(255), ZIPCODE_ID INTEGER, PRIMARY KEY (ID))": com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'zipname' already exists
JDO76609: Got SQLException executing statement "ALTER TABLE ZIPNAME ADD CONSTRAINT FK_ZIPNAME_ZIPCODE_ID FOREIGN KEY (ZIPCODE_ID) REFERENCES ZIPCODE (ID)": com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Key column 'ZIPCODE_ID' doesn't exist in table
Enable of GeographicalBean in target server completed successfully
Enable of application in all targets completed successfully
All operations completed successfully
post-run-deploy:
run-deploy:
run-display-browser:
run-ac:
Copying 1 file to C:\Documents and Settings\Jack\GeographicalBean\dist
Caught an unexpected exception!
java.lang.NullPointerException
at client.ZipcodeApplicationClient.main(ZipcodeApplicationClient.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:449)
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:259)
at com.sun.enterprise.appclient.Main.main(Main.java:200)
run-GeographicalBean-app-client:
run:

Thanks,

Jack
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve detached @ManyToOne entities with relationship