一对一 就像是一个学生(Person)对应一个年级(Classses),运用的是@OneToOne 及@JoinColumn
例子
下面代码来自:Person.java
// 一对一表连接
@OneToOne
@JoinColumn(name="gid",referencedColumnName = "gid",insertable = false,updatable = false)
private Grade grade;
public Grade getGrade() {
return grade;
}
下面代码来自:Grade.java
package com.sdbairui.demo.Entity;
import javax.persistence.*;
@Table
@Entity(name="grade")
public class Grade {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int gid;
private String gname;
// @OneToOne
// @JoinColumn(name="gid",referencedColumnName = "gid",insertable = false,updatable = false)
// private Classes classes;
//
// public Classes getClasses() {
// return classes;
// }
//
// public void setClasses(Classes classes) {
// this.classes = classes;
// }
@OneToOne
@JoinColumn(name="gid",referencedColumnName = "gid",insertable = false,updatable = false)
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
}
下面代码来自:index.html
<td th:text="${person.grade.gname}"></td>
通过person对象和grade对象连接,person取得了grade对象的gname属性