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