Hibernate 的多表关系

hibernate表关联的各种配置:

1、多对多单向:

user----->role

user.hbm.xml:

role.hbm.xml:

2、多对多双向:

user.hbm.xml:

role.hbm.xml:




3、一对多单向:

student(多)----->classes(一)

student.hbm.xml:

classes.hbm.xml:

//classesid是在student表中加的一字段列名

4、一对多双向:

student(多)----->classes(一)

student.hbm.xml:

classes.hbm.xml:

//inverse表示维护关系反转,true表示失效,不由classes

//这端来维护。cascade表示级联,即当对classes操作时,发现还未有student持久化,会自动持久化student的

5、一对一主键关联(单向关联Person---->IdCard)

一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建

Person.Java

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class Person {

private int id;

private String name;

private IdCard idCard;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public IdCard getIdCard() {

return idCard;

}

public void setIdCard(IdCard idCard) {

this.idCard = idCard;

}

}

package com.bjsxt.hibernate;

public class Person {

private int id;

private String name;

private IdCard idCard;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public IdCard getIdCard() {

return idCard;

}

public void setIdCard(IdCard idCard) {

this.idCard = idCard;

}

}

Person.hbm.xml://其对应的表中只有id,name两个属性


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

//主键生成方式受制于IdCard主键生成方式

idCard

IdCard.java:

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

}

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

}

IdCard.hbm.xml:


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

6、一对一主键关联(双向Person<-------->IdCard)

与一对一主键关联(单向)大体一致,只不过IdCard.hbm.xml的配置里要一个one-to-ont属性,IdCard.java里加一个Person的引用。

IdCard.java:

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

private Person person;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

public Person getPerson() {

return person;

}

public void setPerson(Person person) {

this.person = person;

}

}

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

private Person person;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

public Person getPerson() {

return person;

}

public void setPerson(Person person) {

this.person = person;

}

}

IdCard.hbm.xml:

view plaincopy to clipboardprint?


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

7、一对一唯一外键关联(单向Person----->IdCard)

其实是多对对一的一个特例。

Person.java

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class Person {

private int id;

private String name;

private IdCard idCard;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public IdCard getIdCard() {

return idCard;

}

public void setIdCard(IdCard idCard) {

this.idCard = idCard;

}

}

package com.bjsxt.hibernate;

public class Person {

private int id;

private String name;

private IdCard idCard;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public IdCard getIdCard() {

return idCard;

}

public void setIdCard(IdCard idCard) {

this.idCard = idCard;

}

}

Person.hbm.xml

view plaincopy to clipboardprint?


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

IdCard.java

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

}

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

}

IdCard.hbm.xml

view plaincopy to clipboardprint?


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

8、一对一唯一外键关联(双向Person<----->IdCard)

相对于上面的一对一唯一外键关联(单向)不同的是,IdCard.java添加了Person的引用与IdCard.hbm.xml添加了one-to-one标签。

IdCard.java:

view plaincopy to clipboardprint?

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

private Person person;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

public Person getPerson() {

return person;

}

public void setPerson(Person person) {

this.person = person;

}

}

package com.bjsxt.hibernate;

public class IdCard {

private int id;

private String cardNo;

private Person person;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCardNo() {

return cardNo;

}

public void setCardNo(String cardNo) {

this.cardNo = cardNo;

}

public Person getPerson() {

return person;

}

public void setPerson(Person person) {

this.person = person;

}

}

IdCard.hbm.xml:

view plaincopy to clipboardprint?


"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

你可能感兴趣的:(Hibernate 的多表关系)