Hi all!
its possible to map a one-to-one relation and the same time mapping a one-to-many to the same entity? Or its a concetual error?
for example:
I have follow diagram:
each 'MS' (Message) must have one 'SE' (Recipient). thats the 'fr' (from) relation.
in the same time I have tree other relations (1-N) from 'MS' to 'SE'. (to, cc and bcc relations)
my Message (MS) mapping:
Code:
<class name="com.xyz.test.dorm.bean.Message">
<id column="id_message" name="id">
<generator class="increment"/>
</id>
<property name="sent"/>
<property name="lastRetry"/>
<property name="body"/>
<property name="subject"/>
<property name="retryCycle"/>
<property name="retryThreshold"/>
<property name="contentType"/>
<property name="created"/>
<one-to-one cascade="all-delete-orphan"
class="com.xyz.test.dorm.bean.Recipient" name="from"/>
<set cascade="all-delete-orphan" inverse="true" name="cc">
<key column="ref_id_message"/>
<one-to-many class="com.xyz.test.dorm.bean.Recipient"/>
</set>
<set cascade="all-delete-orphan" inverse="true" name="bcc">
<key column="ref_id_message"/>
<one-to-many class="com.xyz.test.dorm.bean.Recipient"/>
</set>
<set cascade="all-delete-orphan" inverse="true" name="to">
<key column="ref_id_message"/>
<one-to-many class="com.xyz.test.dorm.bean.Recipient"/>
</set>
</class>
and my Recipient.hbm.xml (SE)
Code:
<class name="com.xyz.test.dorm.bean.Recipient">
<id column="id_message" name="id">
<generator class="foreign">
<param name="property">message</param>
</generator>
</id>
<property name="email"/>
<property name="mediaType"/>
<property name="name"/>
<many-to-one cascade="save-update"
class="com.zyx.test.dorm.bean.Message"
column="ref_id_message" name="message"/>
</class>
1st situation of test:
Code:
Message message = new Message();
Recipient recFrom = new Recipient();
recFrom.setName("Recipient FROM");
recFrom.setEmail("recipient.from@test.com");
recFrom.setMediaType(new Integer(0));
recFrom.setMessage(message);
Recipient recTo1 = new Recipient();
recTo1.setName("To1");
recTo1.setEmail("to1@test.com");
recTo1.setMediaType(new Integer(0));
recTo1.setMessage(message);
Recipient recCc1 = new Recipient();
recCc1.setName("Cc1");
recCc1.setEmail("cc1@teste.com");
recCc1.setMediaType(new Integer(0));
recCc1.setMessage(message);
Recipient recBcc1 = new Recipient();
recBcc1.setName("Bcc1");
recBcc1.setEmail("bcc1@test.com");
recBcc1.setMediaType(new Integer(0));
recBcc1.setMessage(message);
message.setFrom(recFrom);
message.addTo(recTo1);
message.addCc(recCc1);
message.addBcc(recBcc2);
and the error:
Code:
java.lang.Exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xyz.test.dorm.bean.Recipient#7]
Ok. the cause (I think) its because all the Recipients relates to the same Message, and the 1-1 relation already done.
I change my class test.. I only set the setMessage for 1-1 relation.
follow my other test:
Code:
Message message = new Message();
Recipient recFrom = new Recipient();
recFrom.setName("Recipient FROM");
recFrom.setEmail("recipient.from@test.com");
recFrom.setMediaType(new Integer(0));
recFrom.setMessage(message);
Recipient recTo1 = new Recipient();
recTo1.setName("To1");
recTo1.setEmail("to1@test.com");
recTo1.setMediaType(new Integer(0));
Recipient recCc1 = new Recipient();
recCc1.setName("Cc1");
recCc1.setEmail("cc1@teste.com");
recCc1.setMediaType(new Integer(0));
Recipient recBcc1 = new Recipient();
recBcc1.setName("Bcc1");
recBcc1.setEmail("bcc1@test.com");
recBcc1.setMediaType(new Integer(0));
message.setFrom(recFrom);
message.addTo(recTo1);
message.addCc(recCc1);
message.addBcc(recBcc2);
only recFrom do setMessage...
but now, the new error is:
Code:
java.lang.Exception: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: message
I think because each Recipient must have a reference of Message because 1-1 relation... but I cant do it because ocour the other error..
finally, is there a way to map this model (one-to-one and one-to-many to the same entity)?
thanks so much!
PS: sorry my english