Skip to content

Commit 03cf67b

Browse files
authored
BAEL-6556 - TupleTransformer and ResultListTransformer in Hibernate (#18791)
* experiments * bael-6556 - complete * bael-6556 fix line break at EOF
1 parent 004e230 commit 03cf67b

File tree

11 files changed

+414
-1
lines changed

11 files changed

+414
-1
lines changed

persistence-modules/hibernate6/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282
<maven.compiler.target>17</maven.compiler.target>
8383
</properties>
8484

85-
</project>
85+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.transformers;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.PropertySource;
6+
7+
@PropertySource("classpath:application-transformers.yaml")
8+
@SpringBootApplication(scanBasePackageClasses = TransformersApplication.class)
9+
public class TransformersApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(TransformersApplication.class, args);
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.transformers.dto;
2+
3+
import java.util.List;
4+
5+
public record DepartmentStudentsDto(String department, List<String> students) {
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.transformers.dto;
2+
3+
public record StudentDto(Long id, String name) {
4+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.transformers.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.ManyToOne;
7+
import jakarta.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "courses")
11+
public class Course {
12+
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
private String name;
17+
18+
@ManyToOne
19+
private Department department;
20+
21+
protected Course() {
22+
}
23+
24+
public Course(String name, Department department) {
25+
this.name = name;
26+
this.department = department;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
public Department getDepartment() {
46+
return department;
47+
}
48+
49+
public void setDepartment(Department department) {
50+
this.department = department;
51+
}
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.transformers.entity;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import jakarta.persistence.Entity;
7+
import jakarta.persistence.GeneratedValue;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.OneToMany;
10+
import jakarta.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "departments")
14+
public class Department {
15+
16+
@Id
17+
@GeneratedValue
18+
private Long id;
19+
private String name;
20+
21+
@OneToMany(mappedBy = "department")
22+
private List<Student> students = new ArrayList<>();
23+
24+
protected Department() {
25+
}
26+
27+
public Department(String name) {
28+
this.name = name;
29+
}
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public List<Student> getStudents() {
40+
return students;
41+
}
42+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.transformers.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.ManyToOne;
7+
import jakarta.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "enrollments")
11+
public class Enrollment {
12+
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
17+
@ManyToOne
18+
private Student student;
19+
20+
@ManyToOne
21+
private Course course;
22+
23+
protected Enrollment() {
24+
}
25+
26+
public Enrollment(Student student, Course course) {
27+
this.student = student;
28+
this.course = course;
29+
}
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public void setId(Long id) {
36+
this.id = id;
37+
}
38+
39+
public Student getStudent() {
40+
return student;
41+
}
42+
43+
public void setStudent(Student student) {
44+
this.student = student;
45+
}
46+
47+
public Course getCourse() {
48+
return course;
49+
}
50+
51+
public void setCourse(Course course) {
52+
this.course = course;
53+
}
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.transformers.entity;
2+
3+
4+
import org.hibernate.annotations.Cache;
5+
import org.hibernate.annotations.CacheConcurrencyStrategy;
6+
7+
import jakarta.persistence.Cacheable;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.GenerationType;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.SequenceGenerator;
13+
14+
@Entity
15+
@Cacheable
16+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
17+
public class PersonEntity {
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PERSON_SEQ")
21+
@SequenceGenerator(name = "PERSON_SEQ", sequenceName = "PERSON_SEQ", allocationSize = 100)
22+
private Long id;
23+
24+
private String name;
25+
private Long mobile;
26+
private String designation;
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public Long getMobile() {
33+
return mobile;
34+
}
35+
36+
public String getDesignation() {
37+
return designation;
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.transformers.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.ManyToOne;
7+
import jakarta.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "students")
11+
public class Student {
12+
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
private String name;
17+
18+
@ManyToOne
19+
private Department department;
20+
21+
protected Student() {
22+
}
23+
24+
public Student(String name, Department department) {
25+
this.name = name;
26+
this.department = department;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public Department getDepartment() {
38+
return department;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public void setDepartment(Department department) {
50+
this.department = department;
51+
}
52+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spring:
2+
config:
3+
name: application-transformers
4+
sql:
5+
init:
6+
mode: never
7+
datasource:
8+
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
9+
driverClassName: org.h2.Driver
10+
username: sa
11+
password:
12+
jpa:
13+
hibernate:
14+
ddl-auto: create-drop
15+
show-sql: true
16+
properties:
17+
hibernate:
18+
format_sql: true

0 commit comments

Comments
 (0)