Consideration on Java Persistence Framework 2017 (8) Hibernate5

Previous post

-Discussion on Java Persistence Framework for 2017 (1) -Consideration of 2017 Java Persistence Framework (2) Doma2 -Guessing about the 2017 Java Persistence Framework (3) Reladomo -Discussion on Java Persistence Framework for 2017 (4) jOOQ -Discussion on Java Persistence Framework for 2017 (5) Iciql -Discussion on Java Persistence Framework for 2017 (6) Ebean -Discussion on Java Persistence Framework 2017 (7) EclipseLink

Preface

Since I am writing using the gap time, I would appreciate it if you could point out any parts with poor accuracy. First, exclude those that are EOL. We will consider paid functions, but we will not actually use them, due to the problem of pockets. The DB used is fixed to Postgre for a stupid reason such as "Well, maybe Postgre can be used even if it is not specified."

environment

Table structure

employeeテーブルからpostテーブルにID紐づけ

Correspondence range

ORM Transaction Data Model DSL

○: Correspondence ×: Not supported *: If there is a many-to-many relationship, there is a function to create a related table.

Impressions

--There is no automatic generation of entities. --The configuration file is heavy, really heavy --I was at a loss because there is no one-to-many element directly under the class of the mapping file that existed until the old version. --It has moved to the child elements of collection elements such as bag and set, well, that's true.

sample

Single table search

Search all

Main.java


//The tutorial that recommends JPQL first ... and it's not type-safe ...
session.createQuery("from Employee").list().stream().forEach(o -> {
    Employee e = (Employee)o;
    Main.sysout(e);
});

//CriteriaBuilder is fine, but it's a tutorial ...
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
Root<Employee> root = query.from(Employee.class);
session.createQuery(query.select(root).select(root)).getResultList().stream().forEach(Main::sysout);

//Tutorial to recommend JPQL even from EntityManager ……
em.createQuery("from Employee", Employee.class).getResultList().stream().forEach(Main::sysout);

//After all Criteria Builder is good, it's a tutorial ...
CriteriaBuilder builder2 = em.getCriteriaBuilder();
CriteriaQuery<Employee> query2 = builder2.createQuery(Employee.class);
Root<Employee> root2 = query2.from(Employee.class);
em.createQuery(query2.select(root2).select(root2)).getResultList().stream().forEach(Main::sysout);

Main#sysout(Employee).java


public static void sysout(Employee e) {
    System.out.format("%1s: %2s, %3s, %4s\n", e.id, e.first_name, e.middle_name, e.last_name);
}

Primary key search

Main.java


//version to use session
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
Root<Employee> root = query.from(Employee.class);
session.createQuery(query.select(root).select(root).where(builder.equal(root.get(Employee_.id), 1L)))
        .getResultList()
        .stream()
        .forEach(Main::sysout);

//Version to use EntityManager
CriteriaBuilder builder2 = em.getCriteriaBuilder();
CriteriaQuery<Employee> query2 = builder2.createQuery(Employee.class);
Root<Employee> root2 = query2.from(Employee.class);
em.createQuery(query2.select(root2).select(root2).where(builder2.equal(root2.get(Employee_.id), 1L)))
        .getResultList()
        .stream()
        .forEach(Main::sysout);

Table join

Main.java


CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
Root<Employee> root = query.from(Employee.class);
session.createQuery(query.select(root).select(root).where(builder.equal(root.get(Employee_.id), 1L)))
        .getResultList()
        .stream()
        .peek(Main::sysout)
        .map(e -> e.posts)
        .flatMap(ps -> ps.stream())
        .forEach(Main::sysout);

CriteriaBuilder builder2 = em.getCriteriaBuilder();
CriteriaQuery<Employee> query2 = builder2.createQuery(Employee.class);
Root<Employee> root2 = query2.from(Employee.class);
em.createQuery(query2.select(root2).select(root2).where(builder2.equal(root2.get(Employee_.id), 0L)))
        .getResultList()
        .stream()
        .peek(Main::sysout)
        .map(e -> e.posts)
        .flatMap(ps -> ps.stream())
        .forEach(Main::sysout);

Main#sysout(Post).java


public static void sysout(Post p) {
    System.out.format("\t%1s: %2s, %3s\n", p.id, p.employee_id, p.name);
}

Employee.java


@Entity
@Table(name="employee")
public class Employee {
    @Id
    public long id;
    public String first_name;
    public String middle_name;
    public String last_name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "id", referencedColumnName = "employee_id", table = "post")
    public List<Post> posts = new ArrayList<>();

    public long getId() {return this.id;}
    public void setId(long id) {this.id = id;}
    public String getFirst_name() {return this.first_name;}
    public void setFirst_name(String first_name) {this.first_name = first_name;}
    public String getMiddle_name() {return this.middle_name;}
    public void setMiddle_name(String middle_name) {this.middle_name = middle_name;}
    public String getLast_name() {return this.last_name;}
    public void setLast_name(String last_name) {this.last_name = last_name;}
    public List<Post> getPosts() {return this.posts;}
    public void setPosts(List<Post> posts) {this.posts = posts;}
}

Post.java


@Entity
@Table(name = "post")
public class Post {
    @Id
    public long id;
    public long employee_id;
    public String name;

    public long getId() {return this.id;}
    public void setId(long id) {this.id = id;}
    public long getEmployee_id() {return this.employee_id;}
    public void setEmployee_id(long employee_id) {this.employee_id = employee_id;}
    public String getName() {return this.name;}
    public void setName(String name) {this.name = name;}
}

Employee.hbm.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="hibernate.models">
    <class name="Employee" table="employee">
        <id name="id" column="id"/>
        <property name="first_name" column="first_name"/>
        <property name="middle_name" column="middle_name"/>
        <property name="last_name" column="last_name"/>
        <bag name="posts" table="post">
            <key column="employee_id"/>
            <one-to-many class="Post"/>
        </bag>
    </class>
</hibernate-mapping>

Post.hbm.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="hibernate.models">
    <class name="Post" table="post">
        <id name="id" column="id"/>
        <property name="employee_id" column="employee_id"/>
        <property name="name" column="name"/>
    </class>
</hibernate-mapping>

The point

--Almost JPA implementation ――Since the setting is heavy, I feel that I can not operate with confidence without an expert. --You can issue DDL according to the mapping XML, of course you can choose, but it seems easy and scary ...

Addictive points

-"It's a JPA implementation, so it can be the same as EclipseLink." --It doesn't work --I didn't write mapping XML in the first place ―― "You can feel the same as JPA annotation, right?" --As mentioned above, one-to-many is not at the same level as the property, first you need to read DTD etc.

After post

Is not yet.

Reference article

-Hibernate Core Reference Guide

Recommended Posts

Consideration on Java Persistence Framework 2017 (8) Hibernate5
Consideration on Java Persistence Framework 2017 (Summary) -1
Consideration on Java Persistence Framework 2017 (6) Ebean
Consideration on Java Persistence Framework 2017 (5) Iciql
Consideration on Java Persistence Framework 2017 (7) EclipseLink
Consideration on the 2017 Java Persistence Framework (1)
Consideration on Java Persistence Framework 2017 (2) Doma2
java framework
Guess about the 2017 Java Persistence Framework (3) Reladomo
Java framework comparison
[Java] Collection framework
Play Framework2.5 (Java) Tips
Let's touch on Java
Install Java on Mac
Run PostgreSQL on Java
[Development] Java framework comparison
Learning Java framework # 1 (Mac version)
Java version control on macOS
Install OpenJDK7 (JAVA) on ubuntu 14.04
Downgrade Java on openSUSE Linux
Reflection on Java string manipulation
On passing Java Gold SE 8
Oracle Java 8 on Docker Ubuntu
Java Collections Framework Review Notes
Install Java on WSL Ubuntu 18.04
Run java applet on ubuntu
Put Oracle Java 8 on CircleCI 2
Java version change on CentOS
Install java 1.8.0 on Amazon linux2