18 June 2014

ID Generation (Table)


There are four identifier generation strategies (AUTO, IDENTITY, SEQUENCE and TABLE). Marking a field with the @GeneratedValue annotation specifies that a value will be automatically generated for that field.

Employee.java
package org.metadata.jee.jpa.entities;

import java.io.Serializable;

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;

@Entity
@Table(name = "EMP_TBL")
@TableGenerator(name = "EMP_GENTOR", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "EMP_GEN", allocationSize = 10)
public class Employee implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "EMP_GENTOR")
      private long id;
      @Column(name = "EMP_NAME")
      private String name;

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


EmployeeTest.java
package org.metadata.jee.jpa.entities;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EmployeeTest {
      private EntityManagerFactory emf;

      public EmployeeTest() {
            emf = Persistence.createEntityManagerFactory("METADATA_JPA");
      }

      public String insert() {
            EntityManager em = emf.createEntityManager();
            // Begin Transaction
            em.getTransaction().begin();

            Employee employee = new Employee();
            employee.setName("Mr Jone");

            // Insert employee to database
            em.persist(employee);

            // Commit Transaction
            em.getTransaction().commit();
            return "OK";
      }

      public static void main(String[] args) {
            EmployeeTest employeeTest = new EmployeeTest();
            String status = employeeTest.insert();
            if (status.equals("OK")) {
                  System.out.println("Insert Employee test is success.");
            }
      }
}


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="METADATA_JPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      <class>org.metadata.jee.jpa.entities.Employee</class>
    <properties>
      <property name="javax.persistence.jdbc.url"           value="jdbc:mysql://localhost:3306/metadatajpa"/>
      <property name="javax.persistence.jdbc.user" value="metadatajpa"/>
        <property name="javax.persistence.jdbc.password" value="metadatajpa"/>
      <property name="javax.persistence.jdbc.driver"       value="com.mysql.jdbc.Driver"/>     
    </properties>
  </persistence-unit>
</persistence>

create.sql
CREATE TABLE EMP_TBL (
      ID BIGINT NOT NULL,
      EMP_NAME VARCHAR(255), PRIMARY KEY (ID)
);
CREATE TABLE ID_GEN (GEN_NAME VARCHAR(50) NOT NULL, GEN_VAL DECIMAL(38), PRIMARY KEY (GEN_NAME));
INSERT INTO ID_GEN(GEN_NAME, GEN_VAL) values ('EMP_GEN', 0);


No comments:

Post a Comment

Like us on Facebook