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>