Question

Is it possible with Eclipselink (or even better with standard JPA 2.x) to use JPQL or SQL to define a calculated collection mapping?

We have the following tables:

  • story
  • substory (foreign key to story)
  • substory_x_company (foreign key to substory and company)
  • company

Therefore I created three entities:

  • Story (has many SubStories)
  • SubStory (has many Companies)
  • Company

So far so good, no problem. But now the requirement:

I should map a collection of all companies (ordered by name) in the entity Story and it would be good if this would be possible directly inside the entity (instead of using a separate DAO or even collect and sort all companies from the sub-stories). I'm asking this, because Story has 15 other relations, designed the same way as described above and I like to minimize boilerplate if possible.

Please don't criticize the table structure, as I cannot change it for the moment ;-)

Thanks a lot in advance for all your inputs!

Was it helpful?

Solution

You could define a database view that defines the Story/Company relationship as a ManyToMany relationship (or make it a real table).

http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Views

With EclipseLink you can use arbitrary expressions to map relationships,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

or, you could also use your own custom SQL,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/CRUDStoredProcedures

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