My problem is simple, and most likely right before my eyes. But being new to Hibernate, I am unable to figure out the issue. I have 2 POJO's FilerBean and FilerShareBean. There is a one to many relationship between the FileBean and the FilerShareBean. I am attempting to use a set collection to represent this via Hibernate. Below you will see the code and mappings. The problem is when I save the FilerBean, which contains 3 FilerShareBeans in the set, to the database, the rows in the FilerShare table all have <null> FILER_ID column.
Filer Table in the database after the save.
FILER_ID: 1
NAME: alabama
DESCIPTION: Enter description here
VENDOR: IBM N-Series
FilerShare Table in the database
SHARE_ID: 1
FILER_ID: <null>
NAME: share_2
PATH: /lhome/dir_2
PROTOCOL: 0
If someone would be kind enough to take a look and let me know why this is happening, I would be greatful.
Thanks
kshenes
[b]Hibernate version:[/b] version 3.2.2, January 24, 2007
[b]Mapping documents:[/b]
<hibernate-mapping>
<class name="FilerBean" table="Filer">
<!-- the unique identifier for the Filer -->
<id name="id" column="FILER_ID">
<generator class="native"/>
</id>
<!-- name for the Filer -->
<property name="name">
<column name="NAME" sql-type="varchar(64)" not-null="true" unique="true"/>
</property>
<!-- name for the Filer -->
<property name="description">
<column name="DESCRIPTION" sql-type="varchar(128)" not-null="true"/>
</property>
<!-- name for the Filer -->
<property name="vendor">
<column name="VENDOR" sql-type="varchar(64)" not-null="true"/>
</property>
<set name="filerShares" inverse="true" cascade="all-delete-orphan">
<key column="FILER_ID"/>
<one-to-many class="FilerShareBean"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="FilerShareBean" table="FilerShares">
<!-- the unique identifier for the Filer -->
<id name="id" column="SHARE_ID">
<generator class="native"/>
</id>
<many-to-one name="filer"
class="FilerBean"
column="FILER_ID"/>
<!-- name for the Share -->
<property name="name">
<column name="NAME" sql-type="varchar(64)" not-null="true"/>
</property>
<!-- path for the Share -->
<property name="path">
<column name="PATH" sql-type="varchar(128)" not-null="true"/>
</property>
<!-- protocol for the Share -->
<property name="protocol">
<column name="PROTOCOL" sql-type="integer" not-null="true"/>
</property>
</class>
</hibernate-mapping>
[b]Code between sessionFactory.openSession() and session.close():[/b]
HashSet shares = new HashSet();
for (int i = 0; i < 3; i++)
{
FilerShareBean share = new FilerShareBean("share_" + i, FilerShareBean.PROTO_CIFS, "/lhome/dir_" + i);
shares.add(share);
}
filer_.setFilerShares(shares);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Long filerId = (Long) session.save(filer_);
tx.commit();
session.close();
[b]Full stack trace of any exception that occurs:[/b]
No exception is thrown
[b]Name and version of the database you are using:[/b] HSQLDB 1.8.0.7
[b]The generated SQL (show_sql=true):[/b]
Hibernate:
insert
into
Filer
(FILER_ID, NAME, DESCRIPTION, VENDOR)
values
(null, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
select
filerbean0_.FILER_ID as FILER1_0_,
filerbean0_.NAME as NAME0_,
filerbean0_.DESCRIPTION as DESCRIPT3_0_,
filerbean0_.VENDOR as VENDOR0_
from
Filer filerbean0_
1 Filers found.
Hibernate:
select
filershare0_.FILER_ID as FILER2_1_,
filershare0_.SHARE_ID as SHARE1_1_,
filershare0_.SHARE_ID as SHARE1_1_0_,
filershare0_.FILER_ID as FILER2_1_0_,
filershare0_.NAME as NAME1_0_,
filershare0_.PATH as PATH1_0_,
filershare0_.PROTOCOL as PROTOCOL1_0_
from
FilerShares filershare0_
where
filershare0_.FILER_ID=?
|