-->
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: How to check hibernate mappings using reflection?
PostPosted: Mon Feb 27, 2012 2:20 pm 
Newbie

Joined: Mon Feb 27, 2012 12:50 pm
Posts: 2
So, I realize this is out of the normal use case of Hibernate, but...

I am using java reflection to populate DTO classes that persist via Hibernate with annotation config.
I need to reflexively find the @ManyToOne relation mappedBy attribute in order to set the back link from one to many.

Code:

class Router{
@OneToMany(targetEntity = Port.class, cascade = CascadeType.ALL, mapped = "router", fetch = FetchType.LAZY)
Collection<Port> ports;
}

class Port{
@JsonBackReferene
@ManyToOne(cascade = {CascadeType.ALL, fetch = FetchType.LAZY)
Router router;
}

Reflexively (and recursively), I populate the DTOs starting from the Router. When the port has all of its attributes set (auto-negotiate, etc), it is returned to the Router to be added to the portList. The Port.router field is unset because I can't find a way to reflexively get the @OneToMany.mappedBy value, which would let me know which field in Port to set to the Router instance.

Any advice is much appreciated!!!

Thanks!


Top
 Profile  
 
 Post subject: Re: How to check hibernate mappings using reflection?
PostPosted: Tue Feb 28, 2012 5:09 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
import javax.persistence.OneToMany;


for (Field f : Router.class.getFields()) {
         OneToMany oneToMany = f.getAnnotation(OneToMany.class);
         if (oneToMany != null) {
            System.out.println("field " + f.getName() + " has mappedby attribute: " + oneToMany.mappedBy());
         }
}


Top
 Profile  
 
 Post subject: Re: How to check hibernate mappings using reflection?
PostPosted: Tue Feb 28, 2012 10:02 am 
Newbie

Joined: Mon Feb 27, 2012 12:50 pm
Posts: 2
Thank you!


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.