-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to define unique constraints with Annotations ?
PostPosted: Wed Feb 15, 2006 10:31 am 
Newbie

Joined: Wed Feb 15, 2006 10:17 am
Posts: 13
Location: Rome, Italy
Hi,
I am pretty new to Hibernate, I tried the tutorial and looked for documentation, and I have to ask something related on how to define in the OO model the equivalent concept of unique contraints in the relational model, so that I don't risk to have duplicate insertion for newly instanted objects that are "equal" on some fields.

So:

1) I am using Annotations to define the mapping, and I would like to define the equivalent relational unique constraint on N properties of one of my given domain objects, since the key is the surrogate Long id field. Can someone give me a hint on how to do that or point me some reference documentation? I only found how to do that with the mapping in xml.

2) Assuming I did all at point (1), is that just enough to prevent me from inserting duplicate "equivalent" objects into the database or I have also to ovveride equals() (and hashCode() ) to follow the natural key ? (I think it would be reasonable to do so)

Thanks a lot,
Alessio Pace.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 1:25 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
1) You can define a unique constraint with @Column( unique=true )... There are also constraint annotations (Annotations Reference Guide Chapter 3) but there are none that annotate 'Unique'.

2) From what I understand, Hibernate does not use hashCode and equals when inserting records in a database ( see http://www.hibernate.org/109.html ). Hibernate looks at what you have listed as an id and checks if that is already in the table, if not then it will add it.

If you have a composite primary key set or if you make a column unique in your table schema then Hibernate will throw an Exception and not insert the record if it is violated. You can catch this Exception and deal with it however you would like.

-B
___________________________________
Please rate me if you found my post useful.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 1:33 pm 
Newbie

Joined: Wed Feb 15, 2006 10:17 am
Posts: 13
Location: Rome, Italy
I found a solution for my multi-column unique constraint like this:

Code:
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"aProperty","aForeignKey"})})
public class Foo implements Serializable{
    private String aProperty;
    ....
}



So that I can declare the multi-column uinque constraint all together, and not per-column.

Thanks for your help,
Alessio Pace


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