hi,
currently I am working on Hibernate using Eclipse PlugIn.I got a doubt when making
associations.
I am having two tables,one with composite key of two fields and one with a
composite key of three fields.I need to place an associaion betwen them.The schemas are:
parent SCHEMA(only the composite primary key is specified)
-------------
create table LCS_PRODUCT_HDR(
COMP_ID VARCHAR2(50) not null,
PARTID VARCHAR2(50) not null,
.......);
child SCHEMA(only composite primary key specified)
------------
create table LCS_PRODUCT_DTL(
COMP_ID VARCHAR2(50) not null,
PARTID VARCHAR2(50) not null,
COUNTRY_ID VARCHAR2(50) not null,
.........);
so I have configured the corresponding HBM's as follows:
inside parent hbm
------------------
<hibernate-mapping package="persistence">
<class
name="LcsProductHdr"
table="LCS_PRODUCT_HDR"
>
<composite-id name="Id" class="LcsProductHdrPK">
<key-property
name="CompId"
column="COMP_ID"
type="string"
/>
<key-property
name="Partid"
column="PARTID"
type="string"
/>
</composite-id>
<!-- one to many association -->
<set name="productdets" lazy="true" inverse="true"
cascade="all-delete-orphan" >
<key>
<column name="COMP_ID" />
<column name="PARTID" />
</key>
<one-to-many class="LcsProductDtl" />
</set>
<property ...
..
</class>
</hibernate-mapping>
(inside) child hbm
------------------
<hibernate-mapping package="persistence">
<class
name="LcsProductDtl"
table="LCS_PRODUCT_DTL"
>
<composite-id name="Id" class="LcsProductDtlPK">
<key-property
name="CompId"
column="COMP_ID"
type="string"
/>
<key-property
name="Partid"
column="PARTID"
type="string"
/>
<key-property
name="CountryId"
column="COUNTRY_ID"
type="string"
/>
</composite-id>
<!-- one to many association -->
<many-to-one name="producthdr" class="LcsProductHdr" insert="false" update="false">
<column name="CompId" />
<column name="PARTID" />
</many-to-one>
<property ...
..
</class>
</hibernate-mapping>
after synchronizing these HBM's I got the following additional code in POJO's:
(inside)POJO of parent
----------------------
public java.util.Set getProductdets () {
return productdets;
}
public void setProductdets (java.util.Set productdets) {
this.productdets = productdets;
}
public void addToproductdets (LcsProductDtl
lcsProductDtl) {
if (null == getProductdets()) setProductdets(new java.util.HashSet());
getProductdets().add(lcsProductDtl);
}
public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof LcsProductHdr))
return false;
else {
LcsProductHdr lcsProductHdr =
(LcsProductHdr) obj;
if (null == this.getId() || null == lcsProductHdr.getId()) return false;
else
return (this.getId().equals(lcsProductHdr.getId()));
}
}
(inside)POJO of child
----------------------
public LcsProductHdr getProducthdr () {
return producthdr;
}
public void setProducthdr (LcsProductHdr
producthdr){
this.producthdr = producthdr;
}
public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof LcsProductDtl))
return false;
else {
LcsProductDtl lcsProductDtl =
(LcsProductDtl) obj;
if (null == this.getId() || null == lcsProductDtl.getId()) return
false;
else return (this.getId().equals(lcsProductDtl.getId()));
}
}
public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getId()) return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":" +
this.getId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}
public String toString () {
return super.toString();
}
Then in the DAO i have written the following code and its working fine but my doubt is that
unless I am making an explicit transaction commit() , the data in the session is not synchronizing with
the database even I am flushing the data using "sess.flush()"(indicated by the marker "-->").
First I've tried without using transactions and the data is existing in the
session but it is not being persisted in the database.So again with the same code I have
done transaction commit and I am able to see the data in the database.
-->So then what is the need of flush().
-->and commit() with out flush() is also not working with
one-to-many associations as with flush() without commit().
-->unless I do both as shown in the code with "-->" markers the data is
not being persisted in the database
-->is there any better solution than this
-->before flush sess.isDirty() returning true and after flush it is
returning false as u can see in the WEBLOGIC console(given below).
inside the DAO
--------------
Transaction t = null;
Session sess = null;
try {
sess = lphdao.getSession();
//s = getSession();
t = sess.beginTransaction();
//-----------------------------old
sess.save(lph);
try{
lpt.setProducthdr(lph);
Set s=lph.getProductdets();
if(s==null)
s=new HashSet();
s.add(lpt);
lph.setProductdets(s);
}catch(NullPointerException e)
{
e.printStackTrace();
}
sess.save(lpt);
System.out.println(sess.isDirty());
[look here]--> sess.flush();
System.out.println(sess.isDirty());
[look here]--> t.commit();
}
catch (HibernateException e) {
if (null != t) t.rollback();
throw e;
}
finally {
sess.close();
}
}catch (HibernateException e) {
e.printStackTrace();
}
inside the WEBLOGIC console(after sess.flush() statement two inserts r
-----------------------------------
happening so i guess there is nothing wrong with my code what i have written)
true
Hibernate: insert into LCS_PRODUCT_HDR (PRODDESC, SELLING_UNIT, PRICE, COO, PRODUCT_TYPE,
PRODUCT_SUB_TYPE, LENGTH, WIDTH, HEIGHT, DIM_UNIT, CH_WEIGHT, CURRENCY, ACT_WEIGHT,
DIM_WEIGHT, PRODUCT_BUNDLE, CH_WEIGHT_UNIT, COMP_ID, PARTID) values (?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into LCS_PRODUCT_DTL (HTS_CHAPTER_NO, ECN_NO, UOM1, UOM2, UOM3, UOM1_CONV,
UOM2_CONV, UOM3_CONV, HTS_DESC, NETW, PRICE, LENGTH, WIDTH, HEIGHT, CURRENCY,
PRODUCT_BUNDLE, COMP_ID, PARTID, COUNTRY_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?)
false[/code]
_________________ hi,
I am from b'lore working for "NEXTLINX INDIA LTD".
|