Question

I have two subclasses of MyObject: SubObject1 and SubObject2. Now i have one more Entity: OtherClass. There is a 1:1 relationship between OtherClass und SubObject1 and a 1:n relationship between OtherClass and SubObject2.

MyObject:

@Entity
@Table( name = "MYOBJECT" )
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name = "OBJ_TYP", discriminatorType = DiscriminatorType.STRING )
public abstract class MyObject{

     @Id
     @Column( name = "OBJ_ID")
     private Long id;

     ...
}

SubObject1:

@Entity
@Table( name = "MYOBJECT" )
@DiscriminatorValue( value = "SUB1" )
public class SubObject1 extends MyObject{
    @OneToOne(mappedBy="subObject1")
    private OtherClass otherClass;
    ...
}

SubObject2:

@Entity
@Table( name = "MYOBJECT" )
@DiscriminatorValue( value = "SUB2" )
public class SubObject2 extends MyObject{

    @OneToMany(mappedBy="subObject2")
    private Set<OtherClass> otherClassList;  
    ...
}

OtherClass:

@Entity
@Table( name = "OTHER")
public class OtherClass{

    @OneToOne
    @JoinColumn(name = "OTHER_OBJ_ID", referencedColumn = "OBJ_ID", nullable=true)
    private SubObject1 subObject1;

    @ManyToOne
    @JoinColumn( name = "OTHER_OBJ_ID", referencedColumn = "OBJ_ID", nullable = true)
    private SubObject2 subObject2;

}

Now, when i have a hql query like:

FROM OtherClass c LEFT JOIN FETCH c.subObject1 LEFT JOIN FETCH c.subObject2

i get:

java.lang.RuntimeException: org.hibernate.MappingException: Repeated column in mapping for entity: ...OtherClass column: OTHER_OBJ_ID (should be mapped with insert="false" update="false")

I know that the column "OTHER_OBJ_ID" is repeated. But why i can't do it like that? I thought that hibernate could find out which type of entity should be there because of the declaration for the discriminator value. I really don't want to add a column in the OTHER Table. Is there a possibility to do it without adding a new column and seperate obj1 and obj2?

Please help me!

No correct solution

OTHER TIPS

You have no choice but to use distinct columns in your mapping or use a single OneToMany collection of MyObject and have your Java code or HQL get the subclasses you're looking for.

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