JPA ID表生成器示例

2018-02-23 15:07 更新

JPA教程 - JPA ID表生成器示例


我們可以使用一個表作為id生成表。

以下代碼使用@TableGenerator創(chuàng)建表并設置id值的初始值。

然后它使用表來在@GeneratedValue注釋中生成值。

@TableGenerator(
   name = "Address_Gen", 
   table = "ID_GEN", 
   pkColumnName = "GEN_NAME", 
   valueColumnName = "GEN_VAL", 
   pkColumnValue = "Addr_Gen", 
   initialValue = 10000, 
   allocationSize = 100)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen")
private long id;

例子

下面的代碼來自PersonDaoImpl.java。

package cn.w3cschool.common;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test(){
    Person p1 = new Person("Tom");
    
    p1.setName("Tom");
    
    Department d = new Department();
    d.setName("Design");
    p1.setDepartment(d);
    
    em.persist(p1);
    em.persist(d);
  }
  @PersistenceContext
  private EntityManager em;
}

下面的代碼來自Person.java。

package cn.w3cschool.common;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;

@Entity
public class Person {
  @TableGenerator(name = "Address_Gen", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100)
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen")

  private long id;
   
  private String name;
  @OneToOne
  private Department department;
  
  public Person() {}

  public Person(String name) {
    this.name = name;
  }


  public Department getDepartment() {
    return department;
  }

  public void setDepartment(Department department) {
    this.department = department;
  }

  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;
  }

  @Override
  public String toString() {
    return "Person [id=" + id + ", name=" + name + "]";
  }

}

以下代碼來自App.java。

package cn.w3cschool.common;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        "applicationContext.xml");
    PersonDaoImpl dao = (PersonDaoImpl) context.getBean("personDao");

    dao.test();

    context.close();
    
    Helper.checkData();
  }
}
下載 ID_Table_Generator.zip

以下是數據庫表轉儲。

Table Name: ID_GEN
 Row:
    Column Name: GEN_NAME,
    Column Type: VARCHAR:
    Column Value: Addr_Gen

    Column Name: GEN_VAL,
    Column Type: INTEGER:
    Column Value: 1





Table Name: PERSON
 Row:
    Column Name: ID,
    Column Type: BIGINT:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: Tom

    Column Name: DEPARTMENT_ID,
    Column Type: BIGINT:
    Column Value: 1



以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號