JPA 2.1 + Hibernate 5.2.17 + MySQL 8.0.11 persistence.xml
的一些設定。
首先用Maven下載需要的jar。
pom.xml
<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>idv.matt</groupId>
<artifactId>mytest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mytest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.17.Final</version>
</dependency>
</dependencies>
</project>
如果你是以WAR來部屬應用程式,則JPA Hibernate的persistence.xml
的位置必須放在WAR檔中的WEB-INF/classes/META-INF/
目錄下,所以就是在(例如Eclipse)專案類別路徑(classpath)下的META-INF
目錄(src/META-INF/
。是folder,不是package)(參考)。
JPA Hibernate的persistence.xml
的範例如下
persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>idv.matt.entity.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb?useSSL=false&characterEncoding=utf-8" />
<property name="javax.persistence.jdbc.user" value="matt" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
關於persistence.xml
中Hibernate的設定參數可參考這裡。
Entity類別Employee
,其對映資料庫的employee
資料表。資料庫中email欄位有唯一限制(unique)。
Employee.java
package idv.matt.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="email", unique=true)
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
測試查詢資料的程式碼如下,直接使用main方法來測試。在Hibernate JPA要取得EntityManager
來下SQL。
Main.java
package idv.matt.main;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import idv.matt.entity.Employee;
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb");
EntityManager em = emf.createEntityManager();
Query query = em.createNativeQuery("select * from employee", Employee.class);
@SuppressWarnings("unchecked")
List<Employee> employeeList = query.getResultList();
em.close();
for(Employee employee : employeeList) {
System.out.println(employee.getEmail());
}
}
}
如果本篇有幫助到您,幫忙點一下廣告支持,感恩。
參考:
- Tutorial Using the Java Persistence API (JPA)
- The Java EE 5 Tutorial - Introduction to the Java Persistence API
- What's the difference between JPA and Hibernate?
- JPA Bootstrapping
- General Configuration
- Persistence Units
- Spring Boot 2 + Spring Data JPA + MySQL 8 簡單範例
- Hibernate JPA Entity 生命週期 (JPA Entity Lifecycle)
- Hibernate JPA 設定多個資料庫來源
沒有留言:
張貼留言