最近在学着用anotation配置hibernate,结果发现一些诡异的情况……
实体类:
[code="java"]
ackage com.yyz.beans;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "score")
public class Score {
private int sId;
private Course cou;
private Student stu;
private double score;
@Id
@GeneratedValue
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public Score(Student s1, Course c1, double i) {
this.stu=s1;
this.cou=c1;
this.score=i;
}
@ManyToOne
@JoinColumn(name = "course_id")
public Course getCou() {
return cou;
}
public void setCou(Course cou) {
this.cou = cou;
}
@ManyToOne
@JoinColumn(name = "student_id")
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
[/code]
然后我让hibernate自动帮我生成表,结果它给我来了这么个建表语句:
[code="sql"]
22:37:41,130 DEBUG SchemaExport:377 -
create table score (
sId integer not null,
score double precision not null,
course_id integer,
student_id integer not null auto_increment,
primary key (student_id, course_id)
)
[/code]
明显我指定的主键是sid,为什么生成的建表语句里course_id和student_id为联合主键呢?
明显我设置了sid为自增长,为什么生成的建表语句里student_id成了自增长字段呢?