JPA:使用外部table來生成主鍵
使用情景
.將當前主鍵的值單獨儲存到一個數據庫的表中,主鍵的值每次都是從指定的表中查詢來獲得
.這種方法生成主鍵的策略可以適用於任何資料庫,不必擔心不同資料庫不相容造成的問題
.準備前提:
-建立一個jpa專案
-配置persistence.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpa_1_helloworld" transaction-type="RESOURCE_LOCAL">
<!-- 改配置檔名字和位置必須是固定的 -->
<!-- 配置使用什麼orm產品來作為jpa的實現 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- 新增持久化類 -->
<class>com.wayne.helloworld.Customer</class>
<properties>
<!-- 連線資料庫基本資訊 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!-- 配置JPA實現產品的基本屬性,配置hibernate的基本屬性 -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
-新增jar包:
1.首先建立如下表:
2.實體類程式碼如下:
package com.wayne.helloworld;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
/*
* 如果不新增@Table註解,則表名與實體類名相同
*/
@Table(name="JPA_CUSTOMERS")
@Entity
public class Customer {
private Integer id;
private String lastName;
private String email;
private int age;
private Date createdDate;
private Date birthDay;
@Column(name="ID")
@TableGenerator(name="ID_GENERATOR",
table="JPA_ID_GENERATOR",
//橫向找到PK_NAME這一列
pkColumnName="PK_NAME",
pkColumnValue="CUSTOMER_ID",
//縱向找到PK_VALUE這一列
valueColumnName="PK_VALUE",
//增長值
allocationSize=100)
@GeneratedValue(strategy=GenerationType.TABLE,generator="ID_GENERATOR")
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="LAST_NAME",length=50,nullable=true)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//若屬性名與欄位名相同,則不需要填寫註解
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//精度時間戳
@Temporal(TemporalType.TIMESTAMP)
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Temporal(TemporalType.DATE)
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
//工具方法,不需要對映為某個資料庫表的一列
@Transient
public String getInfo(){
return "lastName:"+this.getLastName();
}
}
3.測試類程式碼如下:
package com.wayne.helloworld;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.建立EntitymanagerFactory
//是persistence.xml的persistence-unit標籤name屬性
String persistenceUnitName="jpa_1_helloworld";
EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory(persistenceUnitName);
//2.建立EntityManager
EntityManager entityManager=entityManagerFactory.createEntityManager();
//3.開啟事務
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//4.進行持久化操作
Customer customer=new Customer();
customer.setAge(22);
customer.setEmail("www.wayne.com");
customer.setLastName("wayne");
customer.setCreatedDate(new Date());
customer.setBirthDay(new Date());
entityManager.persist(customer);
//5.提交事務
transaction.commit();
//6.關閉EntityManager
entityManager.close();
//7.關閉EntitymanagerFactory
entityManagerFactory.close();
}
}
4.執行後資料庫結果: