开发者

Does @AttributeOverride annotation breaks polymorphism?

I need help with inheritance in JPA

I will illustrate my problem w开发者_如何学运维ith an example:

Supose we have an Animal class and a Cat class.

@Entity
@Table(name = "animal")
@DiscriminatorColumn(name="animal_type",columnDefinition="Integer",length=11)
public class Animal implements Serializable{
    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "animal_id", nullable = false)
    private Long id;

    ...

}

@Entity
@Table(name = "cat")
@DiscriminatorValue("1")
@AttributeOverride(name="id",column=@Column(name="cat_id"))
public class Cat extends Animal {

    ...

}

As the example shows, i want to use a different name for my id column in the cat table

But then when i try to perform a polymorphic query asking for all the Animal instances i get an error. It is like the query cannot handle the different field names according the animal instance.

Am i doing this right? Thanks in advance.


You can try use this annotation. I put an examples

@PrimaryKeyJoinColumn(name="cat_id")
public class Cat extends Animal


No, i guess I'm not.

First i forgot the @Inheritance annotation:

@Inheritance(strategy=InheritanceType.JOINED)

Anyway i see that i can override the rest of the fields but not the Id. Or maybe the problem is that I'm using the id in the query but not the rest of the fields.


You have to use the annotations @PrimaryKeyJoinColumn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜