-Discussion on Java Persistence Framework for 2017 (1)
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."
ORM | Transaction | Data Model | DSL |
---|---|---|---|
○ | ○ | × | × |
○: Correspondence ×: Not supported
SelectByIdTest.java
EmployeeDao dao = new EmployeeDaoImpl();
AppConfig.singleton().getTransactionManager().required(() -> {
Employee e1 = dao.selectById(BigDecimal.ZERO);
});
SelectByFirstNameAsStreamTest.java
EmployeeDao dao = new EmployeeDaoImpl();
AppConfig.singleton().getTransactionManager().required(() -> {
// EmployeeDao#selectByFirstNameAsStream(String)Is a manually added signature
Employee e1 = dao.selectByFirstNameAsStream("John");
});
EmployeeDao.java
//Argument names and parameters used in the SQL file must have the same name
@Select
Stream<Employee> selectByFirstNameAsStream(String first_name);
META-INF/~/EmployeeDao/selectByFirstNameAsStream.sql
select
/*%expand*/*
from
employee
where
first_name = /* first_name */1
SelectByFirstNameForCountTest.java
EmployeeDao dao = new EmployeeDaoImpl();
AppConfig.singleton().getTransactionManager().required(() -> {
System.out.println(
// EmployeeDao#selectByFirstNameForCount(String, Function<Stream<Employee>, AtomicInteger>)Is a manually added signature
dao.selectByFirstNameForCount(
"Taro",
e -> new AtomicInteger((int)e.count())));
});
EmployeeDao.java
//I didn't think it wouldn't accept IntFunction
@Select(strategy = SelectType.STREAM)
AtomicInteger selectByFirstNameForCount(String first_name, Function<Stream<Employee>, AtomicInteger> mapper);
META-INF/~/EmployeeDao/selectByFirstNameForCountTest.sql
select
/*%expand*/*
from
employee
where
first_name = /* first_name */1
EmployeeWithPost.java
@Entity(naming = NamingType.SNAKE_LOWER_CASE)
public class EmployeeWithPost {
/** */
@Id
@Column(name = "id")
BigDecimal id;
/** */
@Column(name = "first_name")
String firstName;
/** */
@Column(name = "middle_name")
String middleName;
/** */
@Column(name = "last_name")
String lastName;
/** */
@Id
@Column(name = "post_id")
BigDecimal postId;
/** */
@Column(name = "name")
String name;
public void setId(BigDecimal id) {
this.id = id;
}
public BigDecimal getId() {
return this.id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getMiddleName() {
return this.middleName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setPostId(BigDecimal postId) {
this.postId = postId;
}
public BigDecimal getPostId() {
return this.postId;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "EmployeeWithPost["
+ this.id
+ ":"
+ this.firstName.trim()
+ "/"
+ this.middleName.trim()
+ "/"
+ this.lastName.trim()
+ " "
+ this.postId
+ "-"
+ this.name.trim()
+ "]";
}
}
EmployeeWithPostDao.java
@Dao(config = ORMConfig.class)
public interface EmployeeWithPostDao {
@Select
public List<EmployeeWithPost> selectByEmployeeId(BigDecimal employeeId);
}
META-INF/(package name)/EmployeeWithPostDao/selectByEmployeeId.sql
select
employee.id AS id,
employee.first_name AS first_name,
employee.middle_name AS middle_name,
employee.last_name AS last_name,
post.id AS post_id,
post.name AS name
from
employee
INNER JOIN
post
ON
post.employee_id = employee.id
where
employee_id = /* employeeId */1
@ Singleton
.@ Singleton
, use the instance obtained by ʻAppConfig.singleton ()`@Singleton
, use the instance obtained bynew AppConfig ()
--Sample is with @ Singleton
, created is without @ Singleton
, test code is as sample, result did not work
--The reason why it didn't work is because TransactionManager is managed in AppConfig.@ Singleton
with the code without @ Singleton
, DAO and AppConfig will be separate transactions from the above specifications.-Guessing about the 2017 Java Persistence Framework (3) Reladomo -Discussion on Java Persistence Framework for 2017 (4) jOOQ
-10 things you value in the development of Doma
Recommended Posts