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 ...
随机推荐
- java heap space解决方法和JVM参数设置
在JVM中如果98%的时间是用于GC(Garbage Collection)且可用的 Heap size 不足2%的时候将抛出异常信息,java.lang.OutOfMemoryError: Java ...
- win10磁盘碎片整理
如果我们想要加快win10系统运行速度的话,就需要定期整理碎片才可以,减少卡顿,提高性能. 一:注意事项 固态硬盘用户千万不要使用‘磁盘碎片整理功能’,因为使用的技术不一样,使用window自带的该功 ...
- 45. Jump Game II (Array; Two-Pointers,Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Mac下MySQL卸载方法
mac下mysql的DMG格式安装内有安装文件,却没有卸载文件……很郁闷的事.1 sudo rm /usr/local/mysql2 sudo rm -rf /usr/local/mysql*3 su ...
- 121. Best Time to Buy and Sell Stock买卖股票12
一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...
- iPhone iPad 各种控件默认高度
iPhone和iPad下各种常见控件的宽度和标准是一样的,所以这里就用iPhone说明. 以下是常见的几种控件的高度.Statusbar,Navigationbar和Tabbar的宽度极其图标大小. ...
- 什么场景应该用 MongoDB(转)
很多人比较关心 MongoDB 的适用场景,也有用户在话题里分享了自己的业务场景,比如: 案例1 用在应用服务器的日志记录,查找起来比文本灵活,导出也很方便.也是给应用练手,从外围系统开始使用Mong ...
- springboot的yaml基础语法与取值,配置类,配置文件加载优先级
1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...
- 在java工程中导入jar包的注意事项
在java工程中导入jar包后一定要bulid path,不然jar包不可以用.而在java web工程中导入jar包后可以不builld path,但最好builld path.
- 在探http请求
参考:https://itbilu.com/other/relate/Ny2IWC3N-.html Cookie和Session都是为了解决HTTP协议的无状态问题,存储HTTP通讯中客户端与服务器之 ...