Hibernate 注解和配置文件两种方法的对比(有实例)
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 注解和配置文件两种方法的对比(有实例)的更多相关文章
- jQuery中清空元素.empty()和.html(''),两种方法的对比
jQuery 中有 .empty() 和 .html() 两种方式,都能够清空所选父元素中的所有子元素.但是这两者清空元素的方式上,有着很大的区别: 1.empty() jQuery对象.empty( ...
- mybatis学习之路----批量更新数据两种方法效率对比
原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...
- java web 读取配置文件两种方法
package com.tsinghua.getDataBaseConn; import java.io.IOException;import java.io.InputStream;import j ...
- Oracle中spool命令实现的两种方法比较
---恢复内容开始--- 要输出符合要求格式的数据文件只需在select时用字符连接来规范格式.比如有如下表 SQL>; select id,username,password from myu ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
- spring 配置文件 引入外部的property文件的两种方法
spring 的配置文件 引入外部的property文件的两种方法 <!-- 引入jdbc配置文件 方法一 --> <bean id="propertyConfig ...
- centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节 ...
- 【mybatis基础】mybatis开发dao两种方法
mybatis是一个支持普通SQL查询,存储过程和高级映射的优秀的持久层的框架,是apache下的顶级项目.mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.mybat ...
随机推荐
- python中使用Opencv进行人脸检测
这两天学习了人脸识别,看了学长写的代码,边看边码边理解搞完了一边,再又是自己靠着理解和记忆硬码了一边,感觉还是很生疏,就只能来写个随笔加深一下印象了. 关于人脸识别,首先需要了解的是级联分类器Casc ...
- tensorflow生成随机数的操作 tf.random_normal & tf.random_uniform & tf.truncated_normal & tf.random_shuffle
tf.random_normal 从正态分布输出随机值. random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name ...
- javascript 中 if (window != top) top.location.href = location.href;的意思
如果当前窗口不是顶级窗口,就强制修改为顶级窗口: 目的是为了不让别人用iframe嵌入你的页面
- 第八章 高级搜索树 (a1)伸展树:逐层伸展
- 树莓派项目——基于树莓派的WIFI网络互传系统设计
一 实验原理 所需硬件:树莓派3B,TP-LINK WiFi模块,笔记本电脑,网线 所需软件:Putty.远程桌面链接.python.cmd界面 树莓派3B是只有信用卡大小的微型电脑,其系统基于Lin ...
- Maven核心简析
本文以类图的方式,介绍maven核心的12个概念以及相互之间的关系. Table of Contents 1 maven管理的目标:工程(Project) 1.1 工程依赖关系 1.2 工程聚合关系 ...
- php中正则案例分析
案例一如下: $regex='/[\s\S]*/'; $str='lemon'; $matches=array(); if(preg_match($regex,$str,$matches)){ var ...
- Castle ActiveRecord学习(一)简介
简介 来源:http://www.cnblogs.com/zxj159/p/4082987.html 一.Active Record(活动记录)模式 Active Record是业务逻辑层中(< ...
- 源代码安装grub-customizer
wget https://launchpad.net/grub-customizer/5.0/5.0.6/+download/grub-customizer_5.0.6.tar.gztar zxvf ...
- laravel使用$errors提取错误信息
1.控制器 2.模板