-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate issuing mulitple selects after join
PostPosted: Tue Feb 14, 2006 2:52 pm 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
Hi,

I have created a simple one-to-one mapping and after issuing the correct hibernate-generated SQL involving a JOIN, it starts issuing single selects for each and every ID in the table. I have done the same thing with other classes, the only difference being that the identifying column names are different in this case, and it worked fine (i.e. only the JOIN query was executed). My mapping code is below, along with the two classes, and the "findAll" query.

Any help would be hugely appreciated. I have spent much time on this, and tried every permutation of attributes that made sense to me, to no avail.

Thanks.

Lee.

Hibernate version: 3.1.1
Database: MySQL 5

public class Model {

private String modelCode;
private Integer year;
private String productLine;
private String fullName;

private ModelCategory category;

public Model() {
super();
}

public boolean isHonda() {
return productLine.equals("A");
}

public String getFullName() {
return fullName;
}

private void setFullName(String fullName) {
this.fullName = fullName;
}

public String getModelCode() {
return modelCode;
}

private void setModelCode(String modelCode) {
this.modelCode = modelCode;
}

private String getProductLine() {
return productLine;
}

private void setProductLine(String productLine) {
this.productLine = productLine;
}

public String getSimpleName() {
return category.getName();
}

public Integer getYear() {
return year;
}

private void setYear(Integer year) {
this.year = year;
}

public ModelCategory getCategory() {
return category;
}

public void setCategory(ModelCategory category) {
this.category = category;
}
}

public class ModelCategory {

private String modelCd;
private String name;

public ModelCategory() {
super();
}

public String getName() {
return name;
}

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

public String getModelCd() {
return modelCd;
}

public void setModelCd(String modelCd) {
this.modelCd = modelCd;
}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Model" table="MODEL_S" dynamic-update="true" dynamic-insert="false" lazy="false">
<id name="modelCode" column="MODEL_CDE"/>
<property name="year" column="MODEL_YEAR" not-null="true"/>
<property name="productLine" column="PROD_LINE" not-null="true"/>
<property name="fullName" column="MODEL_NAME" not-null="true"/>

<one-to-one name="category" lazy="false"/>
</class>

<class name="ModelCategory" table="MDLCTG_S">
<id name="modelCd" column="MDLCDE"/>
<property name="name" column="CTGNM"/>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 5:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You forgot to post the query. Try using code tags when posting it.


Top
 Profile  
 
 Post subject: Query
PostPosted: Tue Feb 14, 2006 5:56 pm 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
Sorry, I don't know what "code tags" are, but here is the query:

Criteria crit = getSession().createCriteria(getPersistentClass());
crit.add(Restrictions.in("productLine", productLines));
crit.add(Restrictions.ge("year", new Integer(2006)));
return crit.list();

even removing the crit.add statements yields the same problem. The getPersistentClass() simply returns the Model class.

Thanks.

Lee.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 6:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Criteria use (I think) the mapping to determine how to fetch associated objects. You haven't set 'fetch="join"' in your one-to-one mapping, so it fetches them immediately (you've specified lazy="false") using selects.

To fix this, you can either switch to HQL, which allows you to override that option (using the "join fetch" syntax), or you can set 'fetch="join"' in your mapping.


Top
 Profile  
 
 Post subject: join fetching
PostPosted: Tue Feb 14, 2006 11:10 pm 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
I tried that already. I haven't used HQL, but would like to avoid it since I did the exact same thing elsewhere and it worked. In fact, the first query executed is indeed a join that fetches all the correct row. But after that, it starts fetching each row of MDLCTG (ModelCategory) using a single select. I'm at my wits end on this one - would pay to have it fixed at this point.

I guess I can dig a bit further by stepping through source, but that could be quite time consuming, and I really don't want to have to do that.

Any other suggestions? I think I've tried every attribute of one-to-one, with different combinations. Is there any other way to do this besides using a one-to-one? I also tried many-to-one, but got the same problem.

Thanks.

Lee.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 11:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The fetch="join" option should work. Could it have something to do with the depth of the join? There's a separate option that limits the join depth. In your config file, try increasing the value of hibernate.max_fetch_depth. I use 2, but you could test with 10, and reduce it as necessary.


Top
 Profile  
 
 Post subject: Still doing the single selects
PostPosted: Wed Feb 15, 2006 10:37 am 
Newbie

Joined: Tue Feb 14, 2006 2:40 pm
Posts: 9
Location: Toronto
Hi,

Setting hibernate.max_fetch_depth did not work.


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