0

I have a class called Coach, that has an attribute of type Person. The person class has an attribute called id. Inside of the Coach table in the database, a field, id, is used as the primary key, which corresponds to the id of the person. How do I specify the @id for the Coach class, using annotations?

Well, a coach is a person, so there is a person table that has an id field, first name, and last name.

@Entity
@Table(name="COACH_TABLE")

public class Coach {
  @OneToOne
  @JoinColumn(name = "ID")
  private Person person;

}

@Entity
@Table(name="PERSON_TABLE")

public class Person {
  @Id
  @Column(name="ID")
  private int id;
}

I want to say that the @Id attribute for my Coach class is the id of the Person attribute.

user1154644
  • 4,491
  • 16
  • 59
  • 102
  • JPA has the annotation [`javax.persistence.Id`](http://docs.oracle.com/javaee/7/api/javax/persistence/Id.html). But you seem to be asking about some kind of relation. How can the primary key of `Coach` correspond to the id of one person? What relational design is this? Please include the relevant parts of your DB schema. –  Oct 19 '13 at 19:55
  • Please take a look at this SO: http://stackoverflow.com/questions/6833370/jpa-onetoone-with-shared-id-can-i-do-this-better – John Ament Oct 20 '13 at 20:27
  • That helps, but I think this is a different case. The ID for the Coach is a property in the Person attribute. Can I model this with JPA annotations? – user1154644 Oct 22 '13 at 00:52

0 Answers0