-->
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: Unable to create a "left join query"
PostPosted: Thu Sep 09, 2010 11:23 am 
Newbie

Joined: Mon Jul 21, 2008 3:15 pm
Posts: 7
Hi, I've a problem with the way Hibernate retrievs objects from the DB.

These are my Entities

@Entity
public class Company implements Serializable {

private static final long serialVersionUID = 1730757624645582910L;


private Integer id;
private String name;


@Id
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


@Entity
public class Employee implements Serializable {

private static final long serialVersionUID = -6481859546555223725L;

private Integer id;
private String name;
private Company company;

@Id
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@ManyToOne
@Fetch(FetchMode.JOIN)
public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

}

When I invoke this query:

query "from com.hjoin.dto.Employee"


Hibernate first executes this query:

select
employee0_.id as id9_,
employee0_.company_id as company3_9_,
employee0_.name as name9_
from
Employee employee0_


I assume to bring all employees and then, for every employee, executes this query:

select
company0_.id as id8_0_,
company0_.name as name8_0_
from
Company company0_
where
company0_.id=?


What I need is to define a model so Hibernate executes one unique query like so:


select
employee0_.id as id9_,
employee0_.company_id as company3_9_,
employee0_.name as name9_,
company0_.id as id8_0_,
company0_.name as name8_0_
from
Employee employee0_
left outer join
Company company0_
on employee0_.company_id = employee0_.id


So that every time I need to retrieve, in this case "Employee", (wether directly from a query or indirectly from another class) Hiberante will generate and execute an unique query.


Thx


PS: Couldn't use the [CODE] tags, didn't work for some reason.


Top
 Profile  
 
 Post subject: Re: Unable to create a "left join query"
PostPosted: Thu Sep 09, 2010 1:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In your query, you'll need to explicitly specify that you want to fetch the company. Eg. instead of

Code:
from com.hjoin.dto.Employee


you'll need something like:

Code:
select e from com.hjoin.dto.Employee e left join fetch e.company


The @Fetch annotations are usually not used when you do HQL queries. It is explained here: http://docs.jboss.org/hibernate/stable/ ... ing-custom


Top
 Profile  
 
 Post subject: Re: Unable to create a "left join query"
PostPosted: Thu Sep 09, 2010 2:50 pm 
Newbie

Joined: Mon Jul 21, 2008 3:15 pm
Posts: 7
Thanks very much.


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.