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 ...
随机推荐
- 给vim编辑器自动添加行号
1.只改变当前用户的vim 在~目录下 vim .vimrc添加一行 set number 即可(普通用户权限即可) 2. 改变所有用户的vim 打开文件 /etc/vimrc 添加一行 set n ...
- LIS问题(DP解法)---poj1631
题目链接:http://poj.org/problem?id=1631 这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence. 以下介绍LIS的解法 ...
- inner join和outer join
内连接 只连接匹配的行 左外连接 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 A left join B等价于A left ...
- Control(拆点+最大流)
Control http://acm.hdu.edu.cn/showproblem.php?pid=4289 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- js Array Map and Set
Array slice slice()就是对应String的substring()版本,它截取Array的部分元素,然后返回一个新的Array: var arr = ['A', 'B', 'C', ' ...
- OrgChart 组织架构与PHP结合使用
一.OrgChart下载地址: https://github.com/dabeng/OrgChart 二.组织架构的表设计 CREATE TABLE `org_info` ( `id` int(11) ...
- 配置Spring框架编写XML的提示
1. 步骤一:先复制, http://www.springframework.org/schema/beans/spring-beans.xsd 2. 步骤二:搜索XML Catalog,点击Add按 ...
- Halcon小函数的封装和代码导出
一.Halcon小函数的封装和修改 1.名词解释: 算子:指Halcon中最基础.最底层的函数(即你看不到它的代码实现),一个算子只有一句话,例如threshold算子. 小函数:由多个算子组合成的函 ...
- Linux统计某文件夹下文件的个数
ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ...