-->
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: getting mapping info of Many-To-Many from configuration
PostPosted: Sat Sep 09, 2006 11:32 am 
Newbie

Joined: Sat Sep 09, 2006 9:40 am
Posts: 2
Hibernate version:3.1.3

Hi all

for my project at https://genericrcp.dev.java.net/ I need mapping information from the mapping files. Based on this info a generic gui will be generated. I parse the hbm.xml files manually to get this infos e.g. wich classes, simple attributes or kind of relationsships (otm, mtm ...)

Now I rec ognized that I can use the hibernate Configuration object to get the mapping info and don't have to parse the files by myself.

With this following code I've access to the mapping info especially on relations like ManyToOne and OneToMany.

Configuration cfg = new Configuration();

for (Iterator iter = cfg.getClassMappings(); iter.hasNext();) {
PersistentClass hPc = (PersistentClass) iter.next();
...

for (Iterator iterator = hPc.getPropertyIterator(); iterator.hasNext();) {
Property prop = (Property) iterator.next();


if (prop.getValue() instanceof ManyToOne) {
ManyToOne mto = (ManyToOne) prop.getValue();
}

if (prop.getValue() instanceof org.hibernate.mapping.Set) {
org.hibernate.mapping.Set set = (org.hibernate.mapping.Set) prop.getValue();
if(set.getElement() != null && set.getElement() instanceof OneToMany) {
OneToMany otm = (OneToMany) set.getElement();
}

}
}


My problem now is how do I know when a relation is Many-To-Many and wich Class of objects are in the Collection.

I don't found a class Many-To-Many and set.getElement() returns null when relation is MTM.

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 9:57 am 
Newbie

Joined: Mon Jul 07, 2008 12:31 pm
Posts: 5
Hibernate 3.2.6, Java 1.4.2

More of a bump than a response, as I have a very similar problem. I want to validate the configuration, with one of the criteria being that a collection which maps to a class with a filter attached must also have that filter attached. So I use the iterator from Configuration.getCollectionMappings() with the intention of subsequently looking up the mapping for the collected class, which I find by calling

Code:
Collection.getElement().getType().getReturnedClass()

However, in certain cases Collection.getElement() returns null, these being

a) using a custom UserType to persist a typesafe enum:

Code:
<set name="bars" cascade="all-delete-orphan">
    <key column="fooId" />
    <element column="bar" length="20" not-null="true" type="BarUserType"/>
</set>

and b) having a many-to-many relationship with a persistent entity:

Code:
<set name="bazes" cascade="save-update">
    <key column="fooId" />
    <many-to-many class="Baz" column="bazId" />
</set>

For my requirement, the first case is not an issue as the enum is not a persistent entity which could have a filter, but the second case is problematic. I can see that, in schema terms, here too the association is also not with a persistent entity but with the linking table foo_bars. However I would expect the mapping object to give me a way of getting back the class name given in the many-to-many element, although there doesn’t appear to be anything accessible in the Collection object from which this can be inferred.

Any thoughts appreciated, thanks.


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.