persistence@glassfish.java.net

Re: retrieving data from two related entities(table)

From: Eve Pokua <gorgeous65_at_msn.com>
Date: Thu, 26 Apr 2007 11:13:01 +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


Hello everyone,

I have two ralated entities as below:

Usersent

and

Machineent

the relation is one user to many machines. So each machine must have at
least one user.

So the relation is specify as:
public class Machineent......{
@Id
   @Column(name = "COMPUTNAME", nullable = false)
   private String comptername;
......
@ManyToOne()
   @JoinColumn(name="USERID")
   private Usersent usersent;

and:

public class Usersent.........
@Id
   @Column(name="USERID", nullable=false)
   private String UserID;
..........

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

Now in the Machineent I have specified methods which should be used to set
and get data from the database of the Usersent table


public Usersent getUserID() {
       return usersent;
   }

   public void setUserID(String UserID) {
       this.usersent = usersent;}


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"),

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?

Thanks

eve

_________________________________________________________________
Txt a lot? Get Messenger FREE on your mobile.
https://livemessenger.mobile.uk.msn.com/