在開始之前,我們需要更新我們的Maven依賴項,因為我們現在將使用Hibernate和Spring。 將以下依賴項添加到pom.xml中 :
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>3.6.8.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- spring framework --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>3.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>3.1.0.RELEASE</version></dependency>
如果您是Maven的新手,您可能現在想知道-您怎么知道這些? 我在哪里可以買到? 好吧,只需轉到http://mvnrepository.com/并輸入您要搜索的啤酒。 您將獲得有關Maven依賴項的完整代碼。 如果您曾經嘗試在不使用Maven的情況下自己組裝Spring或Hibernate應用程序,那么您可能知道它是多么痛苦。 使用Maven,事情變得簡單得多。
還要注意,我們包括了對MySQL連接器的依賴。 如果您決定使用其他數據庫,請不要忘記更改它。
使用Hibernate,我們有2種選擇如何將POJO變成實體。 我們要么使用XML并創建映射文件 ,要么將一些元信息放入我們的代碼(java批注)中。 有些人對此感到恐懼,并認為這是與框架的結合。 的確,您在類路徑中將需要javax.persistence批注,但我們不會實現接口或擴展框架類。 我們將只在代碼中添加一些元信息,而POJO仍然只是帶有一些額外信息的POJO。
現在,我們將POJO轉換為實體。 我們將需要進行以下更改:
- 為Hibernate添加默認的無參數構造函數
- 為字段創建獲取器和設置器
- 添加equals和hashCode方法。
- 添加持久性注釋。 請注意,我們還使用@Table批注來區分Java和SQL命名約定。
- 添加ID字段。 這些將是我們關系數據庫中的主鍵。
這是很多樣板代碼,因此讓您的IDE幫您。 大多數現代IDE都會為您生成構造函數,getter,setter,equals和hashCode。
package org.timesheet.domain;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = 'employee')
public class Employee {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;private String name;private String department;public Employee() {}public Employee(String name, String department) {this.name = name;this.department = department;}public String getName() {return name;}public String getDepartment() {return department;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}public void setDepartment(String department) {this.department = department;}@Overridepublic String toString() {return 'Employee [id=' + id + ', name=' + name + ', department='+ department + ']';}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result+ ((department == null) ? 0 : department.hashCode());result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}if (obj == null) {return false;}if (!(obj instanceof Employee)) {return false;}Employee other = (Employee) obj;if (department == null) {if (other.department != null) {return false;}} else if (!department.equals(other.department)) {return false;}if (id == null) {if (other.id != null) {return false;}} else if (!id.equals(other.id)) {return false;}if (name == null) {if (other.name != null) {return false;}} else if (!name.equals(other.name)) {return false;}return true;}}
package org.timesheet.domain;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = 'manager')
public class Manager {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;private String name;public Manager() {}public Manager(String name) {this.name = name;}public String getName() {return name;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}if (obj == null) {return false;}if (!(obj instanceof Manager)) {return false;}Manager other = (Manager) obj;if (id == null) {if (other.id != null) {return false;}} else if (!id.equals(other.id)) {return false;}if (name == null) {if (other.name != null) {return false;}} else if (!name.equals(other.name)) {return false;}return true;}@Overridepublic String toString() {return 'Manager [id=' + id + ', name=' + name + ']';}}
package org.timesheet.domain;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;@Entity
@Table(name='timesheet')
public class Timesheet {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;@OneToOne@JoinColumn(name = 'employee_id')private Employee who;@OneToOne@JoinColumn(name = 'task_id')private Task task;private Integer hours;public Timesheet() {}public Timesheet(Employee who, Task task, Integer hours) {this.who = who;this.task = task;this.hours = hours;}public Employee getWho() {return who;}public Task getTask() {return task;}public Integer getHours() {return hours;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public void setWho(Employee who) {this.who = who;}public void setTask(Task task) {this.task = task;}public void setHours(Integer hours) {this.hours = hours;}/*** Manager can alter hours before closing task* @param hours New amount of hours*/public void alterHours(Integer hours) {this.hours = hours;}@Overridepublic String toString() {return 'Timesheet [id=' + id + ', who=' + who + ', task=' + task+ ', hours=' + hours + ']';}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((hours == null) ? 0 : hours.hashCode());result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((task == null) ? 0 : task.hashCode());result = prime * result + ((who == null) ? 0 : who.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}if (obj == null) {return false;}if (!(obj instanceof Timesheet)) {return false;}Timesheet other = (Timesheet) obj;if (hours == null) {if (other.hours != null) {return false;}} else if (!hours.equals(other.hours)) {return false;}if (id == null) {if (other.id != null) {return false;}} else if (!id.equals(other.id)) {return false;}if (task == null) {if (other.task != null) {return false;}} else if (!task.equals(other.task)) {return false;}if (who == null) {if (other.who != null) {return false;}} else if (!who.equals(other.who)) {return false;}return true;}
}
最后,這是我們需要同時使用@ManyToMany映射的Task實體。 這是因為一名雇員可以從事多個任務,而一項任務可以分配多個雇員。 我們使用@JoinTable和@JoinColumn批注定義了m:n的外觀。
package org.timesheet.domain;import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;@Entity
@Table(name = 'task')
public class Task {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@ManyToMany(fetch = FetchType.EAGER)@JoinTable(name = 'task_employee',joinColumns = {@JoinColumn(name = 'task_id')},inverseJoinColumns = {@JoinColumn(name = 'employee_id')})private List<Employee> assignedEmployees = new ArrayList<Employee>();@OneToOne@JoinColumn(name = 'manager_id')private Manager manager;private String description;boolean completed;public Task() {}public Task(String description, Manager manager, Employee... employees) {this.description = description;this.manager = manager;assignedEmployees.addAll(Arrays.asList(employees));completed = false;}public Manager getManager() {return manager;}public List<Employee> getAssignedEmployees() {return assignedEmployees;}public void addEmployee(Employee e) {assignedEmployees.add(e);}public void removeEmployee(Employee e) {assignedEmployees.remove(e);}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public boolean isCompleted() {return completed;}public void setCompleted(boolean completed) {this.completed = completed;}public void setAssignedEmployees(List<Employee> assignedEmployees) {this.assignedEmployees = assignedEmployees;}public void setManager(Manager manager) {this.manager = manager;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@Overridepublic boolean equals(Object o) {if (this == o) {return true;}if (o == null || getClass() != o.getClass()) {return false;}Task task = (Task) o;if (completed != task.completed) {return false;}if (description != null ? !description.equals(task.description) : task.description != null) {return false;}if (id != null ? !id.equals(task.id) : task.id != null) {return false;}if (manager != null ? !manager.equals(task.manager) : task.manager != null) {return false;}return true;}@Overridepublic int hashCode() {int result = id != null ? id.hashCode() : 0;result = 31 * result + (manager != null ? manager.hashCode() : 0);result = 31 * result + (description != null ? description.hashCode() : 0);result = 31 * result + (completed ? 1 : 0);return result;}@Overridepublic String toString() {return 'Task{' +'id=' + id +', assignedEmployees=' + assignedEmployees +', manager=' + manager +', description='' + description + '\'' +', completed=' + completed +'}';}
}
因此,我們實際上并沒有做任何特別的建模。 如果您喜歡一些UML,請看下圖,關系與以前相同。

好的,我們有實體,現在讓我們創建數據庫。 選擇一些數據庫管理工具(即使是普通終端也可以)并創建時間表數據庫(默認情況下,mysql將在Mac OS X上安裝到/ usr / local / mysql / bin / mysql):
$ mysql -u root
mysql > create database timesheet;
如果您可能在不了解之前就已經配置過Hibernate,那么在處理SessionFactory時就需要很多文件和樣板代碼。 使用Spring,這些事情要簡單得多。
現在,我們將創建我們的第一個Spring Bean配置文件 -該文件在其中注冊Spring容器的bean。 如果我不得不向根本不了解Spring的人解釋這個文件是什么,那是Spring容器可以找到對象的神奇包。
現代IDE將幫助您正確使用所有XML名稱空間,例如,您可以從STS向導中查看圖片。 NetBeans具有類似的功能,IntelliJ可以動態解析名稱空間。
命名配置文件persistence-beans.xml,然后將其放在src / main / resources文件夾下。

因此,設置Hibernate,事務,批注配置等就像在XML文件中創建幾個bean一樣簡單。 另外,我們可以將Java Config用于Spring,但是XML config仍然使用更多,因此我們將堅持使用這些配置。 我不想阻止您使用Java Config! XML config 現在更受歡迎,但是我不能保證在接下來的幾年中。
我已經評論了每個bean,以確保您在繼續之前了解我們在這里所做的事情。 如果要直觀地了解Bean之間的連接,則可以再次使用一些工具-在STS中將其稱為Bean Graph,在IntelliJ中將其稱為Dependencies。 您可以在下面的圖片中看到依賴項示例。

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns:context='http://www.springframework.org/schema/context'xmlns:tx='http://www.springframework.org/schema/tx'xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd'><!-- we can use annotations --><context:annotation-config /> <!-- package to look for annotated classes --><context:component-scan base-package='org.timesheet.service.impl' /><!-- we will manage transactions with annotations --><tx:annotation-driven /><!-- data source for our database --><bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'><property name='driverClassName' value='com.mysql.jdbc.jdbc2.optional.MysqlDataSource' /><property name='url' value='jdbc:mysql://localhost/timesheet' /><property name='username' value='root' /><property name='password' value='' /></bean><!-- configure hibernate session factory --><bean id='sessionFactory'class='org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean'><property name='dataSource' ref='dataSource' /><property name='annotatedClasses' ><list><value>org.timesheet.domain.Employee</value><value>org.timesheet.domain.Manager</value><value>org.timesheet.domain.Task</value><value>org.timesheet.domain.Timesheet</value></list></property><property name='hibernateProperties'><props><prop key='dialect'>org.hibernate.dialect.MySQL5InnoDBDialect</prop><prop key='hibernate.show_sql'>true</prop><prop key='hibernate.hbm2ddl.auto'>update</prop></props></property> </bean></beans>
好的,那是很多配置,對嗎? 不好的是,我們已經將實體的名稱作為純文本放置到XML中,因此它不是友好的重構形式。 但我認為對于本教程來說是可以接受的 讓我們為Hibernate編寫集成測試,以便我們知道一切都已正確設置。
package org.timesheet.integration;import static org.junit.Assert.*;import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@ContextConfiguration(locations = '/persistence-beans.xml')
public class HibernateConfigurationTest extends AbstractJUnit4SpringContextTests {@Autowiredprivate SessionFactory sessionFactory;@Testpublic void testHibernateConfiguration() {// Spring IOC container instantiated and prepared sessionFactoryassertNotNull (sessionFactory); }}
我要你在這里注意兩件事。 首先,我們擴展AbstractJUnit4SpringContextTests類。 我們告訴它應該在哪里尋找使用Spring bean的實際XML配置。 否則,我們將不得不自己創建Spring容器,這意味著需要更多樣板代碼。
其次,我們使用@Autowired批注。 這意味著我們不會使用新的運算符手動創建SessionFactory實例,而是將其通過Spring容器自動裝配(注入)! 這是Spring容器最重要的目的之一-依賴于接口并注入實現而不是手工創建它們。
一切現在都應該正常工作,我認為這部分就足夠了。
如果您愿意,可以檢查普通的SQL,并在此處查看表,請按照以下步驟進行操作:
mysql> use timesheet;
mysql> show tables;
+---------------------+
| Tables_in_timesheet |
+---------------------+
| employee |
| manager |
| task |
| task_employee |
| timesheet |
+---------------------+
5 rows in set (0.00 sec)
參考: 第2部分–持久層–從vrtoonjava博客的JCG合作伙伴 Michal Vrtiak 編寫實體并配置Hibernate 。
翻譯自: https://www.javacodegeeks.com/2012/09/spring-persistence-layer-writing.html