Hi,
I have two tables:
Subscriber and Profile
Subscriber is a child of User, persisted in a single table.
Each subscriber has max one profile.
I have mapeed the profile as a foreign key association. However, when I create a new subscriber with an emtpy profile, the profile doesn't get inserted.
On another note, why do I need to have two columns in the Profile table(subscriberID and subscriber) when they point to the same value?
Any help's appreciated. It's driving me nuts that I can't achieve this simple thing.
Kind regards,
Marc
Subscriber.java
/** * @hibernate.one-to-one outer-join="false" unsaved-value="null" * class="nl.msw.dates4free.business.entities.profile.Profile" cascade="all" * @return Returns the profile. */ public Profile getProfile() { return profile; }
Profile.java
/** * @hibernate.id generator-class="foreign" column="SubscriberID" type="long" unsaved-value="null" * @hibernate.generator-param name="property" value="subscriber" * @return Returns the subscriberID. */ public long getSubscriberID() { return subscriberID; } /** * @param subscriberID The subscriberID to set. */ public void setSubscriberID(long subscriberID) { this.subscriberID = subscriberID; } /** * @hibernate.many-to-one class="nl.msw.dates4free.business.entities.Subscriber" unique="true" constrained="true" cascade="all" * @return Returns the subscriber. */ public Subscriber getSubscriber() { return subscriber; }
Testcode
Subscriber user = new Subscriber(); user.setEmail(email); user.setProfile(new Profile()); user.setIdeal(new IdealProfile());
Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); session.save(sub); tx.commit(); HibernateUtil.closeSession();
Subscriber.hbm.xml
<class name="nl.msw.dates4free.business.entities.User" table="Users" discriminator-value="U" >
<id name="userID" column="UserID" type="long" unsaved-value="null" > <generator class="native"> </generator> </id>
<discriminator column="Subclass" type="char" />
<one-to-one name="profile" class="nl.msw.dates4free.business.entities.profile.Profile" cascade="all" outer-join="false" constrained="false" />
...
Profile.hbm.xml
<class name="nl.msw.dates4free.business.entities.profile.Profile" table="Profiles" proxy="nl.msw.dates4free.business.entities.profile.Profile" >
<id name="subscriberID" column="SubscriberID" type="long" unsaved-value="null" > <generator class="foreign"> <param name="property">subscriber</param> </generator> </id>
<many-to-one name="subscriber" class="nl.msw.dates4free.business.entities.Subscriber" cascade="all" outer-join="auto" column="subscriber" unique="true" /> ...
|