添加
1.简单对象ORM教程
2.双向一对多教程
diff --git a/hibernate-tutorial/pom.xml b/hibernate-tutorial/pom.xml
new file mode 100644
index 0000000..8d74564
--- /dev/null
+++ b/hibernate-tutorial/pom.xml
@@ -0,0 +1,90 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.supwisdom.tutorial</groupId>
+ <artifactId>hibernate-tutorial</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>hibernate-tutorial</name>
+ <url>http://maven.apache.org</url>
+
+ <properties>
+ <slf4j.version>1.6.6</slf4j.version>
+ <logback.version>1.0.13</logback.version>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <distributionManagement>
+ <repository>
+ <id>ekingstar-releases</id>
+ <name>internal release</name>
+ <url>http://app.supwisdom.com:81/artifactory/libs-release-local</url>
+ </repository>
+ <snapshotRepository>
+ <id>ekingstar-snapshots</id>
+ <name>internal Snapshots</name>
+ <url>http://app.supwisdom.com:81/artifactory/libs-snapshot-local</url>
+ </snapshotRepository>
+ <downloadUrl>http://app.supwisdom.com:81/artifactory</downloadUrl>
+ </distributionManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-core</artifactId>
+ <version>4.3.5.Final</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>${logback.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <version>1.4.178</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.1</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.3</version>
+ <configuration>
+ <includeProjectDependencies>true</includeProjectDependencies>
+ <mainClass>org.h2.tools.Server</mainClass>
+ <commandlineArgs>-baseDir ${basedir}</commandlineArgs>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Many2ManyBiTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Many2ManyBiTest.java
new file mode 100644
index 0000000..f009a83
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Many2ManyBiTest.java
@@ -0,0 +1,10 @@
+package com.supwisdom.tutorial.hibernate.manytomany.bidirection;
+
+/**
+ * 一个学生有多个老师,一个老师又有多个学生,他们之间互相感知,因此是双向多对多关系
+ * @author qianjia
+ *
+ */
+public class Many2ManyBiTest {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Student.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Student.java
new file mode 100644
index 0000000..3fc31b0
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Student.java
@@ -0,0 +1,5 @@
+package com.supwisdom.tutorial.hibernate.manytomany.bidirection;
+
+public class Student {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Teacher.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Teacher.java
new file mode 100644
index 0000000..e89cc78
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/bidirection/Teacher.java
@@ -0,0 +1,5 @@
+package com.supwisdom.tutorial.hibernate.manytomany.bidirection;
+
+public class Teacher {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Boy.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Boy.java
new file mode 100644
index 0000000..f19fc47
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Boy.java
@@ -0,0 +1,57 @@
+package com.supwisdom.tutorial.hibernate.manytomany.unidirection;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name="BOYS")
+public class Boy {
+
+ private Long id;
+ private int age;
+ private String firstname;
+ private String lastname;
+
+ public Boy() {
+ }
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getFirstname() {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname) {
+ this.firstname = firstname;
+ }
+
+ public String getLastname() {
+ return lastname;
+ }
+
+ public void setLastname(String lastname) {
+ this.lastname = lastname;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Girl.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Girl.java
new file mode 100644
index 0000000..23406a0
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Girl.java
@@ -0,0 +1,74 @@
+package com.supwisdom.tutorial.hibernate.manytomany.unidirection;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinTable;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name="GIRLS")
+public class Girl {
+
+ private Long id;
+ private int age;
+ private String firstname;
+ private String lastname;
+ private Set<Boy> boyFriends = new HashSet<Boy>();
+
+ public Girl() {
+ }
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getFirstname() {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname) {
+ this.firstname = firstname;
+ }
+
+ public String getLastname() {
+ return lastname;
+ }
+
+ public void setLastname(String lastname) {
+ this.lastname = lastname;
+ }
+
+ @ManyToMany
+ @JoinTable(name="GIRL_BOY_FRIENDS")
+ public Set<Boy> getBoyFriends() {
+ return boyFriends;
+ }
+
+ public void setBoyFriends(Set<Boy> boyFriends) {
+ this.boyFriends = boyFriends;
+ }
+
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Many2ManyUniTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Many2ManyUniTest.java
new file mode 100644
index 0000000..3e314d9
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytomany/unidirection/Many2ManyUniTest.java
@@ -0,0 +1,10 @@
+package com.supwisdom.tutorial.hibernate.manytomany.unidirection;
+
+/**
+ *
+ * @author qianjia
+ *
+ */
+public class Many2ManyUniTest {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/Many2OneTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/Many2OneTest.java
new file mode 100644
index 0000000..3147114
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/Many2OneTest.java
@@ -0,0 +1,10 @@
+package com.supwisdom.tutorial.hibernate.manytoone;
+
+/**
+ * 有若干害羞的男孩暗恋同一个漂亮姑娘,但是姑娘感知不到暗恋者的存在,因此是多对一关系
+ * @author qianjia
+ *
+ */
+public class Many2OneTest {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/PrettyGirl.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/PrettyGirl.java
new file mode 100644
index 0000000..8b3ffc2
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/PrettyGirl.java
@@ -0,0 +1,5 @@
+package com.supwisdom.tutorial.hibernate.manytoone;
+
+public class PrettyGirl {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/ShyBoy.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/ShyBoy.java
new file mode 100644
index 0000000..cd02ebf
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/manytoone/ShyBoy.java
@@ -0,0 +1,5 @@
+package com.supwisdom.tutorial.hibernate.manytoone;
+
+public class ShyBoy {
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Company.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Company.java
new file mode 100644
index 0000000..cfba4e0
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Company.java
@@ -0,0 +1,52 @@
+package com.supwisdom.tutorial.hibernate.onetomany.bidirection;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name = "COMPANIES")
+public class Company {
+ private Long id;
+
+ private String name;
+
+ private Set<Employee> employees = new HashSet<Employee>();
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ 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;
+ }
+
+ @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="company")
+ public Set<Employee> getEmployees() {
+ return employees;
+ }
+
+ public void setEmployees(Set<Employee> employees) {
+ this.employees = employees;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Employee.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Employee.java
new file mode 100644
index 0000000..242761e
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/Employee.java
@@ -0,0 +1,49 @@
+package com.supwisdom.tutorial.hibernate.onetomany.bidirection;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name = "EMPLOYEES")
+public class Employee {
+
+ private Long id;
+
+ private String name;
+
+ private Company company;
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ 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;
+ }
+
+ @ManyToOne
+ public Company getCompany() {
+ return company;
+ }
+
+ public void setCompany(Company company) {
+ this.company = company;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/One2ManyBiTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/One2ManyBiTest.java
new file mode 100644
index 0000000..77d16ff
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/bidirection/One2ManyBiTest.java
@@ -0,0 +1,100 @@
+package com.supwisdom.tutorial.hibernate.onetomany.bidirection;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+import com.supwisdom.tutorial.hibernate.util.HibernateUtil;
+
+/**
+ * 一个公司有多个雇员,一个雇员只能效力于一各公司,公司和雇员之间知道彼此的存在,因此是双向一对多
+ * 在结构上可以看到 Company里有employees, Employee里有company,因此他们是双向的
+ * @author qianjia
+ */
+public class One2ManyBiTest {
+
+ public static void main(String[] args) {
+ SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
+ try {
+ /**
+ * 新建一个公司和它的雇员
+ */
+ Session session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ Company company = new Company();
+ company.setName("测试公司");
+ Employee e1 = new Employee();
+ e1.setName("雇员A");
+ e1.setCompany(company); // 注意:要在这里设置雇员的公司
+ Employee e2 = new Employee();
+ e2.setName("雇员B");
+ e2.setCompany(company);
+ company.getEmployees().add(e1);
+ company.getEmployees().add(e2);
+ /*
+ * 只需保存公司即可,因为配置了 @OneToMany(cascade=CascadeType.ALL
+ * 也就是说,保存公司会连带着保存雇员
+ */
+ session.saveOrUpdate(company);
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+
+ /**
+ * 删除一个雇员
+ */
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ List<Company> result = session.createQuery("from Company").list();
+ Company company = result.get(0);
+ Employee firstEmployee = company.getEmployees().iterator().next();
+ /**
+ * 只需要把一个雇员从company.employees删除即可,因为配置了 @OneToMany(..., orphanRemoval=true
+ * 意思是,如果Employee不属于任何公司,那么就算orphan(孤儿),孤儿是要被删除的
+ */
+ company.getEmployees().remove(firstEmployee);
+ session.saveOrUpdate(company);
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+
+ /**
+ * 删除一个公司
+ */
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ /**
+ * 只需删除公司就可以了,因为配置了 @OneToMany(cascade=CascadeType.ALL
+ * 也就是说,删除公司会连带着删除雇员
+ */
+ List<Company> result = session.createQuery("from Company").list();
+ Company company = result.get(0);
+ session.delete(company);
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+ } finally {
+ sessionFactory.close();
+ }
+
+ }
+
+ public static void showDatas(Session session) {
+ List<Company> result = session.createQuery("from Company").list();
+ System.out.println("======= Company LIST =======");
+ for (Company company : result) {
+ System.out.println("Company: " + company.getName());
+ System.out.println("Employee List: ");
+ for (Employee emp : company.getEmployees()) {
+ System.out.println(emp.getName());
+ }
+ }
+ }
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/One2ManyUniTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/One2ManyUniTest.java
new file mode 100644
index 0000000..d198575
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/One2ManyUniTest.java
@@ -0,0 +1,76 @@
+package com.supwisdom.tutorial.hibernate.onetomany.unidirection;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+import com.supwisdom.tutorial.hibernate.util.HibernateUtil;
+
+/**
+ * 一个警察盯梢多个小偷,但是小偷不知道已经被警察监视,因此是单向一对多
+ * @author qianjia
+ *
+ */
+public class One2ManyUniTest {
+
+ public static void main(String[] args) {
+ SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
+ try {
+ /**
+ * 新建一个警察和若干小偷
+ */
+ Session session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ /**
+ * TODO 留给你的作业
+ * 需要注意的是,警察和小偷是相互独立的,不能够通过保存警察就自动把小偷保存
+ * 进去,两者需要分别保存,然后再把小偷放到警察里
+ * 提示:
+ * Long id = (Long) session.save(entity); // 可以获得保存到数据库里的对象所分配的id
+ * session.get(Entity.class, id); // 根据id将对象数据库中获取出来
+ */
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+
+ /**
+ * 删除一个小偷
+ */
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ /**
+ * TODO 留给你的作业
+ */
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+
+ /**
+ * 删除警察
+ */
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+ {
+ /**
+ * TODO 留给你的作业
+ */
+ showDatas(session);
+ }
+ session.getTransaction().commit();
+ session.close();
+ } finally {
+ sessionFactory.close();
+ }
+
+ }
+
+ public static void showDatas(Session session) {
+ /**
+ * TODO 留给你的作业,将警察打印出来,将小偷打印出来,将哪个警察监视哪些小偷打印出来
+ */
+ }
+}
+
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Policeman.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Policeman.java
new file mode 100644
index 0000000..7aeb410
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Policeman.java
@@ -0,0 +1,55 @@
+package com.supwisdom.tutorial.hibernate.onetomany.unidirection;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name = "POLICEMEN")
+public class Policeman {
+
+ private Long id;
+
+ private String name;
+
+ private Set<Thief> theives = new HashSet<Thief>();
+
+ @OneToMany
+ @JoinTable(name = "POLICEMEN_THEIVES", joinColumns = @JoinColumn(name = "POLICEMAN_ID"), inverseJoinColumns = @JoinColumn(name = "THIEF_ID"))
+ public Set<Thief> getTheives() {
+ return theives;
+ }
+
+ public void setTheives(Set<Thief> theives) {
+ this.theives = theives;
+ }
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ 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;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Thief.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Thief.java
new file mode 100644
index 0000000..a88f580
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/onetomany/unidirection/Thief.java
@@ -0,0 +1,37 @@
+package com.supwisdom.tutorial.hibernate.onetomany.unidirection;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name = "THIEVES")
+public class Thief {
+
+ private Long id;
+
+ private String name;
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ 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;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/Event.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/Event.java
new file mode 100644
index 0000000..cc87da1
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/Event.java
@@ -0,0 +1,83 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package com.supwisdom.tutorial.hibernate.simple;
+
+import java.util.Date;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table(name = "EVENTS")
+public class Event {
+ private Long id;
+
+ private String title;
+ private Date date;
+
+ public Event() {
+ // this form used by Hibernate
+ }
+
+ public Event(String title, Date date) {
+ // for application use, to create new events
+ this.title = title;
+ this.date = date;
+ }
+
+ @Id
+ @GeneratedValue(generator = "increment")
+ @GenericGenerator(name = "increment", strategy = "increment")
+ public Long getId() {
+ return id;
+ }
+
+ private void setId(Long id) {
+ this.id = id;
+ }
+
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "EVENT_DATE")
+ public Date getDate() {
+ return date;
+ }
+
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/SimpleTest.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/SimpleTest.java
new file mode 100644
index 0000000..f3ad532
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/simple/SimpleTest.java
@@ -0,0 +1,73 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package com.supwisdom.tutorial.hibernate.simple;
+
+import java.util.Date;
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+import com.supwisdom.tutorial.hibernate.util.HibernateUtil;
+
+public class SimpleTest {
+
+ public static void main(String[] args) {
+ SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
+
+ // 保存Event到数据库
+ Session session = sessionFactory.openSession();
+ session.beginTransaction();
+ session.save(new Event("Our very first event!", new Date()));
+ session.save(new Event("A follow up event", new Date()));
+ showDatas(session);
+ session.getTransaction().commit();
+ session.close();
+
+ // 将Event从数据库中删除
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+ List<Event> result = session.createQuery("from Event").list();
+ for (Event event : result) {
+ session.delete(event);
+ }
+ showDatas(session);
+ session.getTransaction().commit();
+ session.close();
+
+ session.getSessionFactory().close();
+ }
+
+ /**
+ * 将数据库中的相关数据打印到控制台
+ * @param sessionFactory
+ */
+ public static void showDatas(Session session) {
+ List<Event> result = session.createQuery("from Event").list();
+ System.out.println("======= EVENT LIST =======");
+ for (Event event : result) {
+ System.out.println("Event (" + event.getDate() + ") : " + event.getTitle());
+ }
+ }
+}
diff --git a/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/util/HibernateUtil.java b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/util/HibernateUtil.java
new file mode 100644
index 0000000..34f37a0
--- /dev/null
+++ b/hibernate-tutorial/src/main/java/com/supwisdom/tutorial/hibernate/util/HibernateUtil.java
@@ -0,0 +1,38 @@
+package com.supwisdom.tutorial.hibernate.util;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.hibernate.SessionFactory;
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.service.ServiceRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper class that takes care of startup and makes accessing the org.hibernate.SessionFactory more
+ * convenient
+ *
+ * @author stivlo
+ */
+public class HibernateUtil {
+
+ private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
+ private static SessionFactory sessionFactory;
+
+ public static SessionFactory getSessionFactory() {
+ if (sessionFactory == null) {
+ try {
+ Configuration configuration = new Configuration();
+ configuration.configure("hibernate.cfg.xml");
+ ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
+ configuration.getProperties()).build();
+ sessionFactory = configuration.buildSessionFactory(serviceRegistry);
+ } catch (Throwable ex) {
+ logger.error(ExceptionUtils.getStackTrace(ex));
+ System.exit(0);
+ }
+ }
+ return sessionFactory;
+ }
+
+}
diff --git a/hibernate-tutorial/src/main/resources/hibernate.cfg.xml b/hibernate-tutorial/src/main/resources/hibernate.cfg.xml
new file mode 100644
index 0000000..b02a979
--- /dev/null
+++ b/hibernate-tutorial/src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,43 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!DOCTYPE hibernate-configuration PUBLIC
+ "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+ "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+
+ <session-factory>
+
+ <!-- Database connection settings -->
+ <property name="connection.driver_class">org.h2.Driver</property>
+ <property name="connection.url">jdbc:h2:tcp://localhost/test</property>
+ <property name="connection.username">sa</property>
+ <property name="connection.password"></property>
+
+ <!-- JDBC connection pool (use the built-in) -->
+ <property name="connection.pool_size">1</property>
+
+ <!-- SQL dialect -->
+ <property name="dialect">org.hibernate.dialect.H2Dialect</property>
+
+ <property name="current_session_context_class">thread</property>
+
+ <!-- Disable the second-level cache -->
+ <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
+
+ <!-- Echo all executed SQL to stdout -->
+ <property name="show_sql">true</property>
+
+ <!-- Drop and re-create the database schema on startup -->
+ <property name="hbm2ddl.auto">create</property>
+
+
+ <mapping class="com.supwisdom.tutorial.hibernate.simple.Event"/>
+
+ <mapping class="com.supwisdom.tutorial.hibernate.onetomany.bidirection.Company"/>
+ <mapping class="com.supwisdom.tutorial.hibernate.onetomany.bidirection.Employee"/>
+
+ <mapping class="com.supwisdom.tutorial.hibernate.onetomany.unidirection.Policeman"/>
+ <mapping class="com.supwisdom.tutorial.hibernate.onetomany.unidirection.Thief"/>
+ </session-factory>
+
+</hibernate-configuration>
diff --git a/hibernate-tutorial/src/main/resources/logback.xml b/hibernate-tutorial/src/main/resources/logback.xml
new file mode 100644
index 0000000..335ce65
--- /dev/null
+++ b/hibernate-tutorial/src/main/resources/logback.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<configuration debug="false">
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%d{MM-dd HH:mm:s} %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <root level="INFO">
+ <appender-ref ref="STDOUT" />
+ </root>
+ <!--
+ <logger name="org.hibernate" level="WARN">
+ <appender-ref ref="STDOUT" />
+ </logger>
+ <logger name="org.apache.struts2" level="WARN">
+ <appender-ref ref="STDOUT" />
+ </logger>
+ -->
+</configuration>