2

Good evening. I'm trying to map a class hierarchy to a single table using JPA/Hibernate and receive an error on my subclass stating "The entity has no primary key attribute defined". The classes are defined as follows:

@Entity
@Table(name = "payments")
@Inheritance (strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="creditDebitFlag",discriminatorType=DiscriminatorType.STRING)
@Veto
public abstract class Payment implements IPayment, Serializable{

private static final long serialVersionUID = 8354755060201271169L;
public Integer id;
     ...

@Override
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public void setId(Integer id) {
    this.id = id;
}

@Override
public Integer getId() {
    return this.id;
}
}
@Entity
@DiscriminatorValue("C")
@Veto
public class CreditPayment extends Payment implements Serializable {


    private static final long serialVersionUID = 1L;

public CreditPayment() {
    super();
}   
}

What am I doing wrong? I've read through the Hibernate docs and spent quite a bit of time pawing through Google and can't see what I've missed.

Thanks in advance for your assistance!

Chris K.
  • 692
  • 3
  • 8
  • 14

1 Answers1

3

Your problem has nothing to do with inheritance. This error comes, because you have persistence mapping annotations in setter. Those annotations should be in fields or getters. In this case just move annotations from setId to getId (and if you have these annotations for other setters, move them as well).

JPA specification:

When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[7]....
[7] These annotations must not be applied to the setter methods.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • That was it! I could have sworn I had set the annotations on the getter, but I just moved them and the problem went away. Thank you! – Chris K. Oct 26 '11 at 02:02