-->
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: properties tag constituents - runtime inference, how?
PostPosted: Fri Sep 23, 2005 12:53 pm 
Newbie

Joined: Tue Jan 11, 2005 10:52 pm
Posts: 8
Hi all,

Is there a way to infer at runtime the property constituents of a properties tag in a mapping file?

In other words, what are the unique fields of a given domain object asked at runtime?

Thanks, Jon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 4:46 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
Normally this is what you do to create your session factory:

Code:
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration( );
cfg.configure( ); // Looks for hibernate.cfg.xml in classpath
org.hibernate.SessionFactory factory = cfg.buildSessionFactory( );


Now, both the SessionFactory and the Configuration object hold information about your mappings. The information in SessionFactory is immutable and really hard to get to, but it is always accurate. The information in the Configuration object is mutable, and as such should not be trusted. However, the Configuration object is probably your best bet:

Example using the SessionFactory object:

Code:
org.hibernate.metadata.ClassMetaData cmd = factory.getClassMetadata( SomeDomainObject.class );
String idName = cmd.getIdentifierPropertyName( );
org.hibernate.type.Type idType = cmd.getIdentifierType( );


I don't think that's what you want, especially if you have composite keys. Here is an equivalent example using the Configuration object:

Code:
org.hibernate.mapping.PersistentClass pc = cfg.getClassMapping( SomeDomainObject.class.getName( ) );
org.hibernate.mapping.Property id = pc.getIdentifierProperty( );
String idPropertyName = id.getName( );
java.util.Iterator idColIter = id.getColumnIterator( );
while( idColIter.hasNext( ) )
{
   org.hibernate.mapping.Column idCol = (Column)idColIter.next( );
   String name = idCol.getName( );
   ...
}

java.util.Iterator subclassIter = pc.getSubclassIterator( );
while( subclassIter.hasNext( ) )
{
    org.hibernate.mapping.Subclass subclass = (Subclass)subclassIter.next( );
    org.hibernate.mapping.KeyValue key = subclass.getKey( );
    java.util.Iterator keyColIter = key.getColumnIterator( );
    while( keyColIter.hasNext( ) )
    {
        org.hibernate.mapping.Column keyCol = (Column)keyColIter.next( );
        String keyColName = keyCol.getName( );
        ...
    }
}


And so on. The API documentation is really helpful:

http://www.hibernate.org/hib_docs/v3/api/

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 5:52 pm 
Newbie

Joined: Tue Jan 11, 2005 10:52 pm
Posts: 8
Jesse,

Most helpful. Thank you. My approach at this point is to interrogate the Configuration like you said and inspect all PersistentClasses. For each persistent class, I need to determine the "biz key". In each of my hbm class mapping files I have a business key ("biz key") definition like:

Code:
<!-- biz-key -->
<properties name="biz-key" unique="true">
  <property name="emailAddress" column="email_address" type="string" length="128" not-null="true"/>
</properties>


so i need to get at the biz-key properties tag and its constituents. Problem is, I only see a Property class in org.hibernate.mapping and no class that would corres. to the 'properties' tag.

Thanks again, Jon[/code]


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.