-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-to-Many Query
PostPosted: Mon Sep 21, 2009 9:39 pm 
Newbie

Joined: Wed Jun 18, 2008 9:17 am
Posts: 12
Hi,

Just been playing around with hibernate and i came across this issue which i dont know how to solve.
Any help will be appreciated.

I have a 2 Entities.

Client Entity has collection of Session Entities.

Client.java

public class ClientDO implements Serializable{
@Id
@Column(name="clientID")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String status;
@Column(name="DateOfBirth")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dob;
private String login;
private Integer testerid;
private String type;
private String role;

@OneToMany(cascade=CascadeType.PERSIST,
mappedBy="id",
fetch=FetchType.EAGER)
@JoinColumn(name="clientID",nullable=false)
private Set<SessionDO> sessions;

}

Session.java
@Entity
@Table(name="session")
public class SessionDO implements Serializable {
@Id
@Column(name="sessionID")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@ManyToOne(optional=false)
private ClientDO client;
...
}

When I load client object, SQL produced by Hibernate looks like:
select clientdo0_.clientID as clientID0_2_, clientdo0_.status as status0_2_, clientdo0_.DateOfBirth as DateOfBi3_0_2_, clientdo0_.login as login0_2_, clientdo0_.testerid as testerid0_2_, clientdo0_.type as type0_2_, clientdo0_.role as role0_2_, sessions1_.clientID as clientID4_, sessions1_.sessionID as sessionID4_, sessions1_.sessionID as sessionID1_0_, sessions1_.client_clientID as client7_1_0_, sessions1_.status as status1_0_, sessions1_.productId as productId1_0_, sessions1_.suffix as suffix1_0_, sessions1_.hand as hand1_0_, sessions1_.demographicsConfirmed as demograp6_1_0_, clientdo2_.clientID as clientID0_1_, clientdo2_.status as status0_1_, clientdo2_.DateOfBirth as DateOfBi3_0_1_, clientdo2_.login as login0_1_, clientdo2_.testerid as testerid0_1_, clientdo2_.type as type0_1_, clientdo2_.role as role0_1_
from client clientdo0_
left outer join session sessions1_ on clientdo0_.clientID=sessions1_.clientID
left outer join client clientdo2_ on sessions1_.client_clientID=clientdo2_.clientID where clientdo0_.clientID=?

Why is there an extra outer join when only 1 join is needed to fetch the data required. Any idea?


Top
 Profile  
 
 Post subject: Re: One-to-Many Query
PostPosted: Tue Sep 22, 2009 5:19 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
I Think , your mapping is wrong..
Try this...

Client.java

public class ClientDO implements Serializable{
@Id
@Column(name="clientID")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String status;
@Column(name="DateOfBirth")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dob;
private String login;
private Integer testerid;
private String type;
private String role;

@OneToMany(cascade=CascadeType.PERSIST,
mappedBy="client",
fetch=FetchType.EAGER)
private Set<SessionDO> sessions;

}

Session.java
@Entity
@Table(name="session")
public class SessionDO implements Serializable {
@Id
@Column(name="sessionID")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@ManyToOne(optional=false)
@JoinColumn(name="clientID",nullable=false)
private ClientDO client;
...
}


Top
 Profile  
 
 Post subject: Re: One-to-Many Query
PostPosted: Tue Sep 22, 2009 9:01 pm 
Newbie

Joined: Wed Jun 18, 2008 9:17 am
Posts: 12
Thanks. It was the mapping issue as you pointed out.


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