1. 一对一
1.1 单向一对一
@Entity
@Table(name="`HUSBAND`")
@Getter
@Setter
public class Husband {
@Id
private String id;
@Column
private String name;
@OneToOne
@JoinColumn(name="wid",unique = true)
private Wife wife;
}
@Entity
@Table(name="`WIFE`")
@Getter
@Setter
public class Wife {
@Id
private String id;
@Column
private String name;
}
DDL语句
CREATE TABLE "public"."HUSBAND" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
"wid" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "HUSBAND_pkey" PRIMARY KEY ("id"),
CONSTRAINT "fkmdsjpoqvngw6nf2m191u5youl" FOREIGN KEY ("wid") REFERENCES "public"."WIFE" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT "uk_efrl21su077b724pn7stabql1" UNIQUE ("wid")
)
;
CREATE TABLE "public"."WIFE" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "WIFE_pkey" PRIMARY KEY ("id")
)
;
1.2 双向一对一
@Entity
@Table(name="`HUSBAND`")
@Getter
@Setter
public class Husband {
@Id
private String id;
@Column
private String name;
@OneToOne
@JoinColumn(name="wid",unique = true)
private Wife wife;
}
@Entity
@Table(name="`WIFE`")
@Getter
@Setter
public class Wife {
@Id
private String id;
@Column
private String name;
@OneToOne(mappedBy = "wife")
private Husband husband;
}
双向外键关系需要指定一方来管理对象之间的关系。比如在Wife 类中@OneToOne(mappedBy=”wife”)用来指定控制管理权在Husband 类中。
2. 一对多
2.1 双向一对多
@Entity
@Table(name="`ClassRoom`")
@Getter
@Setter
public class ClassRoom {
@Id
private String id;
@Column
private String name;
@OneToMany(mappedBy="cid",cascade={CascadeType.ALL})
private List students;
}
@Entity
@Table(name="`Student`")
@Getter
@Setter
public class Student {
@Id
private String id;
@Column
private String name;
@ManyToOne
@JoinColumn(name = "cid")
private ClassRoom classRoom;
}
DDL语句
CREATE TABLE "public"."ClassRoom" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "ClassRoom_pkey" PRIMARY KEY ("id")
)
;
CREATE TABLE "public"."Student" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
"cid" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "Student_pkey" PRIMARY KEY ("id"),
CONSTRAINT "fk8tk1mcahy6p0icynb5nfp4xro" FOREIGN KEY ("cid") REFERENCES "public"."ClassRoom" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
)
;
2.2 单向一对多
@Entity
@Table(name="`ClassRoom`")
@Getter
@Setter
public class ClassRoom {
@Id
private String id;
@Column
private String name;
@OneToMany(targetEntity = Student.class)
@JoinColumn(name="cid")
private List students;
}
@Entity
@Table(name="`Student`")
@Getter
@Setter
public class Student {
@Id
private String id;
@Column
private String name;
}
3. 多对多
@Entity
@Table(name = "`User`")
@Getter
@Setter
public class User {
@Id
private String id;
@Column
private String name;
@ManyToMany(targetEntity = Role.class)
@JoinTable(
name = "`User_Role`",
joinColumns = @JoinColumn(name = "uid"),
inverseJoinColumns = @JoinColumn(name = "rid")
)
private List roles;
}
@Entity
@Table(name = "`Role`")
@Getter
@Setter
public class Role {
@Id
private String id;
@Column
private String name;
@ManyToMany(targetEntity = User.class)
@JoinTable(
name = "`User_Role`",
joinColumns = @JoinColumn(name = "rid"),
inverseJoinColumns = @JoinColumn(name = "uid")
)
private List users;
}
@Entity
@Table(name = "`User_Role`")
@Getter
@Setter
@EqualsAndHashCode
public class UserRole implements Serializable {
@Id
private String uid;
@Id
private String rid;
}
DDL语句
CREATE TABLE "public"."User" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
)
;
CREATE TABLE "public"."Role" (
"id" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(255) COLLATE "pg_catalog"."default",
CONSTRAINT "Role_pkey" PRIMARY KEY ("id")
)
;
CREATE TABLE "public"."User_Role" (
"uid" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"rid" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
CONSTRAINT "User_Role_pkey" PRIMARY KEY ("uid", "rid"),
CONSTRAINT "fk3sgutdk2w2qp0m93b56668iq3" FOREIGN KEY ("rid") REFERENCES "public"."Role" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT "fkkp3fplbbyj2ohb5vv6668ubg2" FOREIGN KEY ("uid") REFERENCES "public"."User" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
)
;
如果需要单向多对多,去掉对应关系即可。