I have an Advertisement.java class that maintains a Set of AdvParameter objects. AdvParameter is a composite-element that contains a many-to-one association to a Parameter object and a value property.
The association from Advertisement to set of AdvParameters is defined as inverse=false.
The problem is everytime I retrieve an Advertisement from the database,
all AdvParameter records gets deleted and reinserted again.
How can I prevent this delete & reinsert?
I can't change inverse to 'true' as AdvParameter records only get inserted when I create a new Advertisement and AdvParameter is a composite-element.
All details are below.
Thanks in advance,
Cagan
Hibernate version: 2.1
Mapping documents:
Advertisement.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="false">
<class
name="com.emirca.touch.module.advert.model.Advertisement"
table="tbl_advertisement"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
/>
<set
name="parameters"
table="tbl_adv_parameters"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="adv_id"
>
</key>
<composite-element
class="com.emirca.touch.module.advert.model.AdvParameter"
>
<many-to-one
name="parameter"
class="com.emirca.touch.module.advert.model.Parameter"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="parameter_id"
/>
<property
name="value"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="value"
/>
</composite-element>
</set>
<component
name="amount"
class="com.emirca.touch.module.share.model.share.Price"
>
<property
name="amount"
type="java.lang.Float"
update="true"
insert="true"
access="property"
column="amount"
/>
<many-to-one
name="currency"
class="com.emirca.touch.module.share.model.share.Currency"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="currency_id"
/>
</component>
<property
name="clicks"
type="long"
update="true"
insert="true"
access="property"
column="clicks"
/>
<property
name="expDate"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="exp_date"
/>
<property
name="maxClicks"
type="long"
update="true"
insert="true"
access="property"
column="max_clicks"
/>
<property
name="maxImpressions"
type="long"
update="true"
insert="true"
access="property"
column="max_impressions"
/>
<property
name="impressions"
type="long"
update="true"
insert="true"
access="property"
column="impressions"
/>
<property
name="live"
type="boolean"
update="true"
insert="true"
access="property"
column="is_live"
/>
<property
name="startDate"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="start_date"
/>
<many-to-one
name="status"
class="com.emirca.touch.module.advert.model.Status"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="status_id"
/>
<many-to-one
name="type"
class="com.emirca.touch.module.advert.model.Type"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="type_id"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Advertisement.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Parameter.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="false">
<class
name="com.emirca.touch.module.advert.model.Parameter"
table="tbl_adv_type_parameter"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Integer"
>
<generator class="native">
</generator>
</id>
<property
name="displayName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="display_name"
/>
<property
name="file"
type="boolean"
update="true"
insert="true"
access="property"
column="is_file"
/>
<many-to-one
name="type"
class="com.emirca.touch.module.advert.model.Type"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="type_id"
/>
<property
name="variableName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="variable_name"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Parameter.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx = null;
try {
Session session = PortalHibernate.currentSession();
tx = session.beginTransaction();
List results = null;
Criteria criteria = session.createCriteria(Advertisement.class);
criteria.add(Expression.ilike("name", form.getName() + "%"));
criteria.addOrder(Order.asc("name"));
results = criteria.list();
tx.commit();
}catch(Exception e){...}
Full stack trace of any exception that occurs:None
Name and version of the database you are using:MySQL Standard 4.0.12
The generated SQL (show_sql=true):Code:
Hibernate: select this.id as id3_, this.name as name3_, this.amount as amount3_, this.currency_id as currency4_3_, this.clicks as clicks3_, this.exp_date as exp_date3_, this.max_clicks as max_clicks3_, this.max_impressions as max_impr8_3_, this.impressions as impressi9_3_, this.is_live as is_live3_, this.start_date as start_date3_, this.status_id as status_id3_, this.type_id as type_id3_, currency1_.id as id0_, currency1_.name as name0_, currency1_.symbol as symbol0_, status2_.id as id1_, status2_.status as status1_, type3_.id as id2_, type3_.html as html2_, type3_.type as type2_, type3_.is_horizontal as is_horiz4_2_, type3_.is_pop_up as is_pop_up2_, type3_.is_vertical as is_verti6_2_ from tbl_advertisement this left outer join tbl_currency currency1_ on this.currency_id=currency1_.id left outer join tbl_adv_status status2_ on this.status_id=status2_.id left outer join tbl_adv_type type3_ on this.type_id=type3_.id where lower(this.name) like ? order by this.name asc
Hibernate: select parameters0_.adv_id as adv_id__, parameters0_.parameter_id as paramete2___, parameters0_.value as value__, parameter1_.id as id0_, parameter1_.display_name as display_2_0_, parameter1_.is_file as is_file0_, parameter1_.type_id as type_id0_, parameter1_.variable_name as variable5_0_, type2_.id as id1_, type2_.html as html1_, type2_.type as type1_, type2_.is_horizontal as is_horiz4_1_, type2_.is_pop_up as is_pop_up1_, type2_.is_vertical as is_verti6_1_ from tbl_adv_parameters parameters0_ left outer join tbl_adv_type_parameter parameter1_ on parameters0_.parameter_id=parameter1_.id left outer join tbl_adv_type type2_ on parameter1_.type_id=type2_.id where parameters0_.adv_id=?
Hibernate: select parameters0_.type_id as type_id__, parameters0_.id as id__, parameters0_.id as id0_, parameters0_.display_name as display_2_0_, parameters0_.is_file as is_file0_, parameters0_.type_id as type_id0_, parameters0_.variable_name as variable5_0_ from tbl_adv_type_parameter parameters0_ where parameters0_.type_id=?
Hibernate: delete from tbl_adv_parameters where adv_id=? and parameter_id=? and value=?
Hibernate: insert into tbl_adv_parameters (adv_id, parameter_id, value) values (?, ?, ?)
Hibernate: insert into tbl_adv_parameters (adv_id, parameter_id, value) values (?, ?, ?)
Debug level Hibernate log excerpt: