学习hibernate第一天 ----关系映射

一对:单项外键关联

annotation:@OneToOne   @JoinColumn(name="外键的名字") 自动生成一个外键(相当于代替另一个类的对象 Wife wife)

xml:注意:用<many-to-one  name="关联类的实例名" column="数据库中的列名"  unique="true" ></many-to-one> unique=true一定要设置

一对一双向外键关联

annotation:两个关联类都用@OneToOne 在你不希望生成外键的类中加入mappedBy=""  如;

public class Husband {

private int id;

private String name;

private Wife wife;

@OneToOne

@JoinColumn(name="wifeId")

public Wife getWife() {

return wife;

}

public void setWife(Wife wife) {

this.wife = wife;

}

 

 

 

public class Wife {

private int id;

private String name;

private Husband husband;

@OneToOne(mappedBy="wife")

public Husband getHusband() {

return husband;

}

public void setHusband(Husband husband) {

this.husband = husband;

}

如果不希望wife类中出现外键,可在wife类中加入mappedBy,代表外键映射已经被husband类中的wife实例映射了

 

*凡是双向关联,必设mappedBy

xml:

 

这里面的property-ref=""相当于mappedby

 

一对一双向主键关联

不重要

组建映射:java中有两个类,但是在数据库中只有一张表

annotation: 将Wife类的主键删除,什么标记都不需要,为了避免name和husband的name冲突,将Wife类的name改成wifeName:

package com.bjsxt.hibernate;

 

 

 

public class Wife {

 

private String wifeName;

private int age;

 

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

 

public String getWifeName() {

return wifeName;

}

public void setWifeName(String name) {

this.wifeName = name;

}

 

}

 

 

husband类中,将Wife引用类型加@Embedded,代表被引用:

package com.bjsxt.hibernate;

 

import javax.persistence.Embedded;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.JoinColumns;

import javax.persistence.OneToOne;

 

@Entity

public class Husband {

private int id;

private String name;

private Wife wife;

@Id

@GeneratedValue

public int getId() {

return id;

}

 

public String getName() {

return name;

}

@Embedded

public Wife getWife() {

return wife;

}

public void setId(int id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public void setWife(Wife wife) {

this.wife = wife;

}

 

}

xml的实现:只需要加入<compont>,在此标签下面加入要映射的列(property)

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.Husband" >

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<component name="wife">

<property name="wifeName"></property>

<property name="age"></property>

</component>

    </class>

 

</hibernate-mapping>

多对一单向关联;

annotation:

可以在多的那个类中添加关于一个类的,并在这个类中增加@manytoone,对于那个少的类,可以不做修改:

package com.bjsxt.hibernate;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="t_user")

public class User {

private int id;

private String name;

private Group group;

@ManyToOne

 

public Group getGroup() {

return group;

}

public void setGroup(Group group) {

this.group = group;

}

@Id

@GeneratedValue

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;

}

}

xml实现:

在多的那个类的xml设置值:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.User" table="t_user">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<many-to-one name="group" column="groupId" />

    </class>

 

 

</hibernate-mapping>

一对多单向关联:

 

 

 

一对多,多对一双向关联:

annotation:

在一的那一方,即group方设置Set 来存放Group;在其get方法上,设置@onetomany,设置mappedBy属性,是关联字段设置在user表中

package com.bjsxt.hibernate;

 

import java.util.HashSet;

import java.util.Set;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToMany;

import javax.persistence.Table;

 

@Entity

@Table(name="t_group")

public class Group {

private int id;

private String name;

private Set<User> users = new HashSet<User>();

@Id

@GeneratedValue

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;

}

@OneToMany(mappedBy="group")

 

public Set<User> getUsers() {

return users;

}

public void setUsers(Set<User> users) {

this.users = users;

}

}

在多的类中,设置类与一的那一方对应,即设置Group group,在其get方法上设置@manytoone;

package com.bjsxt.hibernate;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="t_user")

public class User {

private int id;

private String name;

private Group group;

@ManyToOne

public Group getGroup() {

return group;

}

public void setGroup(Group group) {

this.group = group;

}

@Id

@GeneratedValue

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;

}

}

xml实现:

在多的那一方,即user方,设置<many-to-one name="映射的一的实例的名字" column=“用来关联的字段(外键)”>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.User" table="t_user">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<many-to-one name="group" column="groupId"></many-to-one>

    </class>

 

</hibernate-mapping>

在一的那一方,即Group方,设置<set><one-to-many name="set的名字" >

<key column="对应的类的主键"></key>

<one-to-many class="对应的多的那一方的类的地址">

 

</set>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.Group" table="t_group">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<set name="users">

<key column="groupId"></key>

<one-to-many class="com.bjsxt.hibernate.User"/>

</set>

    </class>

 

</hibernate-mapping>

 

一对:单项外键关联

annotation:@OneToOne   @JoinColumn(name="外键的名字") 自动生成一个外键(相当于代替另一个类的对象 Wife wife)

xml:注意:用<many-to-one  name="关联类的实例名" column="数据库中的列名"  unique="true" ></many-to-one> unique=true一定要设置

一对一双向外键关联

annotation:两个关联类都用@OneToOne 在你不希望生成外键的类中加入mappedBy=""  如;

public class Husband {

private int id;

private String name;

private Wife wife;

@OneToOne

@JoinColumn(name="wifeId")

public Wife getWife() {

return wife;

}

public void setWife(Wife wife) {

this.wife = wife;

}

 

 

 

public class Wife {

private int id;

private String name;

private Husband husband;

@OneToOne(mappedBy="wife")

public Husband getHusband() {

return husband;

}

public void setHusband(Husband husband) {

this.husband = husband;

}

如果不希望wife类中出现外键,可在wife类中加入mappedBy,代表外键映射已经被husband类中的wife实例映射了

 

*凡是双向关联,必设mappedBy

xml:

 

这里面的property-ref=""相当于mappedby

 

一对一双向主键关联

不重要

组建映射:java中有两个类,但是在数据库中只有一张表

annotation: 将Wife类的主键删除,什么标记都不需要,为了避免name和husband的name冲突,将Wife类的name改成wifeName:

package com.bjsxt.hibernate;

 

 

 

public class Wife {

 

private String wifeName;

private int age;

 

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

 

public String getWifeName() {

return wifeName;

}

public void setWifeName(String name) {

this.wifeName = name;

}

 

}

 

 

husband类中,将Wife引用类型加@Embedded,代表被引用:

package com.bjsxt.hibernate;

 

import javax.persistence.Embedded;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.JoinColumns;

import javax.persistence.OneToOne;

 

@Entity

public class Husband {

private int id;

private String name;

private Wife wife;

@Id

@GeneratedValue

public int getId() {

return id;

}

 

public String getName() {

return name;

}

@Embedded

public Wife getWife() {

return wife;

}

public void setId(int id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public void setWife(Wife wife) {

this.wife = wife;

}

 

}

xml的实现:只需要加入<compont>,在此标签下面加入要映射的列(property)

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.Husband" >

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<component name="wife">

<property name="wifeName"></property>

<property name="age"></property>

</component>

    </class>

 

</hibernate-mapping>

多对一单向关联;

annotation:

可以在多的那个类中添加关于一个类的,并在这个类中增加@manytoone,对于那个少的类,可以不做修改:

package com.bjsxt.hibernate;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="t_user")

public class User {

private int id;

private String name;

private Group group;

@ManyToOne

 

public Group getGroup() {

return group;

}

public void setGroup(Group group) {

this.group = group;

}

@Id

@GeneratedValue

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;

}

}

xml实现:

在多的那个类的xml设置值:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.User" table="t_user">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<many-to-one name="group" column="groupId" />

    </class>

 

 

</hibernate-mapping>

一对多单向关联:

 

 

 

一对多,多对一双向关联:

annotation:

在一的那一方,即group方设置Set 来存放Group;在其get方法上,设置@onetomany,设置mappedBy属性,是关联字段设置在user表中

package com.bjsxt.hibernate;

 

import java.util.HashSet;

import java.util.Set;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToMany;

import javax.persistence.Table;

 

@Entity

@Table(name="t_group")

public class Group {

private int id;

private String name;

private Set<User> users = new HashSet<User>();

@Id

@GeneratedValue

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;

}

@OneToMany(mappedBy="group")

 

public Set<User> getUsers() {

return users;

}

public void setUsers(Set<User> users) {

this.users = users;

}

}

在多的类中,设置类与一的那一方对应,即设置Group group,在其get方法上设置@manytoone;

package com.bjsxt.hibernate;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="t_user")

public class User {

private int id;

private String name;

private Group group;

@ManyToOne

public Group getGroup() {

return group;

}

public void setGroup(Group group) {

this.group = group;

}

@Id

@GeneratedValue

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;

}

}

xml实现:

在多的那一方,即user方,设置<many-to-one name="映射的一的实例的名字" column=“用来关联的字段(外键)”>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.User" table="t_user">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<many-to-one name="group" column="groupId"></many-to-one>

    </class>

 

</hibernate-mapping>

在一的那一方,即Group方,设置<set><one-to-many name="set的名字" >

<key column="对应的类的主键"></key>

<one-to-many class="对应的多的那一方的类的地址">

 

</set>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

 

<hibernate-mapping>

<class name="com.bjsxt.hibernate.Group" table="t_group">

<id name="id">

<generator class="native"></generator>

</id>

 

<property name="name"></property>

<set name="users">

<key column="groupId"></key>

<one-to-many class="com.bjsxt.hibernate.User"/>

</set>

    </class>

 

</hibernate-mapping>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值