开发者

JPA annotations and SQL tables

开发者 https://www.devze.com 2023-02-14 12:46 出处:网络
I just have quick Question. If I had an sql script like this: create tabl开发者_如何学运维e person(

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

create tabl开发者_如何学运维e 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;


}


  • 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.


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.

0

精彩评论

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