1

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!

1
  • Not answering your question, but don't you think generating SQLs directly wouldn't be simpler? Commented Feb 27, 2012 at 18:33

1 Answer 1

2

You can use reflection API for this. Something like this:

Class Cls = ... ;
cls.getField("xyz").getAnnotation(ManyToMany.class).mappedBy
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.