Étant donné que j'écris en utilisant le temps d'intervalle, je vous serais reconnaissant si vous pouviez indiquer les parties avec une précision médiocre. Tout d'abord, excluez ceux qui sont EOL. Nous considérerons les fonctions payantes, mais nous ne les utiliserons pas réellement, en raison du problème des poches. La base de données utilisée est fixée à Postgre pour une raison stupide telle que "Eh bien, peut-être que Postgre peut être utilisé même s'il n'est pas spécifié."
ORM | Transaction | Data Model | DSL |
---|---|---|---|
○ | ○ | ※ | ○ |
○: Correspondance ×: non pris en charge *: Il existe une fonction pour créer des tables liées lorsqu'il existe une relation plusieurs-à-plusieurs.
Main.java
//Le premier tutoriel à recommander JPQL ... et ce n'est pas de type sécurisé ...
session.createQuery("from Employee").list().stream().forEach(o -> {
Employee e = (Employee)o;
Main.sysout(e);
});
//CriteriaBuilder est bien, mais c'est un tutoriel ...
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);
//Un tutoriel qui recommande JPQL même depuis EntityManager ……
em.createQuery("from Employee", Employee.class).getResultList().stream().forEach(Main::sysout);
//Après tout, Criteria Builder est bon, c'est un tutoriel ...
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);
}
Main.java
//version de session à utiliser
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 pour utiliser 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);
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>
N'est pas encore.
Recommended Posts