Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: detect unique members in a mapped class
PostPosted: Thu Dec 11, 2008 7:40 am 
Newbie

Joined: Thu Jun 19, 2008 4:49 am
Posts: 12
Is it possible to retrieve all members of a class that are mapped with a unique constraint?

e.g.:
with an annotation i can define the column to be unique in the database:
@Column(name = "BraToken", length = 10, nullable = false, unique = true)

With an EntityPersister i can get the EntityMetamodel which has a method "getProperties()" returning objects of type "StandardProperty". I can ask the StandardProperty if it's nullable. What about unique?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 8:25 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
I spent a little time looking into this myself as I am looking for information about foreign keys (practically the same problem)... I attacked this from the Configuration side and couldn't find anything either. There is an object "Constraint" from the API and the only references I found to it are UniqueKey, ForeignKey, and PrimaryKey. From what I can tell, these appear to be used for generating the schema generation output, but do not seem to be kept as part of the model data.

I only spent a few minutes looking through this, so you may want to continue my approach by following all the references to UniqueKey... perhaps that will lead to some information about how that data is maintained. Please post if you find anything!

_________________
-----------------------
Paul
Software Engineer
Lockheed Martin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 9:27 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Ok... couldn't stop myself from playing some more. Here is one way to get at unique column information. I don't know if this is the best way or not, so if anyone knows of something better, please post.

Code:
Configuration config = session.getConfiguration();
PersistentClass pc = config.getClassMapping(CLASS_CANONICAL_NAME);
Table table = pc.getTable();

Iterator<Column> columns = (Iterator<Column>) table.getColumnIterator();
while (columns.hasNext())
{
        Column column = columns.next();
        System.err.println(column.getName() + ": unique --> "
            + column.isUnique());
}


For my project, we store this kind of information in our own objects for extremely easy and fast lookup. I would recommend you do something like that as well... you can load things on the fly or a start time...

One other thing... i tried getting the unique keys from the table directly, but for some reason i was not getting any results. Here is the code if you want to play around with it to see what I was missing:
Code:
Iterator<UniqueKey> uKeys = (Iterator<UniqueKey>) table
          .getUniqueKeyIterator();
while (uKeys.hasNext())
{
        UniqueKey uKey = uKeys.next();
        System.err.println(uKey.getName() + ", "
            + uKey.getClass().getCanonicalName());
}


Good luck.

_________________
-----------------------
Paul
Software Engineer
Lockheed Martin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 9:45 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Ok.. I think I know why I get nothing from the UniqueKey iterator.

When iterating the Columns, isUnique tells you if that column (and that column alone) must be unique. A UniqueKey is generated only when you have a key with multiple columns. So, you will probably want to do both depending on your needs here.

This might also tell you what you want to know (on a prop per prop basis, doesn't tell you about complex keys):
Code:
     
Iterator<Property> props = (Iterator<Property>) pc.getPropertyIterator();
while (props.hasNext())
{
        Property prop = props.next();
        System.err.println(prop.getName() + ": isOptional --> "
            + prop.isOptional());
}


I hope all this helps.

_________________
-----------------------
Paul
Software Engineer
Lockheed Martin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 8:34 am 
Newbie

Joined: Thu Jun 19, 2008 4:49 am
Posts: 12
thanks a lot


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.