BDubs
BDubs

Reputation: 1

JPA annotations and SQL tables

I just have quick Question. If I had an sql script like this:

create table person(
    phoneNumber int
    name varchar(100) not null
    primary key(phoneNumber, name)
);

How would I set up the Entity Class for this in Java using JPA Annotations?

I have this so far:

@Entity
public class Person {


    @Column(name="phoneNumber")
    public int phoneNumber;


    @Column(name="name" length=255)
    public String name;


}

Upvotes: 0

Views: 485

Answers (2)

MikeTheReader
MikeTheReader

Reputation: 4190

Take a look at this:

http://download.oracle.com/docs/cd/B32110_01/web.1013/b28221/cmp30cfg001.htm

Basically, what you have is a composite primary key. What you'll end up with is a MemberOFPK class that encapsulates the fields of the primary key, and then your MemberOf class will have either an EmbeddedID or two @ID classes depending on whether you make the primary key class embeddable or not.

Upvotes: 0

Bozho
Bozho

Reputation: 597016

  • you'll need @Table("person") on the class
  • you'll need @IdClass or @EmbeddableId to specify the primary key. Check the documentations of those for how exactly to achieve it. @IdClass is a bit easier.

Upvotes: 2

Related Questions