[英]JPA annotations and SQL tables
I just have quick Question. 我只是快速提问。 If I had an sql script like this:
如果我有这样的SQL脚本:
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? 如何使用JPA Annotations在Java中为此设置实体类?
I have this so far: 到目前为止我有这个:
@Entity
public class Person {
@Column(name="phoneNumber")
public int phoneNumber;
@Column(name="name" length=255)
public String name;
}
@Table("person")
on the class @Table("person")
上课 @IdClass
or @EmbeddableId
to specify the primary key. @IdClass
或@EmbeddableId
来指定主键。 Check the documentations of those for how exactly to achieve it. @IdClass
is a bit easier. @IdClass
有点容易。 Take a look at this: 看看这个:
http://download.oracle.com/docs/cd/B32110_01/web.1013/b28221/cmp30cfg001.htm 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.
您最终会得到一个封装主键字段的MemberOFPK类,然后您的MemberOf类将具有EmbeddedID或两个@ID类,具体取决于您是否使主键类可嵌入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.