-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: NamedQuery for multiple table SELECT
PostPosted: Mon Jul 31, 2006 3:17 am 
Newbie

Joined: Wed Mar 29, 2006 4:37 am
Posts: 3
Hi

I have a JSF form with multiple input fields, they values are passed to
a facade session bean, wich in turn accesses an ejb3 entity bean. The
DB contains a structure like this:

Code:
                      Table AB 
Table A            ------------            Table B
----------          | int id_a |           ----------
| int id |          | int id_b |           | int id |
----------          ------------           ----------
| int f1 |<---------| int f1   |---------->| int f1 |
| int f2 |          | int f2   |           | int f2 |
|  ....  |          |  ....    |           |  ....  |
| int f1 |          | int f1   |           | int f1 |
----------          ------------           ----------

and the entity bean is mapped on 'Table A'. Is there a way to define a NamedQuery like this?

Code:
  SELECT OBJECT(a)
    FROM A  AS a,
         AB AS ab,
         B  AS b
   WHERE a.id = ab.id_a
     AND b.id = ab.id_b
     AND a.f1 = :p_input1
     AND ab.f2 = :p_input2
     AND .....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 5:12 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Sure.
Why not?
Have you had any problems?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 5:35 am 
Newbie

Joined: Wed Mar 29, 2006 4:37 am
Posts: 3
alesj wrote:
Sure.
Why not?
Have you had any problems?


Hi alesj

Yes, I have problems. It seams that the join conditions (a.id = ab.id.id_a, ab.id.id_b = b.id) are the problem.
Have you any suggestion?

A JAVA classes are these:

--------- A -------------
import java.util.*;
import javax.persistence.*;

@Entity
@Table(name = "A")
@NamedQueries({
@NamedQuery(name="getAByParameters",
query="SELECT OBJECT(a)
FROM A AS a,
AB AS ab,
B AS b
WHERE a.id = ab.id.id_a
AND ab.id.id_b = b.id
AND b.name like :p_name")
})
public class A implements java.io.Serializable {

private static final Logger log = Logger.getLogger(A.class);
private int id;
private Date date;
private Collection<B> bs = new ArrayList<B>(0);

public A() {
}

public A(int id,
Date date) {
this.id = id;
}

public A(int id,
Date date,
Collection<b> p_bs) {
this.id = id;
this.date = date;
this.bs = p_bs;
}

@Id
@Column(name="ID")
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }

@Column(name="DATE")
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }

@ManyToMany
@JoinTable(name="AB",
joinColumns={@JoinColumn(name="id_a")},
inverseJoinColumns={@JoinColumn(name="id_b")}
)
public Collection<B> getBs() { return this.bs; }
public void setBs(Collection<B> p_bs) { this.bs = p_bs; }

}

-------------- AB -----------------
import java.util.*;
import javax.persistence.*;

@Entity
@Table(name = "AB")
public class AB implements java.io.Serializable {

private ABId id;

public AB() {
}

public AB(ABId id) {
this.id = id;
}

@EmbeddedId
public ABId getId() { return this.id; }
public void setId(ABId id) { this.id = id; }

}

------------------- ABId ------------
import javax.persistence.*;

@Embeddable
public class ABId implements java.io.Serializable {

private int id_a;
private int id_b;

public ABId() {
}

public ABId(int id_a, int id_b) {
this.id_a = id_a;
this.id_b = id_b;
}

@Column(name="ID_A")
public int getId_a() { return this.id_a; }
public void setId_a(int id_a) { this.id_a = id_a; }

@Column(name="ID_B")
public int getId_b() { return this.id_b; }
public void setId_b(int id_b) { this.id_b = id_b; }

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof ABId) ) return false;
ABId castOther = ( ABId ) other;
return (this.getId_a()==castOther.getId_a())
&& (this.getId_b()==castOther.getId_b());
}

public int hashCode() {
int result = 17;
result = 37 * result + this.getId_a();
result = 37 * result + this.getId_b();
return result;
}
}


--------------------- B -----------------------
import java.util.*;
import javax.persistence.*;

@Entity
@Table(name = "B")
public class B implements java.io.Serializable {

private int id;
private String name;
private Collection<A> as = new ArrayList<A>(0);

public B() {
}

public B(int id,
String name) {
this.id = id;
this.name = name;
}

public B(int id,
String name,
Collection<A> as) {
this.id = id;
this.name = name;
this.as = as;
}

@Id
@Column(name="ID")
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }

@Column(name="name")
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }

@ManyToMany
@JoinTable(name="AB",
joinColumns={@JoinColumn(name="id_b")},
inverseJoinColumns={@JoinColumn(name="id_a")}
)
public Collection<A> getAs() { return this.as; }
public void setAs(Collection<A> as) { this.as = as; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 7:03 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Use simple composite many-to-one id on AB class.

Code:
public class AB {

private A a;
private B b;

.... setter and getters

}

        <composite-id>
            <key-many-to-one name="a" class="A">
                <column name="a_id"/>
            </key-many-to-one>
            <key-many-to-one name="b" class="B">
                <column name="b_id"/>
            </key-many-to-one>
        </composite-id>



Find the equivalent annotations to this xml, if you want to use just annotations.

Than in query use plain object oriented HQL.
It should work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.