persistence@glassfish.java.net

Re: question on entity relationships

From: Craig L Russell <Craig.Russell_at_Sun.COM>
Date: Wed, 05 Dec 2007 12:23:15 -0800

Hi Eve,

First, the StackOverflow is a bug that should never occur based on
performing a query. A reproducible test case would help solve this
issue.

But, let's talk about what you're trying to accomplish. If the caller
of this method expects a List<Lampent> it can iterate the List and
for each Lampent, get the fields it needs:
SELECT l FROM Lampent l
List<Lampent> lampents = allLightingpro();
for (Lampent lampent : lampents) {
   System.out.println(
   "Lampent pnumb: " + lampent.getPnumb() +
   ", woltage: " + lampent.getWoltage() +
   ", manuname: " + lampent.getManufacturerent().getName() );
}

This will query the Lampents and then retrieve the Manufacturerent
for each Lampent.

But you might only want the fields to be returned, not the Lampent
instances. In this case, projecting the fields from Lampent and
Manufacturent would be ok, but the return from the query should be
List<Object[]> and then you iterate the result:
  SELECT l.pnumb, l.catergory, l.lwoltage, lm.manuname FROM Lampent l
LEFT OUTER JOIN l.lamManufacturerent lm
List<Object[ ]> results = allLightingpro();
for (Object[ ] obj : results) {
   System.out.println(
   "Lampent pnumb: " + obj[0] +
   ", catergory: " + obj[1] +
   ", woltage: " + obj[2] +
   ", manuname: " + obj[3]);
}

Regards,

Craig

On Dec 5, 2007, at 11:24 AM, Eve Pokua wrote:

> Hello,
>
> Yes, same msg:
>
> [#|2007-12-05T19:00:44.843+0000|INFO|sun-appserver9.1|
> javax.enterprise.system.stream.out|
> _ThreadID=17;_ThreadName=httpSSLWorkerThread-8080-1;|
> Can not access DB data|#]
> [#|2007-12-05T19:00:53.609+0000|INFO|sun-appserver9.1|
> javax.enterprise.system.stream.out|
> _ThreadID=17;_ThreadName=httpSSLWorkerThread-8080-1;|
> Can not access DB data|#]
>
>
> here is the change code:
>
> public List<lightingspecreimp.Lampent> allLightingpro(){
> List results=emp.createNamedQuery("getAlldetails").getResultList();
> return results;
>
>
> }
>
> Could it be that I need to make changes to my toString method of my
> Lampent:
>
> public String toString()
> {String s= pnumb + " " + catergory + " " + lwoltage + " " +
> lamManufacturerent ;
> return s;}
>
>
> or even, the get and set methods in Lampent relating to
> lamManufacturerent :
>
> public LamManufacturerent getLamManufacturerent()
> {return lamManufacturerent;}
>
> public void setLamManufacturerent(LamManufacturerent
> lamManufacturerent)
> {this.lamManufacturerent=lamManufacturerent;}
>
> The above get method means it will return an object of
> lamManufacturerent not fields.
> So how do I get fields of lamManufacturerent from Lampent?
>
> Thanks
>
> eve
>
>
>
>
>
>
>
>
> Date: Tue, 4 Dec 2007 15:07:54 -0800
> From: Craig.Russell_at_Sun.COM
> To: persistence_at_glassfish.dev.java.net
> Subject: Re: question on entity relationships
>
> Hi eve,
>
> The return type of the query should not be List<Lampent> since
> you're not returning Lampent instances, just a bunch of fields.
>
> Do you get the same exception if you do:
> List results=emp.createNamedQuery("getAlldetails").getResultList();
>
> Craig
>
> On Dec 4, 2007, at 3:01 PM, Eve Pokua wrote:
>
> Hello,
>
> I have checked this and made changes to this:
>
> @NamedQuery(name ="getAlldetails", query=" SELECT l.pnumb,
> l.catergory, l.lwoltage, lm.manuname FROM Lampent l LEFT OUTER JOIN
> l.lamManufacturerent lm")
>
> I am still not able to retreive data. error msg:
>
> [#|2007-12-04T22:51:10.754+0000|INFO|sun-appserver9.1|
> oracle.toplink.essentials.session.file:/C:/Sun/SDK2/domains/domain1/
> applications/j2ee-apps/LIGHTINGSPEC/LIGHTINGSPEC-ejb_jar/-
> LIGHTINGSPEC-ejbPU3|
> _ThreadID=19;_ThreadName=httpSSLWorkerThread-8080-1;|file:/C:/Sun/
> SDK2/domains/domain1/applications/j2ee-apps/LIGHTINGSPEC/
> LIGHTINGSPEC-ejb_jar/-LIGHTINGSPEC-ejbPU3 login successful|#]
>
> [#|2007-12-04T22:51:11.234+0000|INFO|sun-appserver9.1|
> javax.enterprise.system.stream.out|
> _ThreadID=19;_ThreadName=httpSSLWorkerThread-8080-1;|
> Can not access DB data|#]
>
>
>
> here is the cde from my implementation:
>
> public List<lightingspecreimp.Lampent> allLightingpro(){
> List<Lampent> results=emp.createNamedQuery
> ("getAlldetails").getResultList();
> return results;
>
> }
>
> apprecaite your everything. thanks
>
> eve
>
>
>
> > From: gordon.yorke_at_oracle.com
> > To: persistence_at_glassfish.dev.java.net
> > Date: Mon, 3 Dec 2007 16:32:03 -0500
> > Subject: RE: question on entity relationships
> >
> > Hello eve,
> > Did you cut and paste that query or type it in? There is a typo
> in the FROM clause.
> >
> > @NamedQuery(name ="getAlldetails", query=" SELECT l.pnumb,
> l.catergory, l.lwoltage, lm.manuname FROM Lampent l LEFT OUTER JOIN
> *l*.LamManufacturerent lm")
> >
> > --Gordon
> >
> > -----Original Message-----
> > From: Eve Pokua [mailto:gorgeous65_at_msn.com]
> > Sent: Monday, December 03, 2007 4:28 PM
> > To: persistence_at_glassfish.dev.java.net
> > Subject: RE: question on entity relationships
> >
> >
> >
> >
> > Thanking you for your reply.
> >
> > James,
> >
> > LamManufacturerents is the foreign key in Lampent.
> LamManufacturerents exists in Lampent. So the query should be like
> this:
> >
> >
> > @NamedQuery(name ="getAlldetails", query=" SELECT l.pnumb,
> l.catergory, l.lwoltage, lm.manuname FROM Lampent l LEFT OUTER JOIN
> lm.LamManufacturerent lm")
> >
> > But i get the following errors:
> >
> > [#|2007-12-03T21:18:31.671+0000|INFO|sun-appserver9.1|
> oracle.toplink.essentials.session.file:/C:/Sun/SDK/domains/domain1/
> applications/j2ee-apps/LIGHTINGSPEC/LIGHTINGSPEC-ejb_jar/-
> LIGHTINGSPEC-ejbPU3|_ThreadID=34;_ThreadName=Thread-185;|file:/C:/
> Sun/SDK/domains/domain1/applications/j2ee-apps/LIGHTINGSPEC/
> LIGHTINGSPEC-ejb_jar/-LIGHTINGSPEC-ejbPU3 logout successful|#]
> >
> > [#|2007-12-03T21:18:31.687+0000|SEVERE|sun-appserver9.1|
> javax.enterprise.system.tools.deployment|
> _ThreadID=34;_ThreadName=Thread-185;_RequestID=3961ec6a-8774-47c5-876a
> -b986184d7369;|Exception occured in J2EEC Phase
> > java.lang.StackOverflowError
> > at java.util.HashMap.get(HashMap.java:300)
> > at
> oracle.toplink.essentials.internal.parsing.ParseTreeContext.pathForVar
> iable(ParseTreeContext.java:198)
> > at
> oracle.toplink.essentials.internal.parsing.VariableNode.resolveClass
> (VariableNode.java:282)
> > at oracle.toplink.essentials.internal.parsing.DotNode.resolveClass
> (DotNode.java:262)
> > at
> oracle.toplink.essentials.internal.parsing.VariableNode.resolveClass
> (VariableNode.java:287)
> > at oracle.toplink.essentials.internal.parsing.DotNode.resolveClass
> (DotNode.java:262)
> > at
> oracle.toplink.essentials.internal.parsing.VariableNode.resolveClass
> (VariableNode.java:287)
> > at oracle.toplink.essentials.internal.parsing.DotNode.resolveClass
> (DotNode.java:262)
> > at
> oracle.toplink.essentials.internal.parsing.VariableNode.resolveClass
> (VariableNode.java:287)
> >
> >
> > .............................
> >
> >
> > it gos on.
> >
> > Thankings everyone
> >
> > eve
> >
> > ----------------------------------------
> > > Date: Mon, 3 Dec 2007 07:08:22 -0800
> > > From: jamesssss_at_yahoo.com
> > > To: persistence_at_glassfish.dev.java.net
> > > Subject: RE: question on entity relationships
> > >
> > >
> > > Do you have any LamManufacturerent that have Lampent? This
> query will filter
> > > out the none case, i.e. you will only get back
> LamManufacturerents that have
> > > 1 or more Lampent.
> > >
> > > You could use an outerjoin if you do not wish to filter the
> none case.
> > >
> > > i.e.
> > > SELECT l.pnumb, l.catergory, l.lwoltage, lm.manuname FROM
> LamManufacturerent
> > > lm left outer join lm.lampent l
> > >
> > >
> > > Eve Pokua wrote:
> > > >
> > > >
> > > > Hello James/Everyone, I have finally tried your suggestion
> but I'm
> > > > having problems retrieving data. Below are my to related
> entities with
> > > > their relationships: @Entity_at_Table(name="LAMPS") @Named
> Queries({
> > > > @Named Query(name ="getAlldetails", query=" SELECT l.pnumb,
> l.catergory,
> > > > l.lwoltage, lm.manuname FROM LamManufacturerent lm, in
> (lm.lampent) l")}
> > > > public class Lampent implements Serializable
> { .........................
> > > > @ManyToOne private LamManufacturerent
> > > > lamManufacturerent;................... } @Entity_at_Table
> (name="LAMPMANU")
> > > > public class LamManufacturerent implements
> > > > Serializable{............................ @OneToMany(mappedBy =
> > > > "lamManufacturerent") private List<Lampent>
> lampent; .............
> > > > } Here is the JPQL: @Named Query(name ="getAlldetails",
> query=" SELECT
> > > > l.pnumb, l.catergory, l.lwoltage, lm.manuname FROM
> LamManufacturerent lm,
> > > > in(lm.lampent) l") Did I get it wrong because it is not
> working and not
> > > > retrieving any data. It however works, if I do some thing
> like this:
> > > > "Select l FROM Lampent l" It brings all data plus all the
> data from the
> > > > related entity LamManufacturerent. But i only want manuname from
> > > > LamManufacturerent. Im probably making just a little mistake.
> Please,
> > > > help. Thankseve ________________________________> From:
> > > > gorgeous65_at_msn.com> To: persistence_at_glassfish.dev.java.net>
> Date: Wed, 14
> > > > Nov 2007 15:07:03 +0000> Subject: RE: question on entity
> relationships> >
> > > > Thanks James> > I will try it and let you know.> > eve> >>
> Date: Wed, 14
> > > > Nov 2007 06:20:25 -0800>> From: jamesssss_at_yahoo.com>> To:
> > > > persistence_at_glassfish.dev.java.net>> Subject: Re: question on
> entity
> > > > relationships>>>>>> The JPQL would be,>>>> "Select i.id, i.name,
> > > > i.description, i.colour, i.size, o.oid, o.date,>> o.quantity
> from Order o,
> > > > in(o.items) i">>>> You could also just select the Order and
> fetch join the
> > > > items, if you wanted>> the objects back. Or just select the
> Orders can
> > > > access the items from them>> in Java.>>>> "Select o from
> Order o join
> > > > fetch o.items">>>> "Select o from Order o">>>>>> Eve Pokua
> wrote:>>>>>>>>>
> > > > Hello everyone,>>>>>> I would like to know how to retrieve
> certain data
> > > > from the DB with respect>>> to entity relationship. Consider
> the following
> > > > SQL statement.>>>>>> "Select i.id, i.name, i.description,
> i.colour,
> > > > i.size, o.oid, o.date,>>> o.quantity from Item i, Order
> o">>>>>> Or a very
> > > > complex SQL statement using JOIN in tables.>>>>>> The Order
> entity is a 1
> > > > to many relation to Item - an order can have many>>> items.
> But when
> > > > retrieving the details from order, I want to see certain>>>
> data from the
> > > > Order table as stated above, but not all the data from>>>
> Order. How do I
> > > > achieve this in EJB3 query?>>>>>> Thanks>>>>>> eve>>>>>>>>>
> ----->> --->>
> > > > http://wiki.eclipse.org/User:James.sutherland.oracle.com James
> > > > Sutherland>> http://www.oracle.com/technology/products/ias/
> toplink/ Oracle
> > > > TopLink ,>> http://www.eclipse.org/eclipselink/>> EclipseLink ,
> > > > https://glassfish.dev.java.net/javaee5/persistence/ TopLink>>
> Essentials>>
> > > > Wiki: http://en.wikibooks.org/wiki/Java_Persistence Java
> Persistence ,>>
> > > > http://wiki.eclipse.org/EclipseLink EclipseLink>> Forums:
> > > > http://forums.oracle.com/forums/forum.jspa?forumID=48
> TopLink ,>>
> > > > http://www.nabble.com/EclipseLink-f26430.html EclipseLink ,>>
> > > > http://www.nabble.com/java.net---glassfish-persistence-
> f13455.html
> > > > Glassfish>> Persistence>> -->> View this message in context:
> > > > http://www.nabble.com/question-on-entity-relationships-
> tf4804935.html#a13747259>>
> > > > Sent from the java.net - glassfish persistence mailing list
> archive at
> > > > Nabble.com.> > ________________________________> Get free
> emoticon packs
> > > > and customisation from Windows Live. Pimp My
> > > > Live!<http://www.pimpmylive.co.uk>
> > > >
> _________________________________________________________________
> > > > Get free emoticon packs and customisation from Windows Live.
> > > > http://www.pimpmylive.co.uk
> > > >
> > >
> > >
> > > -----
> > > ---
> > > http://wiki.eclipse.org/User:James.sutherland.oracle.com James
> Sutherland
> > > http://www.oracle.com/technology/products/ias/toplink/ Oracle
> TopLink ,
> > > http://www.eclipse.org/eclipselink/
> > > EclipseLink , https://glassfish.dev.java.net/javaee5/
> persistence/ TopLink
> > > Essentials
> > > Wiki: http://en.wikibooks.org/wiki/Java_Persistence Java
> Persistence ,
> > > http://wiki.eclipse.org/EclipseLink EclipseLink
> > > Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48
> TopLink ,
> > > http://www.nabble.com/EclipseLink-f26430.html EclipseLink ,
> > > http://www.nabble.com/java.net---glassfish-persistence-
> f13455.html Glassfish
> > > Persistence
> > > --
> > > View this message in context: http://www.nabble.com/RE%3A-
> question-on-entity-relationships-tf4930046.html#a14131601
> > > Sent from the java.net - glassfish persistence mailing list
> archive at Nabble.com.
> >
> > _________________________________________________________________
> > The next generation of MSN Hotmail has arrived - Windows Live
> Hotmail
> > http://www.newhotmail.co.uk
>
> The next generation of MSN Hotmail has arrived - Windows Live Hotmail
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell_at_sun.com
> P.S. A good JDO? O, Gasp!
>
>
> Are you the Quizmaster? Play BrainBattle with a friend now!

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell_at_sun.com
P.S. A good JDO? O, Gasp!