Question

Environment: EE7/JPA 2.1 (Glassfish 4/EclipseLink 2.5)

given the following database tables:

+-------------+
| Foo         |
+=============+
| id          |
| ...         |
+-------------+

+-------------+
| Bar         |
+=============+
| id          |
| ...         |
+-------------+

+-------------+
| Baz         |
+=============+
| id          |
| ...         |
+-------------+

+-------------+
| Parameter   |
+=============+
| id_generic  |
| type        |
| ...         |
+-------------+

'type' specifies the concrete table with 'id_generic' being the PrimaryKey of it.

What is the proper way, to map a structure like this in JPA/EclipseLink?

one possible solution may be using an artificial inheritance hierarchy like

@...
@Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorColumn(name="type"...)
public abstract class Parameter {...}

@...
@DiscriminatorValue("FOO")
public class FooParameter {...}

@...
@DiscriminatorValue("BAR")
public class BarParameter {...}

@...
@DiscriminatorValue("BAZ")
public class BazParameter {...}

but this seems rather "ugly" to me, because the different subclasses do not differ in any way.

i know, there are other, implementation-specific solutions like using a constant value inside the @JoinColum like

@JoinColumns({
    @JoinColumn(name="Foo.ID" referencedColumnName="id_generic"),
    @JoinColumn(name="type" referencedColumnName="'FOO'") //FOO as constant value
});
private...

or the @Where Annotation in Hibernate. But what would be the solution in EclipseLink?

UPDATE: DataModel Picture for clarification

enter image description here

Was it helpful?

Solution

You may consider using a @VariableOneToOne mapping in EclipseLink,

http://eclipse.org/eclipselink/documentation/2.5/jpa/extensions/a_variableonetoone.htm

or using an Expression in your mapping's selection criteria,

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

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