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.  [ 4 posts ] 
Author Message
 Post subject: Uniqueness in association table of many-to-many association
PostPosted: Tue Sep 12, 2006 6:03 am 
Newbie

Joined: Tue Sep 12, 2006 5:16 am
Posts: 5
Location: Ukraine
Hello all. Consider two entities: User and PhotoPushpin. Each user can create pushpin, but also user can have favorite photo pushpins. We have many-to-many association between User and PhotoPushpin (in favoritePhotoPushpins collection). It is unidirectional.

The question is: how to prevent a user to add the same photo pushpin to favorite twice? It means: how to make the composite key of both fields in association table to prevent two records to be absolutely equal? Or another way: how to add a key field to association table? Or maybe there is another way to solve this?

I've found something if to use annotations:

Code:
@Table(name="tbl_sky", uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})})


But I can't implement it because I use XML. I believe that there is the way to do it by XML. But how?

Hibernate version: 3.1

Mapping documents:

Association part of user.hbm.xml:

Code:
<bag name="favoritePhotoPushpins" table="favorite_pushpins" inverse="false" lazy="true">
            <key column="USER_ID" />
            <many-to-many class="com.shiftlabs.imagestroll.model.PhotoPushpin" column="PUSHPIN_ID" />
</bag>


Name and version of the database you are using: MySql 4.0.26-nt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 14, 2006 2:41 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:44 am
Posts: 27
use <set> in place of <bag> and hibernate create a double primary key for you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 14, 2006 6:21 am 
Newbie

Joined: Tue Sep 12, 2006 5:16 am
Posts: 5
Location: Ukraine
As for <set> I know it can prevent repetition. But can it be implemented with list? I consiter lists more preferable than sets. Or it was the only way?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 14, 2006 7:07 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:44 am
Posts: 27
if you generate the database with hibernate, you can use :
Code:
<hibernate-mapping>
   <class ...>
   </class>
   <database-object>
      <create>
         ALTER TABLE ... ADD CONSTRAINT...
      </create>
      <drop>
         ALTER TABLE ... DROP CONSTRAINT...
      </drop>
    </database-object>
</hibernate-mapping>


or direct add the constraint to the database self...

But I find this dangerous, you have to impose the unicity in your business (and use sets) and not let your database manage it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.