+1  A: 

It's not that simple, unfortunatelly. Your best bet to detect this is by checking for particular CollectionPersister implementation:

SessionFactory sf = ...;

// Get the class' metadata
ClassMetadata cmd = sf.getClassMetadata(o.getClass());

for(String propertyName:cmd.getPropertyNames()) {
  Type propertyType = cmd.getPropertyType(propertyName);
  if (propertyType.isCollectionType()) {
    CollectionType collType = (CollectionType) propertyType;

    // obtain collection persister
    CollectionPersister persister = ((SessionFactoryImplementor) sf)
      .getCollectionPersister(collType.getRole());

    if (persister instanceof OneToManyPersister) {
      // this is one-to-many
    } else {
     // this is many-to-many OR collection of elements
    }
  } // if
} // for
ChssPly76
PERFECT!!! Bonus question: How to determine @ManyToOne over @OneToOne?
User1
Oh wait. I think I found it. EntityType.isOneToOne(). However, I couldn't have found this without you. I really appreciate all your help! Your entries help me and hopefully others. Good work!
User1
I think I spoke too soon. EntityType.isOneToOne() is always false for some reason. I didn't find a OneToOnePersister. Any ideas?
User1
`EntityType.isOneToOne()` is the way to go. Or you can check for property type to be `instanceof OneToOneType` which is the same thing. You'll NEVER have one-to-one as part of collection, though - you're checking some other property, right?
ChssPly76
They are first checked with isEntityType(). Then cast to EntityType. Always false. It almost looks it from a site that (probably) shows the source code: http://www.docjar.com/docs/api/org/hibernate/type/ManyToOneType.html. Any ideas?
User1
Also, the debugger shows the @OneToOne entity as a ManyToOne. They must have reused the same class for both @OneToOne and @ManyToOne. Do you see this as well?
User1
Sorry, I didn't quite get your response. Are you saying that you're testing a property that **is** mapped as one-to-one (and thus is not part of collection) but isOneToOne() returns false? If that's the case what concrete implementation of EntityType is returned?
ChssPly76
I'm going to make a new issue http://stackoverflow.com/questions/1381862/onetoone-getting-returned-as-manytoonetype. The comments don't have enough room. Thanks again for your help.
User1

related questions