-->
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.  [ 2 posts ] 
Author Message
 Post subject: Trouble using unique constraint to create enumerable types?
PostPosted: Mon Jun 19, 2006 5:29 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I have an object that essentially is like an enumerable type called Namespace. It's simply an ID and a namespace. I want to be able to something like:

Document doc = new Document( new Namespace( 'somenamespace' ) );

Now when I commit the Document object to the database it will create an new entry if and only if the Namespace.namespace property is unique. Otherwise it will just map it to the existing Namespace object saved in the database.

I have mapped the Namespace object like

public class SchemaNamespace {

@Id @GeneratedValue
@Column( name= "NamespaceID" )
private Integer id;

@Column( name = "Namespace", unique = true )
private String namespace;
}

I've set the unique constraint to true thinking that would cause hibernate to select on the table before inserting new Namespace objects. But, if do the code like I've typed above it will create a new namespace object assigning it a different ID.

How do I make hibernate enforce the uniquness of Namespace.namespace so it doesn't insert duplicate entires?

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:07 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That will required a custom ID generator, that goes to the DB to deetermine uniqueness. Start with the javadocs for the IdentifierGenerator interface, that should point you in the right direction.

_________________
Code tags are your friend. Know them and use them.


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