Question

I am building a library that directly exposes entities within a persistence unit via a REST interface, to be included into a JAX-RS or standard web application.

In order to facilitate the usability of this library, I want the library itself to manage the setting/unsetting of bi-directional associations when necessary, in order to make the persistence and modification of complex entity relationships possible using simple state representations.

As an example, consider the typical 'Project and Employee' model, and suppose that there is a one-to-many relationship from Project to Employee - a Project can contain multiple Employee entities, and an Employee may only 'belong' to one Project at a time.

Also suppose that this relationship is bi-directional - Employee has a project property, and Project has a employees property that returns a collection of Employee entities who currently 'belong' to the Project. The properties Employee.project and Project.employees map the two sides of the bi-directional association.

Consider the following url:

PUT /Project/1/employees

And the accompanying (JSON) state representation

//Employee representation
{
   firstName: "some", 
   lastName:  "guy",
   salary: 1000000.01,
   ...
   ...
}

This URL/state representation combo directs my library to construct and add the Employee entity to the Project.employees collection of the Project whose identification property yields the value '1'.

What I need to be able to do is, given the standard Metamodel API, determine if Employee has some property that fulfills the bi-directional side of this association, if one exists, so that I can set that property to the Project entity in question.

In other words, I need to be able to determine that Employee has a Employee.project property that satisfies the bi-directional component of the Project.employees association.

So the state/URL combination essentially performs the following task:

EntityManager em = ....;

//this is the Employee represented by the state representation    
Employee employee = ..... ;

Object id = 1;
Project project = em.find( Project.class, id );
project.getPersons().add( employee );

I need to know when to do the following

employee.setProject( project );

My current implementation relies on a custom annotation that mimics the 'mapped-by' attribute in JPA 2.0 mappings, and I have planned to implement checks for the 'mapped-by' attribute value in JPA 2.0 annotations. This works BUT, it requires proprietary configuration ( which I am trying to get rid of ), or JPA 2.0 annotations (what if someone exclusively uses mapping files?).

If anyone can point me in the right direction towards determining the properties involved in both sides of a bi-directional association when given the Entity java type and the Metamodel API, it would be most appreciated.

Was it helpful?

Solution

I've come to the conclusion that there is no way to obtain such bidirectional data from the standard Persistence Metamodel. I've solved by my problem by using existing JPA 2.0 annotations, falling back to a custom @Bidirectional annotation if XML mapping is being used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top