Search code examples
javainheritancejpaeclipselink

JPA - InheritanceType.Joined generates wrong tables


I'm trying to represent in data base a editable table, with multiple field types. My approach looks like this:

TABLES
- uuid //Primary key
- cols
- rows
- name  

VALUE
-col //Key element 
-row //Key element
-parent //Key element - table to witch the value belongs
-v_type //@DiscriminatorColumn

STRING_VALUE
-col //Key
-row //Key
-parent //Key
-value

This goes on for some more value types.

I've mapped it using eclipselink 2.3 as follows:

Table.java

@SuppressWarnings("serial")
@Entity(name="TABLES")
public class Table implements Serializable{
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
    long uuid;
    String name;
    int rows=0,cols=0;
    @OneToMany(targetEntity=Value.class,mappedBy="parent",cascade={CascadeType.ALL},orphanRemoval=true)
    @OrderBy("row ASC")
    List<Value> values;

}

Value.java

@SuppressWarnings("serial")
@Entity
@IdClass(ValueId.class)
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="V_TYPE",length=1,discriminatorType=DiscriminatorType.STRING)
public abstract class Value implements Serializable{
    @Id
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="PARENT")
    Table parent;
    @Id @Column(unique=false)
    int row;
    @Id @Column(unique=false)
    int col;
}

ValueId.java

@SuppressWarnings("serial")
public class ValueId implements Serializable{
    int row,col;
    long parent;
    // Getters setters etc
}

StringValue.java

@SuppressWarnings("serial")
@Entity
@DiscriminatorValue("S")
public class StringValue extends Value {
    String value;
}

But with this implementation the resulting STRING_VALUE table is lacking [parent] field. Thats bad because i can't resolve a value.

What have i done wrong ? ;) I could not "google resolve" this.


Solution

  • The @Id annotation on the parent field in Value should be replaced by @MapsId.

    But you would really do yourself a favor by having a technical, non-functional, auto-generated ID in Value, and let the row, col and parent field be regular fields of the entity. This wouldcertainly make things more performant, and much easier to use in all the parts of the application.