1. Jar包
添加
hibernate3.2.jar,hibernate-annotations-3.3.0.jar,
hibernate-commons-annotations.jar,
ejb3-persistence.jar.
这样就可以使用hibernate的annotation了。
2. 创建名为ssh_annotation的数据库
3. 配置hibernate.cfg.xml 以下的配置会自动把带有Annotation的javabean映射为数据库表
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=ssh_annotation</property>
<property name="connection.username">sa</property>
<property name="connection.password">sasa</property>
<!-- 数据库言 -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- 将Session扔到线程里去处理 -->
<property name="current_session_context_class">thread</property>
<!-- 在控制台打印SQL语句 -->
<property name="show_sql">true</property>
<!-- 自动把实体类与属性映射成数据库中的表与列 -->
<property name="hbm2ddl.auto">update</property>
<!--
<mapping resource="com/rong/entity/User.hbm.xml"/>
-->
<!-- 在Hibernate中注册User实体类,区别于上面注释掉的resource写法 -->
<property name="myeclipse.connection.profile">SQL Server 2008</property>
<mapping class="com.rong.entity.User"/>
<mapping class="com.rong.entity.many_to_one_Department"/>
<mapping class="com.rong.entity.many_to_one_Employee"/>
<mapping class="com.rong.entity.one_to_many_Department"/>
<mapping class="com.rong.entity.one_to_many_Employee"/>
<mapping class="com.rong.entity.one_to_one_ID_Card"/>
<mapping class="com.rong.entity.one_to_one_Person"/>
<mapping class="com.rong.entity.many_to_many_Student"/>
<mapping class="com.rong.entity.many_to_many_Teacher"/>
</session-factory>
</hibernate-configuration>
4. 一对一
(1)one_to_one_Person.java
packagecom.rong.entity;
importjavax.persistence.CascadeType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.OneToOne;
importjavax.persistence.Table;
@Entity
@Table(name="one_to_one_Person")
publicclass one_to_one_Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@Column(name="age")
private int age;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="ID_Card_fk")
private one_to_one_ID_Card card;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public one_to_one_ID_Card getCard() {
return card;
}
public void setCard(one_to_one_ID_Card card){
this.card = card;
}
}
(2)one_to_one_ID_Card.java
package com.rong.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="one_to_one_ID_Card")
public class one_to_one_ID_Card {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=18)
private String name;
@OneToOne(mappedBy = "card")
private one_to_one_Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public one_to_one_Person getPerson() {
return person;
}
public void setPerson(one_to_one_Person person) {
this.person = person;
}
}
5. 一对多
(1)one_to_many_Employee.java
packagecom.rong.entity;
importjavax.persistence.CascadeType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.ManyToOne;
importjavax.persistence.Table;
@Entity
@Table(name="one_to_many_Employee")
publicclass one_to_many_Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@ManyToOne( cascade = {CascadeType.PERSIST,CascadeType.MERGE} )
@JoinColumn(name="depart_id")
private one_to_many_Department depart;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public one_to_many_Department getDepart() {
return depart;
}
public void setDepart(one_to_many_Departmentdepart) {
this.depart = depart;
}
}
(2)one_to_many_Department.java
packagecom.rong.entity;
importjava.util.HashSet;
importjava.util.Set;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.OneToMany;
importjavax.persistence.Table;
@Entity
@Table(name="one_to_many_Department")
publicclass one_to_many_Department {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@OneToMany(mappedBy="depart")
private Set<one_to_many_Employee>emps=new HashSet<one_to_many_Employee>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<one_to_many_Employee>getEmps() {
return emps;
}
public voidsetEmps(Set<one_to_many_Employee> emps) {
this.emps = emps;
}
}
6. 多对一
(1)many_to_one_Employee.java
packagecom.rong.entity;
importjavax.persistence.CascadeType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.ManyToOne;
importjavax.persistence.Table;
@Entity
@Table(name="many_to_one_Employee")
publicclass many_to_one_Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@ManyToOne( cascade = {CascadeType.PERSIST,CascadeType.MERGE} )
@JoinColumn(name="depart_id")
private many_to_one_Department depart;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public many_to_one_Department getDepart() {
return depart;
}
public void setDepart(many_to_one_Departmentdepart) {
this.depart = depart;
}
}
(2)many_to_one_Department.java
packagecom.rong.entity;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.Table;
@Entity
@Table(name="many_to_one_Department")
publicclass many_to_one_Department {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7. 多对多
(1)many_to_many_Teacher.java
packagecom.rong.entity;
importjava.util.HashSet;
importjava.util.Set;
importjavax.persistence.CascadeType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.JoinTable;
importjavax.persistence.ManyToMany;
importjavax.persistence.Table;
@Entity
@Table(name="many_to_many_Teacher")
publicclass many_to_many_Teacher {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@ManyToMany(targetEntity =com.rong.entity.many_to_many_Student.class,
cascade = {CascadeType.PERSIST,CascadeType.MERGE})
@JoinTable(name="many_to_many_teacher_student",
joinColumns=@JoinColumn(name ="teacher_id"),
inverseJoinColumns = @JoinColumn(name= "student_id"))
private Set<many_to_many_Student>student=new HashSet<many_to_many_Student>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<many_to_many_Student>getStudent() {
return student;
}
public voidsetStudent(Set<many_to_many_Student> student) {
this.student = student;
}
}
(2)many_to_many_Student.java
packagecom.rong.entity;
importjava.util.HashSet;
importjava.util.Set;
importjavax.persistence.CascadeType;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.ManyToMany;
importjavax.persistence.Table;
@Entity
@Table(name="many_to_many_Student")
publicclass many_to_many_Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name", length=15)
private String name;
@ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},
mappedBy ="student",targetEntity = many_to_many_Teacher.class)
private Set<many_to_many_Teacher>teacher=new HashSet<many_to_many_Teacher>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<many_to_many_Teacher>getTeacher() {
return teacher;
}
public voidsetTeacher(Set<many_to_many_Teacher> teacher) {
this.teacher = teacher;
}
}
8. 测试
packagecom.rong.test;
importjava.util.HashSet;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Set;
importorg.hibernate.Query;
importorg.hibernate.Session;
importcom.rong.entity.many_to_many_Student;
importcom.rong.entity.many_to_many_Teacher;
importcom.rong.entity.many_to_one_Department;
importcom.rong.entity.many_to_one_Employee;
importcom.rong.entity.User;
importcom.rong.entity.one_to_many_Department;
importcom.rong.entity.one_to_many_Employee;
importcom.rong.entity.one_to_one_ID_Card;
importcom.rong.entity.one_to_one_Person;
importcom.rong.service.UserServiceBean;
importtest.rong.hibernate.HibernateUtil;
publicclass test {
private static Session session;
public void initSession() {
session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public void closeSession() {
session.close();
}
public void commitSession() {
session.getTransaction().commit();
}
public void save(Object o){
session.beginTransaction();
session.save(o);
}
public List query(String hql) {
session.beginTransaction();
Query query = session.createQuery(hql);
List list = query.list();
return list;
}
public Object get(Class clazz ,int i) {
session.beginTransaction();
Object o= session.get(clazz,i);
return o;
}
public void update(Object o) {
session.beginTransaction();
session.update(o);
}
public void delete(Class clazz ,int i) {
session.beginTransaction();
session.delete(session.load(clazz,i));
}
public static void main(String args[]){
test t=new test();
t.initSession();
//--------------many to one--------------
/*many_to_one_Departmentdepart=new many_to_one_Department();
depart.setName("会计");
many_to_one_Employee emp=newmany_to_one_Employee();
emp.setName("Shiki");
emp.setDepart(depart);*/
//--------------one tomany---------------
/*one_to_many_Department depart=newone_to_many_Department();
depart.setName("会计");
one_to_many_Employee emp1=newone_to_many_Employee();
emp1.setName("AAA");
emp1.setDepart(depart);
one_to_many_Employee emp2=newone_to_many_Employee();
emp2.setName("BBBB");
emp2.setDepart(depart);
Set emps=new HashSet();
emps.add(emp1);
emps.add(emp2);
depart.setEmps(emps);
t.save(emp1);
t.save(emp2);
t.save(depart);*/
//------------one toone---------------------
/*one_to_one_Person p=newone_to_one_Person();
p.setName("1号");
p.setAge(22);
one_to_one_ID_Card card=newone_to_one_ID_Card();
card.setName("120101xxxxxxxxxxxx");
p.setCard(card);
//card.setPerson(p);
t.save(p);
t.save(card);*/
//-------------many to many-------------------
many_to_many_Teacher teacher1=newmany_to_many_Teacher();
teacher1.setName("1号教师");
//many_to_many_Teacher teacher2=newmany_to_many_Teacher();
//teacher1.setName("2号教师");
many_to_many_Student student1=newmany_to_many_Student();
student1.setName("1号学生");
//many_to_many_Student student2=newmany_to_many_Student();
//student2.setName("2号学生");
//many_to_many_Student student3=newmany_to_many_Student();
//student2.setName("3号学生");
teacher1.getStudent().add(student1);
//teacher1.getStudent().add(student2);
//teacher2.getStudent().add(student3);
student1.getTeacher().add(teacher1);
//student2.getTeacher().add(teacher1);
//student3.getTeacher().add(teacher2);
t.save(teacher1);
//t.save(teacher2);
t.save(student1);
//t.save(student2);
//t.save(student3);
t.commitSession();
//t.closeSession();
//t.delete();
//t.save();
}
}