Hibernate映射与注解实例总结

简介:
 
XML配置

1、一对一单向

<class name="Person">

     <id name="id" column="personId">

         <generator class="native"/>

     </id>

     <many-to-one name="address"

        column="addressId"

        unique="true"

         not-null="true"/>

</class>

 

<class name="Address">

     <id name="id" column="addressId">

         <generator class="native"/>

     </id>

</class>
 


2.、一对一双向

<class name="Person">

     <id name="id" column="personId">

         <generator class="native"/>

     </id>

    <many-to-one name="address"

        column="addressId"

        unique="true"

         not-null="true"/>

</class>

 

<class name="Address">

    <id name="id" column="addressId">

         <generator class="native"/>

     </id>

   <one-to-one name="person"

        property-ref="address"/>

</class>
 


3.、一对多单向

<class name="Person">

     <id name="id" column="personId">

         <generator class="native"/>

     </id>

     <set name="addresses">

         <key column="personId"

            not-null="true"/>

      <one-to-many class="Address"/>

     </set>

</class>

 

<class name="Address">

     <id name="id" column="addressId">

         <generator class="native"/>

    </id>

</class>
 


4、多对一单向

<class name="Person">

     <id name="id" column="personId">

         <generator class="native"/>

     </id>

     <many-to-one name="address"

        column="addressId"

         not-null="true"/>

</class>

 

<class name="Address">

     <id name="id" column="addressId">

         <generator class="native"/>

     </id>

</class>
 


5、一对多双向

<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" inverse=”true”>

    <key column="groupId"></key>  //与<many-to-one>的column一致

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

   </set>

     </class>

 

</hibernate-mapping>

<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>
 


6、多对多单向

<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student">
   <id name="id">
    <generator class="native"></generator>
   </id>
 
   <property name="name"></property>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
<class name="com.bjsxt.hibernate.Teacher">
   <id name="id">
    <generator class="native"></generator>
   </id>
   <property name="name"></property>
  <set name="students" table="t_s">
    <key column="teacher_id"></key>
    <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
   </set>
    </class>
</hibernate-mapping>
 


7、多对多双向

<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student">
   <id name="id">
    <generator class="native"></generator>
   </id>
 
   <property name="name"></property>
  <set name="teachers" table="t_s" inverse=”true”>
    <key column="student_id"></key>
    <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
   </set>
    </class>
</hibernate-mapping>

<hibernate-mapping>
<class name="com.bjsxt.hibernate.Teacher">
   <id name="id">
    <generator class="native"></generator>
   </id>
 
   <property name="name"></property>
  <set name="students" table="t_s">
    <key column="teacher_id"></key>
    <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
   </set>
    </class>
</hibernate-mapping>

--------------------------------------------------------------------------------------------------------


Hibernate实体关系:双向关联,mappedBy必设

注解方式

1、一对一单向

@Entity       

public class Husband{

           @Id

           @GenerateValue

             pirvate int id;

             private String name;

            @OneToOne

            @JoinColumn(name="wifeId")

             private Wife wife;

      }

        @Entity

     public class Wife{

           @Id

           @GenerateValue

         private int id;

         private String name;

    }
 

2.、一对一双向

@Entity       

public class Husband{

           @Id

           @GenerateValue

             pirvate int id;

             private String name;

            @OneToOne

            @JoinColumn(name="wifeId")

             private Wife wife;

      }

 

        @Entity

     public class Wife{

           @Id

           @GenerateValue

         private int id;

         private String name;

       @OneToOne(mappedBy="wife")

        private Husband husband;

    }
 

3.、一对多单向

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
@JoinColumn(name="groupId") //不加出现3张表
public Set<User> getUsers() {
   return users;
}
public void setUsers(Set<User> users) {
   this.users = users;
}
}

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;
@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;
}
}
 


4、多对一单向

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
@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;
}
}

 

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;
}
}

5、一对多双向

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;
}
}

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;
}
}
 


6、多对多单向

@Entity
public class Student {
private int id;
private String name;
@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;
}
}

@Entity
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@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;
}
@ManyToMany
@JoinTable(name="t_s",
  joinColumns={@JoinColumn(name="teacher_id")},
  inverseJoinColumns={@JoinColumn(name="student_id")}
   )     //改变中间表的名字和字段名字
public Set<Student> getStudents() {
   return students;
}
public void setStudents(Set<Student> students) {
   this.students = students;
}
}
 


7、多对多双向

@Entity
public class Student {
private int id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@ManyToMany(mappedBy="students")
public Set<Teacher> getTeachers() {
   return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
   this.teachers = teachers;
}
@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;
}
}

@Entity
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@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;
}
@ManyToMany
@JoinTable(name="t_s",
  joinColumns={@JoinColumn(name="teacher_id")},
  inverseJoinColumns={@JoinColumn(name="student_id")}
   )
public Set<Student> getStudents() {
   return students;
}
public void setStudents(Set<Student> students) {
   this.students = students;
}
}


 

原帖地址:http://hi.baidu.com/%BE%B2xiaojing/blog/item/29665c83ff78f08af703a663.html

目录
相关文章
|
7月前
|
Java 数据库连接
hibernate注解实体类(Dept.java)
hibernate注解实体类(Dept.java)
|
3月前
|
Java 数据库连接 API
解锁高效开发秘籍:深入探究 Hibernate 如何优雅处理一对多与多对多关系,让数据映射再无烦恼!
【9月更文挑战第3天】Hibernate 是 Java 领域中最流行的 ORM 框架之一,广泛用于处理实体对象与数据库表之间的映射。尤其在处理复杂关系如一对多和多对多时,Hibernate 提供了丰富的 API 和配置选项。本文通过具体代码示例,展示如何使用 `@OneToMany`、`@JoinColumn`、`@ManyToMany` 和 `@JoinTable` 等注解优雅地实现这些关系,帮助开发者保持代码简洁的同时确保数据一致性。
57 4
|
4月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
97 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
4月前
|
Java 数据库连接 数据库
AI 时代风起云涌,Hibernate 实体映射引领数据库高效之路,最佳实践与陷阱全解析!
【8月更文挑战第31天】Hibernate 是一款强大的 Java 持久化框架,可将 Java 对象映射到关系数据库表中。本文通过代码示例详细介绍了 Hibernate 实体映射的最佳实践,包括合理使用关联映射(如 `@OneToMany` 和 `@ManyToOne`)以及正确处理继承关系(如单表继承)。此外,还探讨了常见陷阱,例如循环依赖可能导致的无限递归问题,并提供了使用 `@JsonIgnore` 等注解来避免此类问题的方法。通过遵循这些最佳实践,可以显著提升开发效率和数据库操作性能。
87 0
|
4月前
|
数据库 开发者 Java
Hibernate映射注解的魔力:实体类配置的革命,让你的代码量瞬间蒸发!
【8月更文挑战第31天】Hibernate 是一款出色的对象关系映射框架,简化了 Java 应用与数据库的交互。其映射注解让实体类配置变得直观简洁。本文深入剖析核心概念与使用技巧,通过示例展示如何简化配置。
49 0
|
6月前
|
JSON Java 数据库连接
Hibernate中使用@Lob 注解保存String[] 问题
Hibernate中使用@Lob 注解保存String[] 问题
36 2
|
7月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
|
7月前
|
Java 数据库连接
hibernate注解实体类(Emp.java)
hibernate注解实体类(Emp.java)
|
7月前
|
Java 数据库连接
hibernate注解的测试
hibernate注解的测试
|
7月前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——( EmpCondition)
Hibernate中使用Criteria查询及注解——( EmpCondition)