-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: ternary association mapped by a map (like book page 347)
PostPosted: Sat Dec 08, 2007 4:32 pm 
Newbie

Joined: Mon Apr 11, 2005 10:51 am
Posts: 7
Hi,

I have a unidirectional ternary association like page 347 of the book between
Person (Category in the book), Category (Item in the book) and Career (User in the book).
The difference is that the person_id and the category_id are not nullable but the Career_id can be null in the association table.
When I try to save a Person with a Category in the key of the map but null in the value.
No insert occurs in the PERSON_CATEGORY table.

Is there a way of "telling" hibernate in the mapping to insert a line in PERSON_CATEGORY if the career is null ?

thanks

Paskos

Here are my details.

Hibernate version:3.2.2 ga

Mapping documents:
Code:
<class name="Person" table="PERSON">
  <id name="id" type="java.lang.Integer">
   <generator class="identity"/>
  </id>
  <property name="name" type="string"/>
 
  <map name="careerPath" table="PERSON_CATEGORY" cascade="all-delete-orphan">
   <key column="PERSON_ID"/>
   <map-key-many-to-many column="CATEGORY_ID" class="Category"/>
   <many-to-many column="CAREER_ID" class="Career"/>
  </map>
</class>



Code between sessionFactory.openSession() and session.close():
Code:
Person person = new Person();
person.setName("toto");
person.getCareerPath().put(newHire,null);
getSessionFactory().getCurrentSession().save(person);



Name and version of the database you are using: hsqldb v 1.8

The generated SQL (show_sql=true):
Code:
insert into person (id, name) values (?,?);



Top
 Profile  
 
 Post subject: I remember having the same issue some time ago
PostPosted: Mon Dec 10, 2007 10:44 am 
Newbie

Joined: Fri Dec 07, 2007 5:03 pm
Posts: 1
Paskos,
I remember having the same issue some time ago in my project. I had a ternary association that had one entity of the association nullable.

The mapping was looking like:

<map name="profiles" table="USERPROFILENICKNAME" cascade="all">
<key>
<column name="USER_ID" precision="10" scale="0" not-null="true"/>
</key>

<map-key-many-to-many entity-name="Nickname">
<column name="NICKNAME_ID" not-null="true"/>
</map-key-many-to-many>
<many-to-many entity-name="Profile" column="PROFILE_ID"/>
</map>

I could not save the record when the nickname part of the relation was null. I wonder if this is a limitation of hibernate, maybe an expert could provide some advises.

Best regards.


Top
 Profile  
 
 Post subject: CAREER_ID nullable
PostPosted: Mon Dec 10, 2007 10:57 am 
Newbie

Joined: Mon Apr 11, 2005 10:51 am
Posts: 7
I was able to specify that the CAREER_ID could be null

Code:
<many-to-many entity-name="Career">
  <column name="CAREER_ID" not-null="false"/>
</many-to-many>

But
Code:
person.getCareerPath().put(newHire,null);
personDAO.save(person);

still doesn't insert anything. And even worse
Code:
getSessionFactory().getCurrentSession().createSQLQuery("INSERT INTO PERSON_CATEGORY (PERSON_ID, CATEGORY_ID, CAREER_ID) values (?,?,null)");
sqlQuery.setInteger(0, id2);
sqlQuery.setInteger(1, capitainCategory.getId());
sqlQuery.executeUpdate();

works perfectly well but when I try to read the person with id2 the null Career doesn't appear.
Code:
Person person3 = personDAO.read(id2);
careerHistory = person3.getCareerHistory();
assertTrue(CollectionUtil.isNotEmpty(careerHistory));

Here my unit test fails because my Map is empty. And it shouldn't because the "manual" insert worked well.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 11:45 am 
Newbie

Joined: Mon Apr 11, 2005 10:51 am
Posts: 7
What I would like to achieve in Person is a Map<Category,Career> where sometimes for a Category the Career object is null. It's a business requirement.
When a Career object is null the following SQL is executed :
Quote:
INSERT INTO PERSON_CATEGORY (PERSON_ID, CATEGORY_ID, CAREER_ID) values (?,?,null)

When a Career object is not null
Quote:
INSERT INTO PERSON_CATEGORY (PERSON_ID, CATEGORY_ID, CAREER_ID) values (?,?,?)

And when I call Map<Category,Career> Person.getCareerPath() method I get the Map with category objects as key but sometimes Career instances sometimes null.

Is it possible to achieve this in Hibernate ?
Any expert out there ?


Top
 Profile  
 
 Post subject: [DELETED] n/m
PostPosted: Thu Sep 18, 2008 11:15 am 
Newbie

Joined: Tue Sep 16, 2008 3:52 pm
Posts: 4
Location: Austin
Oops replied to the wrong message.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.