hibernate多对多形式(User类<---->Educate类)

1.基于注解的形式:

User类:

package com.ssh.entities;

import java.util.Date;
import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table; @Entity
@Table(name="user")
public class User {
@Id
@Column(name="id")
@GeneratedValue
private Long id;//员工编号 @Column(name="name")
private String name;//员工用户名 @Column(name="password")
private String password;//登录密码 @Column(name="sex")
private Byte sex;//性别 @Column(name="birthday")
private Date birthday;//生日 @Column(name="createtime")
private Date createtime;//创建时间 @Column(name="isadmin")
private Byte isadmin;//是否为管理员 @Column(name="content")
private String content;//人员简介 @ManyToMany(targetEntity=com.ssh.entities.Educate.class,cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@JoinTable(
name="user_educate",
joinColumns={@JoinColumn(name="user_id")},
inverseJoinColumns={@JoinColumn(name="educate_id")}
)
private Set<Educate> educate=new HashSet<Educate>();
public Set<Educate> getEducate() {
return educate;
}
public void setEducate(Set<Educate> educate) {
this.educate = educate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Byte getIsadmin() {
return isadmin;
}
public void setIsadmin(Byte isadmin) {
this.isadmin = isadmin;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public User(Long id, String name, String password, Byte sex, Date birthday,
Date createtime, Byte isadmin, String content) {
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.birthday = birthday;
this.createtime = createtime;
this.isadmin = isadmin;
this.content = content;
}
public User() {
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", sex=" + sex + ", birthday=" + birthday + ", createtime="
+ createtime + ", isadmin=" + isadmin + ", content=" + content
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

Educate类:

package com.ssh.entities;

import java.util.Date;
import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table; @Entity
@Table(name="educate")
public class Educate {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;//培训标号 @Column(name="name")
private String name;//培训名称 @Column(name="purpose")
private String purpose;//培训目的 @Column(name="begintime")
private Date begintime;//培训开始时间 @Column(name="endtime")
private Date endtime;//培训结束时间 @Column(name="datum")
private String datum;//培训材料 @Column(name="teacher")
private String teacher;//培训讲师 @Column(name="student")
private String student;//培训人员 @Column(name="createtime")
private Date createtime;//创建时间 @Column(name="educate")
private Byte educate;//培训是否完成 @Column(name="effect")
private String effect;//培训效果 @Column(name="summarize")
private String summarize;//培训总结 @ManyToMany(mappedBy="educate",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private Set<User> user=new HashSet<User>(); public Set<User> getUser() {
return user;
}
public void setUser(Set<User> user) {
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String purpose) {
this.purpose = purpose;
}
public Date getBegintime() {
return begintime;
}
public void setBegintime(Date begintime) {
this.begintime = begintime;
}
public Date getEndtime() {
return endtime;
}
public void setEndtime(Date endtime) {
this.endtime = endtime;
}
public String getDatum() {
return datum;
}
public void setDatum(String datum) {
this.datum = datum;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Byte getEducate() {
return educate;
}
public void setEducate(Byte educate) {
this.educate = educate;
}
public String getEffect() {
return effect;
}
public void setEffect(String effect) {
this.effect = effect;
}
public String getSummarize() {
return summarize;
}
public void setSummarize(String summarize) {
this.summarize = summarize;
}
public Educate(Long id, String name, String purpose, Date begintime,
Date endtime, String datum, String teacher, String student,
Date createtime, Byte educate, String effect, String summarize) {
this.id = id;
this.name = name;
this.purpose = purpose;
this.begintime = begintime;
this.endtime = endtime;
this.datum = datum;
this.teacher = teacher;
this.student = student;
this.createtime = createtime;
this.educate = educate;
this.effect = effect;
this.summarize = summarize;
}
public Educate() {
}
@Override
public String toString() {
return "Educate [id=" + id + ", name=" + name + ", purpose=" + purpose
+ ", begintime=" + begintime + ", endtime=" + endtime
+ ", datum=" + datum + ", teacher=" + teacher + ", student="
+ student + ", createtime=" + createtime + ", educate="
+ educate + ", effect=" + effect + ", summarize=" + summarize
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Educate other = (Educate) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

需要注意的是,如果是通过spring管理的话,需要在applicationContext.xml文件中的<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">中,配置

<property name="packagesToScan" value="com.ssh.entities"></property>

2.基于配置文件的形式:

需要注意的是,如果是通过spring管理的话,需要在applicationContext.xml文件中的<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">中,配置

<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>

User类:

package com.ssh.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set; public class User {
private Long id;//员工编号
private String name;//员工用户名
private String password;//登录密码
private Byte sex;//性别
private Date birthday;//生日
private Date createtime;//创建时间
private Byte isadmin;//是否为管理员
private String content;//人员简介
private Set<Educate> educate=new HashSet<Educate>();
public Set<Educate> getEducate() {
return educate;
}
public void setEducate(Set<Educate> educate) {
this.educate = educate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Byte getIsadmin() {
return isadmin;
}
public void setIsadmin(Byte isadmin) {
this.isadmin = isadmin;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public User(Long id, String name, String password, Byte sex, Date birthday,
Date createtime, Byte isadmin, String content) {
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.birthday = birthday;
this.createtime = createtime;
this.isadmin = isadmin;
this.content = content;
}
public User() {
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", sex=" + sex + ", birthday=" + birthday + ", createtime="
+ createtime + ", isadmin=" + isadmin + ", content=" + content
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

Educate类:

package com.ssh.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set; public class Educate {
private Long id;//培训标号
private String name;//培训名称
private String purpose;//培训目的
private Date begintime;//培训开始时间
private Date endtime;//培训结束时间
private String datum;//培训材料
private String teacher;//培训讲师
private String student;//培训人员
private Date createtime;//创建时间
private Byte educate;//培训是否完成
private String effect;//培训效果
private String summarize;//培训总结
private Set<User> user=new HashSet<User>(); public Set<User> getUser() {
return user;
}
public void setUser(Set<User> user) {
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String purpose) {
this.purpose = purpose;
}
public Date getBegintime() {
return begintime;
}
public void setBegintime(Date begintime) {
this.begintime = begintime;
}
public Date getEndtime() {
return endtime;
}
public void setEndtime(Date endtime) {
this.endtime = endtime;
}
public String getDatum() {
return datum;
}
public void setDatum(String datum) {
this.datum = datum;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Byte getEducate() {
return educate;
}
public void setEducate(Byte educate) {
this.educate = educate;
}
public String getEffect() {
return effect;
}
public void setEffect(String effect) {
this.effect = effect;
}
public String getSummarize() {
return summarize;
}
public void setSummarize(String summarize) {
this.summarize = summarize;
}
public Educate(Long id, String name, String purpose, Date begintime,
Date endtime, String datum, String teacher, String student,
Date createtime, Byte educate, String effect, String summarize) {
this.id = id;
this.name = name;
this.purpose = purpose;
this.begintime = begintime;
this.endtime = endtime;
this.datum = datum;
this.teacher = teacher;
this.student = student;
this.createtime = createtime;
this.educate = educate;
this.effect = effect;
this.summarize = summarize;
}
public Educate() {
}
@Override
public String toString() {
return "Educate [id=" + id + ", name=" + name + ", purpose=" + purpose
+ ", begintime=" + begintime + ", endtime=" + endtime
+ ", datum=" + datum + ", teacher=" + teacher + ", student="
+ student + ", createtime=" + createtime + ", educate="
+ educate + ", effect=" + effect + ", summarize=" + summarize
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Educate other = (Educate) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

User.hbm.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.ssh.entities.User" table="user">
<id column="id" name="id" type="java.lang.Long">
<generator class="native"></generator>
</id>
<property name="name" length="50" type="java.lang.String"/>
<property name="password" length="50" type="java.lang.String"/>
<property name="sex" length="4" type="java.lang.Byte"/>
<property name="birthday" length="23" type="java.util.Date"/>
<property name="createtime" length="23" type="java.util.Date"/>
<property name="isadmin" length="4" type="java.lang.Byte"/>
<property name="content" length="2000" type="java.lang.String"/>
<set name="educate" table="user_educate" lazy="false" cascade="all" inverse="false">
<key column="user_id"></key>
<many-to-many class="com.ssh.entities.Educate" column="educate_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

Educate.hbm.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.ssh.entities.Educate" table="educate">
<id column="id" name="id" type="java.lang.Long">
<generator class="native"></generator>
</id>
<property name="name" length="100" type="java.lang.String"></property>
<property name="purpose" length="500" type="java.lang.String"/>
<property name="begintime" length="23" type="java.util.Date"/>
<property name="endtime" length="23" type="java.util.Date"/>
<property name="datum" length="2000" type="java.lang.String"/>
<property name="teacher" length="50" type="java.lang.String"/>
<property name="student" length="50" type="java.lang.String"/>
<property name="createtime" length="23" type="java.util.Date"/>
<property name="effect" length="500" type="java.lang.String"/>
<property name="educate" length="1" type="java.lang.Byte"/>
<property name="summarize" length="2000" type="java.lang.String"/>
<set name="user" table="user_educate" lazy="true" cascade="all" inverse="true">
<key column="educate_id"></key>
<many-to-many class="com.ssh.entities.User" column="user_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

Hibernate 注解和配置文件两种方法的对比(有实例)的更多相关文章

  1. jQuery中清空元素.empty()和.html(''),两种方法的对比

    jQuery 中有 .empty() 和 .html() 两种方式,都能够清空所选父元素中的所有子元素.但是这两者清空元素的方式上,有着很大的区别: 1.empty() jQuery对象.empty( ...

  2. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  3. java web 读取配置文件两种方法

    package com.tsinghua.getDataBaseConn; import java.io.IOException;import java.io.InputStream;import j ...

  4. Oracle中spool命令实现的两种方法比较

    ---恢复内容开始--- 要输出符合要求格式的数据文件只需在select时用字符连接来规范格式.比如有如下表 SQL>; select id,username,password from myu ...

  5. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  6. Java 获取*.properties配置文件中的内容 ,常见的两种方法

    import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...

  7. spring 配置文件 引入外部的property文件的两种方法

    spring  的配置文件 引入外部的property文件的两种方法 <!-- 引入jdbc配置文件    方法一 --> <bean id="propertyConfig ...

  8. centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课

    centos  lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress  安装phpmyadmin  定时备份mysql两种方法  第二十五节 ...

  9. 【mybatis基础】mybatis开发dao两种方法

    mybatis是一个支持普通SQL查询,存储过程和高级映射的优秀的持久层的框架,是apache下的顶级项目.mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.mybat ...

随机推荐

  1. k-means处理图片

    问题描述:把给定图片,用图片中最主要的三种颜色来表示该图片 k-means思想: 1.选择k个点作为初始中心 2.将每个点指派到最近的中心,形成k个簇cluster 3.重新计算每个簇的中心 4.如果 ...

  2. go 第一个项目

    官方下载go: https://golang.org/dl/ 安装完成后:cmd命令下:go go env:查看当前的环境配置:

  3. web中的中文乱码处理

    1.页面设置pageEncoding="UTF-8" <%@ page contentType="text/html;charset=UTF-8" lan ...

  4. 元素的定位tag_name,link_text,class_name

    tag_name 就是根据HTML的标签的名称来定位的: 案例:打开我要自学网,会有用户名和密码的输入框 例如:拿51zxw.net为例 from time import sleep #加载浏览器驱动 ...

  5. cdoj203-Islands 【并查集】

    http://acm.uestc.edu.cn/#/problem/show/203 Islands Time Limit: 30000/10000MS (Java/Others)     Memor ...

  6. Python str() 函数

    Python str() 函数  Python 内置函数 描述 str() 函数将对象转化为适于人阅读的形式. 语法 以下是 str() 方法的语法: class str(object='') 参数 ...

  7. Django搭建数据库

    ---恢复内容开始--- 一.form表单提交数据的三个要素 1.form标签必须要有action个和method属性 2.所有获取用户输入的标签必须放在form表单中,必须要有name属性 3.必须 ...

  8. SpringMVC工作原理1(基础机制)

    图1.基本原理图 Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获:       2. Dispat ...

  9. IBM MQ + WebSphere + Spring JMS配置方法

    IBM MQ + WebSphere + Spring JMS配置方法  首先要在WAS里面配置IBM MQ作为JMS消息的提供者,在WAS管理控制台: Resources->JMS Provi ...

  10. win7系统administrator用户提示没有管理员权限,造装驱动安装错误,软件无法使用

    警告1909.无法创建快捷方式 最近使用windows 7 32位安装软件,好多都失败.出现以上类似错误. 解决方法:在系统盘(C:)右键属性“安全”选项卡--“编辑”,添加“Everyone”设置为 ...