-->
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.  [ 4 posts ] 
Author Message
 Post subject: save method always executes select of child object
PostPosted: Sat Feb 12, 2011 6:39 am 
Newbie

Joined: Sat Jul 12, 2008 4:17 am
Posts: 9
Code:
package com.abc.common.hibernate.dao.test.domain;

import java.util.Date;

/**
* Company generated by hbm2java
*/
public class Company  implements java.io.Serializable {


     private String id;
     private CompanyType companyType;
     private String name;
    public Company() {
    }

   
   
    public String getId() {
        return this.id;
    }
   
    public void setId(String id) {
        this.id = id;
    }
    public CompanyType getCompanyType() {
        return this.companyType;
    }
   
    public void setCompanyType(CompanyType companyType) {
        this.companyType = companyType;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
}


package com.iblgrp.common.hibernate.dao.test.domain;
// Generated Feb 6, 2011 1:59:59 PM by Hibernate Tools 3.2.1.GA


import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* CompanyType generated by hbm2java
*/
public class CompanyType  implements java.io.Serializable {


     private String id;
     private String title;
    private Set<Company> companies = new HashSet<Company>(0);

    public CompanyType() {
    }

   
   
    public String getId() {
        return this.id;
    }
   
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return this.title;
    }
   
    public void setTitle(String title) {
        this.title = title;
    }
    public Set<Company> getCompanies() {
        return this.companies;
    }
   
    public void setCompanies(Set<Company> companies) {
        this.companies = companies;
    }

}

<?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">

<!-- Generated Feb 6, 2011 1:59:59 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
  <class  name="com.iblgrp.common.hibernate.dao.test.domain.Company" table="gs_company">
    <id name="id" type="string">
      <column length="30" name="id"/>
      <generator class="assigned"/>
    </id>
    <many-to-one class="com.iblgrp.common.hibernate.dao.test.domain.CompanyType" fetch="select" name="companyType" >
      <column length="30" name="company_type" not-null="true"/>
    </many-to-one>
    <property name="name" type="string">
      <column length="150" name="name"/>
    </property>

  </class>
</hibernate-mapping>
<?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">
<!-- Generated Feb 6, 2011 1:59:59 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
  <class name="com.iblgrp.common.hibernate.dao.test.domain.CompanyType" table="gs_company_type">
    <id name="id" type="string">
      <column length="30" name="id"/>
      <generator class="assigned"/>
    </id>
    <property name="title" type="string">
      <column length="150" name="title" not-null="true"/>
    </property>
    <set inverse="true" name="companies">
      <key>
        <column length="30" name="company_type" not-null="true"/>
      </key>
      <one-to-many class="com.iblgrp.common.hibernate.dao.test.domain.Company"/>
    </set>
  </class>
</hibernate-mapping>



Above are two simple many to one beans the problem is that whenever I try to insert company it first execute select statement of company_type I don't want to execute this select statement


Code:
Company company=new Company();
    CompanyType companyType=new CompanyType();
    companyType.setId("1");
    company.setCompanyType(companyType);
    company.setId("16");
    company.setName("SAve Test");
    companyDAO.save(company); //this method call session.save method 


Hibernate: select companytyp_.id, companytyp_.title as title1_from gs_company_type companytyp_ where companytyp_.id=?
Hibernate: insert into gs_company ( company_type, name, id) values (?, ?, ?)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.981 sec


Top
 Profile  
 
 Post subject: Re: save method always executes select of child object
PostPosted: Mon Feb 14, 2011 9:30 am 
Newbie

Joined: Sat Feb 12, 2011 6:26 am
Posts: 2
I also have the same question/problem.

The only difference is that I am using Hibernate Annotation instead of .xml file.
Does anyone have any reasoning of WHY Hibernate executs SELECT statements before executing INSERT statement ?

rizzz86


Top
 Profile  
 
 Post subject: Re: save method always executes select of child object
PostPosted: Mon Feb 14, 2011 10:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Because your CompanyType ids are assigned, Hibernate has no idea if a given id value exists yet in the database when it sees one. So by default it checks against the database to see.

If this CompanyType already exists, then instead do a Session.load( CompnayType.class, 1 )


Top
 Profile  
 
 Post subject: Re: save method always executes select of child object
PostPosted: Fri Feb 18, 2011 1:07 am 
Newbie

Joined: Sat Jul 12, 2008 4:17 am
Posts: 9
Thanks


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