LoginSignup
39
39

More than 5 years have passed since last update.

Hibernate Toolsを使ってJPAのEntityクラスを生成する

Last updated at Posted at 2016-11-19

概要

EclipseプラグインのHibernate Toolsを使ってJPAのEntityクラスを生成するための環境構築や設定方法のまとめです。

環境

  • Windows10 Professional
  • Java 1.8.0_101
  • Eclipse Neon
    • JBoss Tools 4.4.1.Final
    • MySQL Connector/J 5.1.40
  • MySQL 5.6

参考

必要なライブラリの準備

Hibernate ORM

上記のサイトよりアーカイブファイルをダウンロードし適当な場所へ展開しておきます。
今回使用したのはhibernate-release-5.2.4.Final.zipです。

MySQL Connector/J

上記のサイトよりアーカイブファイルをダウンロードし適当な場所へ展開しておきます。
今回使用したのはmysql-connector-java-5.1.40.zipです。

Eclipseの設定

JBoss Toolsのインストール

マーケットプレイスよりJBoss Tools 4.4.1.Finalをインストールします。

jboss_tool_1.png

インストールするのはHibernate Toolsだけで大丈夫です。

jboss_tool_2.png

Driver Definition

メニューバー → Preferences → Data Management → Connectivity → Driver Definitions

Add... をクリックします。

driver_definition_1.png

Vendor FilterにMySQLを選択し、driver templateにMySQL 5.1を指定します。

driver_definition_2.png

JAR Listタブで Add JAR/Zip... を押して先ほどダウンロードしたMySQL Connector/Jのjarを指定します。

driver_definition_3.png

Connection Profile

Data Source ExplorerビューのDatabase Connectionsを右クリックし、コンテキストメニューから"New..."を選択します。

Connection ProfileにMySQLを選択します。
Nameは"MySQL (sample_db)"としました。

connection_profile_1.png

JDBC Driverのpropertyを入力します。
ついでにTest Connectionで接続できるか確認も行います。

connection_profile_2.png

今回の設定内容です。

connection_profile_3.png

Hibernate

メニューバー → Preferences → Java → Build Path → User Libraries

New... をクリックします。

user_libraries_hibernate_1.png

nameに"hibernate"と入力しました。

user_libraries_hibernate_2.png

Add External JARs... をクリックして、先ほどダウンロードしたHibernateのJarを指定します。
指定するのは展開フォルダ下のlib/requireにあるjarファイル全てです。

user_libraries_hibernate_3.png

JPA Projectの作成

Entityクラスを生成するためのプロジェクトを作ります。
Project Explorerで右クリックし、コンテキストメニューからNew → Project... → JPA → JPA Projectを選択します。

jpa_project_1.png

Project nameは"sample_db"としました。この名前がpersistence-unitの名前にもなります。
その他の設定は図の通りです。

jpa_project_2.png

ここはそのまま Next > で進みます。

jpa_project_3.png

JPA implementationに"User Library"を選び、上記で登録した"hibernate"を指定します。
Connectionも上記で登録した"MySQL(sample_db)"を指定します。
Finish をクリックして登録を完了します。

jpa_project_4.png

JPA Projectの作成直後の状態です。

jpa_project_5.png

生成されたpersistence.xmlを下記のように修正します。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    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">
    <persistence-unit name="sample_db">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sample_db"/>
            <property name="hibernate.connection.username" value="test_user"/>
            <property name="hibernate.connection.password" value="test_user"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

プロジェクト名を右クリックしコンテキストメニューからBuild Path → Configure Build Path... → JPA → Entity Generationを選択し、Default packageを設定します。
今回は"com.example.model"としました。

jpa_project_6.png

プロジェクト名を右クリックしコンテキストメニューからNew → JPA Entities from Tablesを選択します。

jpa_project_7.png

データベース接続されていない場合は、図で示すアイコンをクリックしてデータベース接続を行います。

jpa_project_8.png

接続が成功すればテーブルの一覧が表示されるのでEntityクラスが必要なテーブルにチェックを入れます。

jpa_project_9.png

ここはそのまま Next > で進みます。

jpa_project_10.png

生成するEntityクラスのカスタマイズを行います。
ここではKey generatorに"identity"を指定しました。

jpa_project_11.png

Finish をクリックしてEntityクラスを生成します。

jpa_project_12.png

Entityクラスを生成した直後のJPA Projectの状態です。Entityクラスの他にpersistence.xmlの設定にも反映されています。

jpa_project_13.png

補足

使用するDB環境

この記事で使用するDB環境です。

データベース、ユーザー

create database if not exists sample_db character set utf8;

create user 'test_user'@'localhost' identified by 'test_user';

grant all on sample_db.* to 'test_user'@'localhost';

テーブル

customer

create table if not exists customer (
  id int not null auto_increment,
  customer_number int not null,
  customer_name varchar(124) not null,
  contact_last_name varchar(124) not null,
  contact_first_name varchar(124) not null,
  phone varchar(32),
  address_line1 varchar(124),
  address_line2 varchar(124),
  city varchar(32),
  state varchar(32),
  postal_code varchar(32),
  country varchar(32),
  sales_rep_employee_number int,
  credit_limit decimal(10,2),
  primary key (id)
) engine = INNODB;

生成されたEntityクラス(Customer)。getter/setterは省略しています。

Customer.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;

/**
 * The persistent class for the customer database table.
 * 
 */
@Entity
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="address_line1")
    private String addressLine1;

    @Column(name="address_line2")
    private String addressLine2;

    private String city;

    @Column(name="contact_first_name")
    private String contactFirstName;

    @Column(name="contact_last_name")
    private String contactLastName;

    private String country;

    @Column(name="credit_limit")
    private BigDecimal creditLimit;

    @Column(name="customer_name")
    private String customerName;

    @Column(name="customer_number")
    private int customerNumber;

    private String phone;

    @Column(name="postal_code")
    private String postalCode;

    @Column(name="sales_rep_employee_number")
    private int salesRepEmployeeNumber;

    private String state;

    public Customer() {
    }

}

orders

create table if not exists orders (
  id int not null auto_increment,
  order_number int not null,
  order_date date not null,
  required_date date not null,
  shipped_date date,
  status varchar(32),
  comments varchar(256),
  customer_number int,
  primary key (id)
) engine = INNODB;
Order.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;


/**
 * The persistent class for the orders database table.
 * 
 */
@Entity
@Table(name="orders")
@NamedQuery(name="Order.findAll", query="SELECT o FROM Order o")
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String comments;

    @Column(name="customer_number")
    private int customerNumber;

    @Temporal(TemporalType.DATE)
    @Column(name="order_date")
    private Date orderDate;

    @Column(name="order_number")
    private int orderNumber;

    @Temporal(TemporalType.DATE)
    @Column(name="required_date")
    private Date requiredDate;

    @Temporal(TemporalType.DATE)
    @Column(name="shipped_date")
    private Date shippedDate;

    private String status;

    public Order() {
    }

}

order_detail

create table if not exists order_detail (
  id int not null auto_increment,
  order_number int not null,
  product_code varchar(32) not null,
  quantity_ordered int,
  price_each decimal(10,2),
  order_line_number int,
  primary key (id)
) engine = INNODB;
OrderDetail.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
 * The persistent class for the order_detail database table.
 * 
 */
@Entity
@Table(name="order_detail")
@NamedQuery(name="OrderDetail.findAll", query="SELECT o FROM OrderDetail o")
public class OrderDetail implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="order_line_number")
    private int orderLineNumber;

    @Column(name="order_number")
    private int orderNumber;

    @Column(name="price_each")
    private BigDecimal priceEach;

    @Column(name="product_code")
    private String productCode;

    @Column(name="quantity_ordered")
    private int quantityOrdered;

    public OrderDetail() {
    }

}

payment

create table if not exists payment (
  id int not null auto_increment,
  customer_number int not null,
  check_number varchar(64) not null,
  payment_date date not null,
  amount decimal(10,2) not null,
  primary key (id)
) engine = INNODB;
Payment.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;


/**
 * The persistent class for the payment database table.
 * 
 */
@Entity
@NamedQuery(name="Payment.findAll", query="SELECT p FROM Payment p")
public class Payment implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private BigDecimal amount;

    @Column(name="check_number")
    private String checkNumber;

    @Column(name="customer_number")
    private int customerNumber;

    @Temporal(TemporalType.DATE)
    @Column(name="payment_date")
    private Date paymentDate;

    public Payment() {
    }

}

product

create table if not exists product (
  id int not null auto_increment,
  product_code varchar(32) not null,
  product_name varchar(128) not null,
  product_line varchar(32) not null,
  product_scale varchar(32) not null,
  product_vendor varchar(64) not null,
  product_description varchar(1024),
  quantity_in_stock int,
  buy_price decimal(10,2),
  msrp decimal(10,2),
  primary key (id)
) engine = INNODB;
Product.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
 * The persistent class for the product database table.
 * 
 */
@Entity
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="buy_price")
    private BigDecimal buyPrice;

    private BigDecimal msrp;

    @Column(name="product_code")
    private String productCode;

    @Column(name="product_description")
    private String productDescription;

    @Column(name="product_line")
    private String productLine;

    @Column(name="product_name")
    private String productName;

    @Column(name="product_scale")
    private String productScale;

    @Column(name="product_vendor")
    private String productVendor;

    @Column(name="quantity_in_stock")
    private int quantityInStock;

    public Product() {
    }

}

product_line

create table if not exists product_line (
  id int not null auto_increment,
  product_line varchar(32) not null,
  text_description varchar(1024),
  html_description varchar(1024),
  image varchar(1024),
  primary key (id)
) engine = INNODB;
ProductLine.java
package com.example.model;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the product_line database table.
 * 
 */
@Entity
@Table(name="product_line")
@NamedQuery(name="ProductLine.findAll", query="SELECT p FROM ProductLine p")
public class ProductLine implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="html_description")
    private String htmlDescription;

    private String image;

    @Column(name="product_line")
    private String productLine;

    @Column(name="text_description")
    private String textDescription;

    public ProductLine() {
    }

}
39
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39
39