値の自動生成

今回は値の自動生成についてです。

【前提条件】

[環境]
[その他]
  • JPAの設定が完了している*1

【概要】

値を自動生成する場合は
javax.persistence.GeneratedValueアノテーションを使用します。

【サンプルコード】

[テーブル]

今回は以前作成した「user_authority」テーブルを使います。

CREATE TABLE user_authority
(
    seq_number serial NOT NULL,
    auth_user_id character varying(10) NOT NULL,
    authority_type integer,
    CONSTRAINT authority_primary_key PRIMARY KEY (seq_number )
);

ALTER TABLE
    user_authority
ALTER COLUMN seq_number SET DEFAULT nextval('user_authority_seq_number_seq'::regclass);


CREATE SEQUENCE user_authority_seq_number_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

seq_numberカラムにはPostgreSQLのシーケンスを使用しています。

[Entity]

エンティティのクラスです。

package jp.glory.persistence.entity;

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;

@Entity
@Table(name = "user_authority")
public class UserAuthority implements Serializable {

    private static final long serialVersionUID = 1625221760062551052L;

    @Id
    @Column(name = "seq_number")
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_authority_seq_number_seq")
    private int seqNumber = 0;

    @Column(name = "auth_user_id")
    private String authUserId = null;

    @Column(name = "authority_type")
    private int authorityType = 0;

    // アクセサメソッドは省略
}

GeneratedValueアノテーションには
strategy属性とgenerator属性を指定します。

strategy属性はEnum
javax.persistence.GenerationTypeクラスの値を指定します。

GenerationTypeにはTABLE、SEQUENCE、IDENTITY、AUTOの四つが定義されています。

TABLEはシーケンス用テーブルを使用する場合に指定します。
SEQUENCEはシーケンスを使用する場合に指定します。
IDENTITYはID列を使用する場合に指定します。
AUTOはJPAで値を生成する場合にしています。

サンプルではPKにシーケンスを使用していますが、
SEQUENCEではなく、IDENTITYを指定しています。

SEQUENCEではうまく動かなかったので、
PKに使用している場合はIDENTITY、
PKに使用していない場合はSEQUENCEを使用するようです。

generator属性には値を生成するジェネレータを指定します。
シーケンスを使用している場合はシーケンス名を指定します。

[persistence.xml]

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="blog" transaction-type="JTA">
        <jta-data-source>jdbc/blog</jta-data-source>
        <class>jp.glory.persistence.entity.UserAuthority</class>
    </persistence-unit>
</persistence>
[EJB]

EJBのクラスです。
サンプルではUserAuthorityクラスのListを受け取り、登録を行います。

package jp.glory.application;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import jp.glory.persistence.entity.UserAuthority;

@Stateless
public class UserEntryApplication {

    @PersistenceContext(unitName = "blog")
    private EntityManager manager = null;

    public void entryAuthority(final List<UserAuthority> authorityList) {

        for (final UserAuthority authority : authorityList) {

            manager.persist(authority);
        }
    }
}
[登録クラス]

登録を行うクラスです。
ManagedBean上でフラグがオンだったら
UserAuthorityのオブジェクトを作成します。

フラグのオンオフは画面上のチェックボックスで制御します。

package jp.glory.ui;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import jp.glory.application.UserEntryApplication;
import jp.glory.persistence.entity.UserAuthority;

@ManagedBean(name = "userEntryPage")
@RequestScoped
public class UserEntryPage {

    @EJB
    private UserEntryApplication application = null;

    private String userId = null;

    private String name = null;

    private boolean authority1 = false;

    private boolean authority2 = false;

    private boolean authority3 = false;

    private boolean authority4 = false;

    public void entry() {

        final List<UserAuthority> authorityList = new ArrayList<>();
        if (authority1) {
            authorityList.add(createAuthority(1));
        }

        if (authority2) {
            authorityList.add(createAuthority(2));
        }

        if (authority3) {
            authorityList.add(createAuthority(3));
        }

        if (authority4) {
            authorityList.add(createAuthority(4));
        }

        application.entryAuthority(authorityList);

        userId = null;
        name = null;
        authority1 = false;
        authority2 = false;
        authority3 = false;
        authority4 = false;
    }

    private UserAuthority createAuthority(final int authorityType) {

        final UserAuthority authority = new UserAuthority();
        authority.setAuthorityType(authorityType);
        authority.setAuthUserId(userId);

        return authority;
    }

    // アクセサメソッドは省略
[ページ]

登録用のページです。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">
    <f:view>
        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>登録サンプル</title>
        </h:head>
        <h:body>
            <h:form id="entryForm">
                <div>
                    ユーザID:<h:inputText id="userId" value="#{userEntryPage.userId}" />
                </div>
                <div>
                    ユーザ名:<h:inputText id="name" value="#{userEntryPage.name}" />
                </div>
                <div>
                    権限1:<h:selectBooleanCheckbox value="#{userEntryPage.authority1}" />
                </div>
                <div>
                    権限2:<h:selectBooleanCheckbox value="#{userEntryPage.authority2}" />
                </div>
                <div>
                    権限3:<h:selectBooleanCheckbox value="#{userEntryPage.authority3}" />
                </div>
                <div>
                    権限4:<h:selectBooleanCheckbox value="#{userEntryPage.authority4}" />
                </div>
                <div>
                    <h:commandButton action="#{userEntryPage.entry()}" value="登録" />
                </div>
            </h:form>
        </h:body>
    </f:view>
</html>

【実行】

作成したページはで全部の項目に値を入力します。

登録ボタンをクリックすると↓のようなデータが登録されます。

seq_numberカラムには連番が振られていることがわかります。