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

时间: 2024-10-29 06:11:17

Hibernate映射与注解实例总结的相关文章

【SSH系列】Hibernate映射 -- 一对多关联映射

     映射原理       一对多关联映射和多对一关联映射的映射原理是一样一样的,所以说嘛,知识都是相通的,一通百通,为什么说一对多关联映射和多对一关联映射是一样的呢?因为她们都是在多的一端加入一个外键,指向一的一段,关联关系都是在多的一端进行维护,只是我们在写映射的时候发生了变化.       一对多和多对一的映射原理是一样的,但是她们之间也存在着小小的区别,毕竟世界上没有两片完全相同的叶子,她们之间的区别就是维护的关系不同,我们先来看多对一,多端维护一端的关系,在加载多端的时候,可以将一

hibernate中用annotation注解怎么样配置TIMESTAMP类型的数据呢

问题描述 请问hibernate中用annotation注解怎么样配置TIMESTAMP类型的数据并且能自动插入和更新呢?就是相当于mysql中timeTIMESTAMPDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP;这样的数据列,在hibernate中怎么样用annotation注解来配置呢(不要xml的配置方式).各位大虾帮帮忙啊,跪求中-- 解决方案 解决方案二:没用过注解吖.看下这个资料吧解决方案三:@Column(name="D_UP

浅析php设计模式之数据对象映射模式_php实例

php中的设计模式中有很多的各种模式了,在这里我们来为各位介绍一个不常用的数据映射模式吧,希望文章能够帮助到各位. 数据映射模式使您能更好的组织你的应用程序与数据库进行交互. 数据映射模式将对象的属性与存储它们的表字段间的结合密度降低.数据映射模式的本质就是一个类,它映射或是翻译类的属性或是方法到数据库的相应字段,反之亦然. 数据映射的作用(工作)就在于能对双方所呈现出的信息的理解,并能对信息的存取进行控制,如根据存储在数据表中的信息 重建新的域对象,或是用域对象的信息来更新或删除数据表中的相关

Hibernate映射集合属性、映射组件属性和关联映射都是几张数据表之间的映射的问题,它们在应用上有什么区别?分别应用在什么场合?它们的效率的比较如何?

问题描述 Hibernate映射集合属性.映射组件属性和关联映射都是几张数据表之间的映射的问题,它们在应用上有什么区别?分别应用在什么场合?它们的效率的比较如何?我刚学hibernate,希望诸位前辈赐教!!! 解决方案 解决方案二:请加我QQ,我有视屏教程讲的很细解决方案三:50307924

java-mysql year字段用hibernate映射插数据

问题描述 mysql year字段用hibernate映射插数据 year 在java中对应java.sql.Date 我如何通过date把 1990 这个数据插入到year中,不要月和年 解决方案 java.sql.Date的启示年份是1900,使用年份的时候需要减去1900. 解决方案二: String dt = new String(new SimpleDateFormat(""yyyy"").format(date));

hibernate映射表报错 unknown Oracle major version [0]

问题描述 我在MyEclipse中通过hibernate映射表的时候总是报错在"Generating Artifacts"期间发生了内部错误.unknown Oracle major version [0]我查看了MyEclipse的日志文件信息如下:!ENTRY org.eclipse.core.jobs 4 2 2008-11-08 09:56:42.031!MESSAGE 在"Generating Artifacts"期间发生了内部错误.!STACK 0org

sql query-J2EE SSH框架中 hibernate映射文件hbm.xml相关问题

问题描述 J2EE SSH框架中 hibernate映射文件hbm.xml相关问题 使用hbm.xml文件时,有遇到一个标签,在标签中我只要HQL语句加上小于<号MyEclipse就提示错误,那我该如何添加小于号呢,请问大家是怎么回事,如何解决? 具体如下图 解决方案 需要转义的. < > 分别表示 < 和 >! 如果还有其他的,可以查看CSDN移动问答 解决方案二: 打开方式的问题 在MyEclipse的资源树里右击该文件,"Open as"选择合适的方

mysql 数据库-HIbernate映射查询出错,

问题描述 HIbernate映射查询出错, UserIm表继承User表,使用的是@Inheritance(strategy = InheritanceType.JOINED)策略,其中User对象中有Accout对象,User和Accout对象之间是有关联表 ORG_ACCOUNT_USER_CLIENT的,但是这个关联表里还有其他字段有外键关联,字段名为 CLIENT_ID,不知道hibernate是怎么弄的,查询的时候把这个 CLIENT_ID 也查询了,我没设置让他查询 CLIENT_I

hibernate-关于Hibernate映射子类的问题

问题描述 关于Hibernate映射子类的问题 报错如下 Hibernate: select muser0_.id as id2_, muser0_.password as password2_, muser0_.email as email2_, muser0_.username as username2_, muser0_.type as type2_ from mysql.muser muser0_ where muser0_.id=? org.hibernate.WrongClassEx