views:

99

answers:

1

Hi, I need help with inheritance in JPA

I will illustrate my problem with 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.

A: 

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.

Any help will be welcome.

Thanks.

zeven