persistence@glassfish.java.net

Re: retrieving data from two related entities(table)

From: Eve Pokua <gorgeous65_at_msn.com>
Date: Wed, 02 May 2007 11:02:07 +0100

Hello everyone,

I have been able to resolve this issue. Thanks to all your suggestions.

I believed I made the situation more complicated than it should have been.
If anyone is interested, the following is how I resolved it:

The relationship of my two tables/entities are as below:

public class Machineent implements Serializable {

@ManyToOne()
    @JoinColumn(name="USERID")
    private Usersent usersent;

.................}

public class Usersent implements Serializable {

@OneToMany(mappedBy="usersent")
    private Collection <Machineent> machineents;

.................}

So by using the following set method, I am able to put data into my database
successively:

Machineent{
public void setUserID(Usersent usersent) {
        this.usersent = usersent;}
.........}

In my implmentation class I use the following method:

public List<Machineent> searchbyusername(String UserID){
             List<Machineent> machineents = null;
             try{

             machineents=(List<Machineent>)
em.createNamedQuery("Machineent.findMachinebyuser").setParameter("UserID"
,UserID).getResultList();
           return machineents;
        }catch(Exception ex){
            throw new EJBException(ex);}
        }

WHich retrieves the details using the userID provided by the client/User.

I also have the following method inside the other implementation class:

public Usersent searchbyusername(String UserID){

             try{
             Usersent usersent=em.find(Usersent.class,new String(UserID));
            
em.createNamedQuery("Machinedetail2.Usersent.findMachinebyuser").setParameter("UserID"
,UserID).getSingleResult();
           return usersent;
        }catch(Exception ex){
            throw new EJBException(ex);}
        }......



In my client I request information from both my implementation:

Machinesremote machbean=(Machinesremote)ctx.lookup("ejb/MachinebJNDI");
        UsersRemote users=(UsersRemote)ctx.lookup("UsersbJNDI");
        Usersent usersent =users.searchbyusername(muser);

        List<Machineent> machineent =machbean.searchbyusername(muser);

      for (Machineent allmachlist: machineent){

       out.println("<td>");
out.println(allmachlist.getcompModle());out.println("</td>");
        out.println("<td>");
out.println(allmachlist.getcomptername());out.println("</td>");
        out.println("<td>");
out.println(allmachlist.getcomptername());out.println("</td>");

        out.println("<td>");
out.println(usersent.getUserid());out.println("</td>");
         out.println("<td>");
out.println(usersent.getSurname());out.println("</td>");
          out.println("<td>");
out.println(usersent.getFirstname());out.println("</td>");
    }

Which provides the following information:

null SAT SAT U paul white

So the user's name is Paul white. I need to make it look a little
presentable but I got what I was looking for.

I am not sure if this is the best way but it provides the exact information
I was looking for and it does exactly what I was trying to achieve. I will
welcome any other suggestions.

Thanks

eve


>From: "Eve Pokua" <gorgeous65_at_msn.com>
>Reply-To: persistence_at_glassfish.dev.java.net
>To: ejb_at_glassfish.dev.java.net, guruwons_at_gmail.com
>CC: persistence_at_glassfish.dev.java.net
>Subject: Re: retrieving data from two related entities(table)
>Date: Thu, 26 Apr 2007 09:54:31 +0100
>
>Yes Wonseok,
>
>:
>
>I could not get an clear understanding what you're looking for.
>>Could you
>>tell more detail?
>>If you are looking for outer join, you could still use it in JPQL. Or are
>>you looking for projection feature like below?
>>
>>SELECT new XXX(e.id, e.name, e.salary, e.status, d.name)
>> FROM Employee e JOIN e.department d WHERE e.address.state = ?1
>
>I am trying to do something like what you suggested above and more, like
>below:
>
>SELECT new XXX(e.id, e.name, e.salary, e.status, d.name, d.description)
> FROM Employee e JOIN e.department d WHERE e.address.state = ?1
>
>So from the join entity I would like to retrieve not just the id field but
>also other fields relating to a particular id.
>
>For example:
>
>This is what I get when I use the following query:
>
>@NamedQuery(name = "Machineent.findMachinebyuser",query = "SELECT m FROM
>Machineent m JOIN m.usersent us WHERE us.UserID=:UserID")
>
>
>
>null SAT Machinedetail2.Usersent_at_19455f1
>
>So it retrieves data from the Machineent but not the usersent. Basically,
>because I am calling the method from just the Machineent entity but I need
>to refer to both the Machineent and usersent entities i.e both the tables
>from the entities, as u suggested. My client is like below:
>
>List<Machineent> machineent =machbean.searchbyusername("u");
>
>
> for (Machineent allmachlist: machineent){
>
> out.println("<td>");
>out.println(allmachlist.getcompModle());out.println("</td>");
> out.println("<td>");
>out.println(allmachlist.getcomptername());out.println("</td>");
> out.println("<td>");
>out.println(allmachlist.getUserID());out.println("</td>");
> }
>
>So you can understand why this:
>
>out.println("<td>");
>out.println(allmachlist.getUserID());out.println("</td>");
>is only giving this:
>
>Machinedetail2.Usersent_at_19455f1
>
>as the getUserID():
>
>public class Machineent implements Serializable {
>
>..........
>
>............
>
>..........
>
> public Usersent getUserID() {
> return usersent;
> }
>
>I hope this helps.
>
>Thanks
>
>eve
>
>
>
>
>
>>From: "Wonseok Kim" <guruwons_at_gmail.com>
>>Reply-To: ejb_at_glassfish.dev.java.net
>>To: ejb_at_glassfish.dev.java.net
>>Subject: Re: retrieving data from two related entities(table)
>>Date: Thu, 26 Apr 2007 13:35:44 +0900
>>
>>Hi Eve,
>>You might get more answers if you send email to
>>persistence_at_glassfish.dev.java.net list.
>>
>>My comments are inline...
>>
>>On 4/25/07, Eve Pokua <gorgeous65_at_msn.com> wrote:
>>>
>>>Hello everyone,
>>>
>>>I have a query as:
>>>
>>>@NamedQuery(name = "Machineent.findMachinebyuser",query = "SELECT us
>>>FROM
>>>Usersent us WHERE us.UserID=:UserID")
>>>
>>>I also tried the following query.
>>>
>>>@NamedQuery(name = "Machineent.findMachinebyuser",query = "SELECT m FROM
>>>Machineent m JOIN m.usersent us WHERE us.UserID=:UserID"),
>>
>>
>>Those two named query has the same name. Is it just your typo or mistake
>>in
>>the code?
>>
>>In my client I have:
>>>
>>>List<Machineent> machineent =machbean.searchbyusername ("u");
>>>
>>>
>>>
>>>for (Machineent allmachlist: machineent){
>>>
>>>
>>>out.println("<td>");
>>>out.println(allmachlist.getcompModle());out.println("</td>");
>>> out.println ("<td>");
>>>out.println(allmachlist.getcomptername());out.println("</td>");
>>> out.println("<td>");
>>>out.println(allmachlist.getUserID());out.println("</td>");
>>> }
>>>
>>>but it does not do exactly what I am looking for. The bottom line is, I
>>>would like to able to retrieve data from both or either tables of the
>>>entities. What is the best way to go about this?
>>
>>
>>I could not get an clear understanding what you're looking for. Could you
>>tell more detail?
>>If you are looking for outer join, you could still use it in JPQL. Or are
>>you looking for projection feature like below?
>>
>>SELECT new XXX(e.id, e.name, e.salary, e.status, d.name)
>> FROM Employee e JOIN e.department d WHERE e.address.state = ?1
>>
>>Regards,
>>-Wonseok
>
>_________________________________________________________________
>Get Hotmail, News, Sport and Entertainment from MSN on your mobile.
>http://www.msn.txt4content.com/

_________________________________________________________________
Solve the Conspiracy and win fantastic prizes.
http://www.theconspiracygame.co.uk/