-->
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: Optimization problem
PostPosted: Wed Aug 03, 2005 4:47 am 
Newbie

Joined: Tue Aug 02, 2005 10:50 am
Posts: 6
Hi everyone,

I'm a new user of Hibernate and I have a strange problem :

I've 2 classes : Customer AND Story which look like :

Code:
public class Customer {
  private long id;
  private String number;
  private List historique = new ArrayList();

  public Customer() {
  }

  public void setId(long newId) {
     id=newId;
  }
      
  public long getId() {
     return id;
  }

   public void setNumber(String newNumber) {
      this.number=newNumber;
   }
      
   public String getNum_prospect() {
      return this.num_prospect;   
   }
}


Code:
public class Story {
  private long id;
  private Calendar dateHisto;
  private Customer customer;

  public Story() {   
  }
   
  public void setId(long newId) {
     id=newId;
  }
      
  public long getId() {
     return id;
  }
      
  public void setDateHisto(Calendar newDateHisto) {
    if (newDateHisto!=null)
       this.dateHisto=newDateHisto;
  }
   
  public Calendar getDateHisto() {
     return this.dateHisto;
  }
   
  public void setCustomer(Customer newCustomer) {
     this.customer=newCustomer;
  }
   
  public Customer getCustomer() {
     return this.customer;
  }      
}


And the corresponding mapping file looks like :

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.abw.beans">
<class name="com.abw.beans.Customer" table="PROSPECT" lazy="true">
  <id name="id" column="ID_PROSPECT">
     <generator class="increment" />
  </id>
   <property name="number" column="NUM_PROSPECT" />
  <bag name="story" lazy="true" cascade="all" order-by="DATE_HISTO">
   <key column="id_prospect"/>
   <one-to-many class="Historique"/>
  </bag>
</class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.abw.beans">
   <class name="com.abw.beans.Story" table="HISTORIQUE" lazy="true">
  <id name="id" column="ID_HISTORIQUE">
    <generator class="increment" />
  </id>
  <property name="dateHisto" column="DATE_HISTO" />
  <many-to-one name="customer" column="id_prospect" not-null="true"/>
</class>
</hibernate-mapping>



And my problem is :
When i make a simple request like :
Code:
public Vector getCustomer(int max_result,int deb_result) {
  Session session = HibernateUtil.currentSession();
  Transaction tx = session.beginTransaction();
  Query q = session.createQuery("select customer from Customer");
  q.setMaxResults(max_result);
  q.setFirstResult(deb_result);
  List result = q.list();
  //tx.commit();
  Vector v = new Vector(result.size());
  v.addAll(result);
  return v;
}

AND no reference (at all !!!) to the class Story

When I see the SQL log I can see query like :
select ... from Customer where ...
select ... from Story where ...
select ... from Story where ...
select ... from Story where ...
select ... from Story where ...
...

So my question/problem is :
WHY hibernate make request to the Story objet ???
I have set lazy to true and NO reference to the object !!!

I have read a lot of documentation and I have found no answer.
So please help me !!!!

_________________
HuGuEs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 8:16 am 
Beginner
Beginner

Joined: Wed Jun 08, 2005 10:01 am
Posts: 22
Location: Italy
Hi !!

try to use cascade="none" in:

Code:
<hibernate-mapping package="com.abw.beans">
   <class name="com.abw.beans.Story" table="HISTORIQUE" lazy="true">
  <id name="id" column="ID_HISTORIQUE">
    <generator class="increment" />
  </id>
  <property name="dateHisto" column="DATE_HISTO" />
  <many-to-one name="customer" column="id_prospect" cascade="none" not-null="true"/>
</class>
</hibernate-mapping>


bye,
nicola


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 8:59 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I realize you were probably trying to simplify things, but there are some issues with the code you pasted and it won't actually work.

Customer has the following getter method but no field - I assume this is number.

Code:
public String getNum_prospect() { return this.num_prospect;  }


Customer has a field
Code:
private List historique = new ArrayList();
but no getter/setter and the mapped collection is named story.

The story collection is mapped to a Class Historique which I am assuming is Story.

The Query "select customer from Customer" is invalid because customer (lower-case) isn't a mapped object. It should be either "from Customer" or "select customer from Customer customer".

However when I modified it the way I thought it should be, it works as expected without all the extra SELECT statements so I can only assume that my understanding of your code is different than the actual code.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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.