-->
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.  [ 5 posts ] 
Author Message
 Post subject: Novice question about joins
PostPosted: Thu Mar 12, 2009 1:48 pm 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Hello,
This may be simple, but I havent found an answer yet, please help.

I have two tables, A and B which are in many-to many relationship to each other, so there is also table C, containing A.id and B.id pairs. I want to fetch entities from B, which relate to single known id of A. I do not want to fetch any A fields, so that resultset is a nice collection of B entities. This is easily done in native SQL, but how to do that in HQL?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 2:39 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Here is one example:

Code:
Query q = session.createQuery("select b from B b join b.a a where a.id = :aId");
q.setInteger("aId", 17);
List listOfB = q.list();


Assuming that in B you have something like:

Code:
<set name="a" table="C">
  <key column="bId" />
  <many-to-many column="aId" class="A" />
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 3:56 pm 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Thank you for your answer.

I think, I better add some clarity. A=user, B=client, C=user_client tables. User entity may contain one or more Client entities, while Client entity in reality will not contain any User entities and has no appropriate field. Though, database structure does contain many-to-many table and is not available for modification.

As you said, I created a named query like this:
Code:
SELECT c FROM Client c JOIN c.User u WHERE u.id = :userId


But I get this error:
Quote:
Unable to build EntityManagerFactory


I presume, this is because my Client entity does not contain any User fields or properties, so Hibernate cannot process "c.User" part of HQL.

In regard to your last piece of xml code, I'm not sure where it goes, since I have no hibernate configuration files which would define entities relationship.

Here are my two entities:

Code:
@Entity
@NamedQueries ({
    @NamedQuery(name="Client.findByEntityId", query="SELECT c FROM Client c WHERE c.entityId = :entityId"),
    @NamedQuery(name="Client.findByUserId", query="SELECT c FROM Client c JOIN c.User u WHERE u.id = :userId")}
)
public class Client implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private Long entityId;

    ...
    getters and setters skipped, no annotations below this line



Code:
@Entity
@Table(name="User")
@NamedQueries({
    @NamedQuery(name = "User.login", query = "SELECT t FROM User t WHERE t.name = :username and t.password=:password")
})
public class User implements Serializable, XMLSerializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToMany
    protected  List<Client> clients = new ArrayList<Client>();

    private String name;
    private String password;
    private Long role;
    private String realName;

    ...
    getters and setters skipped, no annotations below this line


As you can see, there is OneToMany annotation, though as I said, database dictates many-to-many. I guess, the problem is not the query itself, but something is wrong with my entities description.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 5:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am just wondering why you didn't provide the more detailed information in your first post... The information in the first post is not exactly "compatible" with the information in the last.

Well... you should be able to do this.

Code:
select u.clients from User u where u.id = userId


But a @OneToMany will not work. It assumes that there is a column in the Client table that contains the user id. You need to map it with a @ManyToMany and a @JoinTable. Check section 2.2.5.3.3 in http://www.hibernate.org/hib_docs/annot ... ollections

Quote:
Unable to build EntityManagerFactory


This is some kind of configuration problem. Maybe it is related to the named query. It is hard to tell with incomplete error message. But if you don't have mapped a User from Client the query will not work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 7:02 pm 
Newbie

Joined: Thu Mar 12, 2009 9:38 am
Posts: 6
Thank you for your answers, very helpful.

Just thought I got a minor syntax issue, which, after looking closer, happened to be otherwise.

I will adjust my entities to better reflect database structure and hopefully it will help.

Thank you again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.