-->
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.  [ 8 posts ] 
Author Message
 Post subject: Accessing mapping data
PostPosted: Thu Jun 15, 2006 9:06 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Hello,

I'm trying to get a few things out of the hibernate configuration and can't seem to figure out the mapping. When given an entity's class name, I'd like to retrieve the following things:

- All mapped property names (so I can call setXX and getXX).
- All mapped property types (java.lang.Long, java.lang.String, etc.)
- The primary key property.
- Many-to-one, many-to-many and one-to-many relationships, if any.

Is there any documentation/overview besides the javadoc?

Thanks,
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 10:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The javadocs and this forum are all I'm aware of.

To get that info given a Configuration object, use cfg.getClassMapping(class).getPropertyIterator(). The Property class provides getType() and getName(). getIdentifierProperty() will return the key's property. Collections and associations are also returned from getPropertyIterator(), you just have to check the return from getType() (it'll be a CollectionType for a collection, or EntityType for an association).

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 1:04 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Excellent, that should just about do it. I was going through the Table instead. Boy what I wouldn't give for a Java 5 implementation of hibernate so it could take advantage of generics :).

Thanks again.


Top
 Profile  
 
 Post subject: One quick question...
PostPosted: Fri Jun 16, 2006 1:19 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Can I determine whether or not the property is the ID with isNaturalIdentifier()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 1:52 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, that tells you whether or not it has the natural-id attrbiute set. All properties with isNaturalIdentifier = true are IDs, but not all IDs have isNaturalIdentifier = true. Afaik, you have to get the IdentifierProperty, then compare each property that you iterate over (getPropertyIterator) against that one.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 11:34 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
I found that getPropertyIterator doesn't return the id property, which is certainly fine with me :).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 11:49 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Can I differentiate between many-to-many, many-to-one, one-to-many, one-to-one, that kind of thing? AssociationType will give me the foreign key direction (parent->child/other way), but that seems more towards cascade rather than telling me which type of join is applied to the property.


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Fri Jun 16, 2006 9:21 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
I've been wreaking havoc on the Struts mailing list with a related issue, but I believe I have everything sorted out. Never the one to leave a solved issue without a published solution (I HATE finding replies that say, "I solved it!" with no further explanation!), here's what I've come up with:

Prerequisites: Class entityClass, Configuration configuration, custom Iterable converter for an iterator (I heart Java 5), SessionFactory sessionFactory

Code:
PersistentClass entityMapping = configuration.getClassMapping( entityClass );
Property idProperty = entityMapping.getIdentifierProperty();

for ( Property property : getProperties( entityMapping ) )
{
  Type type = property.getType();
  if ( type.isEntityType() )
  {
    if ( ( (EntityType)type ).isOneToOne() )
    {
      // one-to-one
    }
    else
    {
      // many-to-one
    }
  }
  else if ( type.isCollectionType() )
  {
    PersistentClass associatedClass = configuration.getClassMapping( ( (AssociationType)type ).getAssociatedEntityName( (SessionFactoryImplementor)sessionFactory ) );
    try
    {
      associatedClass.getProperty( entityClass.getName() );
      // one-to-many
    }
    catch ( MappingException e )
    {
      try
      {
        associatedClass.getProperty( pluralize( entityName ) );
        // many-to-many
      }
      catch ( MappingException e )
      {
        // Double-U Tee Ef?
        throw something;
      }
    }
  }
  else
  {
    // basic property
  }
   
}


Couple of points:

1) This is a highly abbreviated version of my copy, I tried to keep it as simple as I could. It contains errors, because I typed it by reference, didn't test it, and assume people interested in my problem can figure things out well enough from what I've written.

2) This is not as generic as it could be--it relies on my own design habits towards entities (hence the "pluralize" thing).

3) IMHO (to the Hibernate developers): it might be nice to get rid of the getXXXIterator() methods, and return the full map/list/set instead. Worried about modifications? Use Collections.unmodifiableXXX(). I've done a few benchmarks on the differences myself, and found none. I know this is anecdotal, but certain situations make the whole iterator vs. collection thing a little cumbersome to code around.

4) I don't particularly like the lines reading "associatedClass.getProperty(...)" surrounded by try/catch. The first situation is an expected one (many-to-many instead of one-to-many) and shouldn't need an exception handler that gets hit on an expected condition. It's likely a combination of my method and my lack of knowledge on how the configuration-related classes work, but I thought I'd point it out anyways.

5) I'm a little confused about having to cast a SessionFactory to a SessionFactoryImplementor. Is this a no-no? (Bonus: Why does the getAssociatedEntityName need it?)

6) I really, really hope I can cache the Getter and Setter objects returned by property.getGetter() and property.getSetter().

7) The "deprecated" JavaDoc (or better yet, the Deprecated annotation) is a much, much, much better IDE-friendly alternative to "// todo : remove" for prospective developers (See org.hibernate.mapping.Property). I wasted a few hours trying to figure out what was going on with the getMethod() accessor of (S|G)etter before I tried (g|s)et() instead.

8) A textarea control is a really bad place to type code.

9) Apparently, "couple" doesn't always mean 2.

10) (The most important point): Thanks tenwit! :)

- sil

BTW: The ordered list tag doesn't seem to work... and phpbb.com isn't responding (?).


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