Skip to content

Commit dcc27bc

Browse files
authored
Bael 7565 (#16857)
* could not determine recommended JDBCType * add versioning property
1 parent 0a27016 commit dcc27bc

File tree

9 files changed

+432
-0
lines changed

9 files changed

+432
-0
lines changed

persistence-modules/java-jpa-4/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<artifactId>jakarta.persistence-api</artifactId>
3131
<version>${jakarta.persistence-api.version}</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-databind</artifactId>
36+
<version>${fasterxml.jackson.version}</version>
37+
</dependency>
38+
3339
</dependencies>
3440

3541
<build>
@@ -51,6 +57,7 @@
5157
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
5258
<h2.version>2.1.214</h2.version>
5359
<jakarta.persistence-api.version>3.1.0</jakarta.persistence-api.version>
60+
<fasterxml.jackson.version>2.17.0</fasterxml.jackson.version>
5461
</properties>
5562

5663
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.EnumType;
6+
import jakarta.persistence.Enumerated;
7+
import jakarta.persistence.GeneratedValue;
8+
import jakarta.persistence.GenerationType;
9+
import jakarta.persistence.Id;
10+
11+
@Entity
12+
public class Address {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
private String street;
18+
19+
// Constructors, getters, and setters
20+
public Address() {}
21+
22+
public Address(String street) {
23+
this.street = street;
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
public String getStreet() {
34+
return street;
35+
}
36+
37+
public void setStreet(String street) {
38+
this.street = street;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
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.GenerationType;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.OneToMany;
11+
12+
@Entity
13+
public class Department {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
private String name;
18+
19+
@OneToMany(mappedBy = "department")
20+
private List<EmployeeWithAnnotation> employees = new ArrayList<>();
21+
public Department() {}
22+
23+
public Department(String name) {
24+
this.name = name;
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public List<EmployeeWithAnnotation> getEmployees() {
44+
return employees;
45+
}
46+
47+
public void setEmployees(List<EmployeeWithAnnotation> employees) {
48+
this.employees = employees;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
2+
3+
import java.time.LocalDate;
4+
5+
import jakarta.persistence.Entity;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.GenerationType;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Temporal;
10+
import jakarta.persistence.TemporalType;
11+
12+
@Entity
13+
public class Employee {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id;
18+
19+
private String name;
20+
21+
private Address address;
22+
23+
private Department department;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public Address getAddress() {
42+
return address;
43+
}
44+
45+
public void setAddress(Address address) {
46+
this.address = address;
47+
}
48+
49+
public Department getDepartment() {
50+
return department;
51+
}
52+
53+
public void setDepartment(Department department) {
54+
this.department = department;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.ManyToOne;
8+
import jakarta.persistence.OneToOne;
9+
10+
@Entity
11+
public class EmployeeWithAnnotation {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private Long id;
16+
17+
private String name;
18+
19+
@OneToOne
20+
private Address address;
21+
22+
@ManyToOne
23+
private Department department;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public Address getAddress() {
42+
return address;
43+
}
44+
45+
public void setAddress(Address address) {
46+
this.address = address;
47+
}
48+
49+
public Department getDepartment() {
50+
return department;
51+
}
52+
53+
public void setDepartment(Department department) {
54+
this.department = department;
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
2+
3+
import java.util.Map;
4+
5+
import jakarta.persistence.Entity;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.GenerationType;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class Student {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private Long id;
16+
17+
18+
private Map<String, String> studentDetails;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public Map<String, String> getStudentDetails() {
29+
return studentDetails;
30+
}
31+
32+
public void setStudentDetails(Map<String, String> studentDetails) {
33+
this.studentDetails = studentDetails;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.jpa.undeterminejdbctype;
2+
3+
import java.util.Map;
4+
5+
import org.hibernate.annotations.JdbcTypeCode;
6+
import org.hibernate.type.SqlTypes;
7+
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.GenerationType;
11+
import jakarta.persistence.Id;
12+
13+
@Entity
14+
public class StudentWithJson {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
20+
@JdbcTypeCode(SqlTypes.JSON)
21+
private Map<String, String> studentDetails;
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public Map<String, String> getStudentDetails() {
32+
return studentDetails;
33+
}
34+
35+
public void setStudentDetails(Map<String, String> studentDetails) {
36+
this.studentDetails = studentDetails;
37+
}
38+
}

persistence-modules/java-jpa-4/src/main/resources/META-INF/persistence.xml

+85
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,89 @@
5959
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
6060
</properties>
6161
</persistence-unit>
62+
63+
<persistence-unit name="jpa-undeterminejdbctype-complextype">
64+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
65+
<class>com.baeldung.jpa.undeterminejdbctype.Student</class>
66+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
67+
<properties>
68+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
69+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
70+
<property name="javax.persistence.jdbc.user" value="sa"/>
71+
<property name="javax.persistence.jdbc.password" value=""/>
72+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
73+
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
74+
<property name="hibernate.format_sql" value="true"/>
75+
<property name="hibernate.show_sql" value="true"/>
76+
</properties>
77+
</persistence-unit>
78+
79+
<persistence-unit name="jpa-undeterminejdbctype-complextypewithJSON">
80+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
81+
<class>com.baeldung.jpa.undeterminejdbctype.StudentWithJson</class>
82+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
83+
<properties>
84+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
85+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
86+
<property name="javax.persistence.jdbc.user" value="sa"/>
87+
<property name="javax.persistence.jdbc.password" value=""/>
88+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
89+
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
90+
<property name="hibernate.format_sql" value="true"/>
91+
<property name="hibernate.show_sql" value="true"/>
92+
</properties>
93+
</persistence-unit>
94+
95+
<persistence-unit name="jpa-undeterminejdbctype-relationship">
96+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
97+
<class>com.baeldung.jpa.undeterminejdbctype.Employee</class>
98+
<class>com.baeldung.jpa.undeterminejdbctype.EmployeeWithAnnotation</class>
99+
<class>com.baeldung.jpa.undeterminejdbctype.Address</class>
100+
<class>com.baeldung.jpa.undeterminejdbctype.Department</class>
101+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
102+
<properties>
103+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
104+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
105+
<property name="javax.persistence.jdbc.user" value="sa"/>
106+
<property name="javax.persistence.jdbc.password" value=""/>
107+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
108+
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
109+
<property name="hibernate.format_sql" value="true"/>
110+
<property name="hibernate.show_sql" value="true"/>
111+
</properties>
112+
</persistence-unit>
113+
114+
<persistence-unit name="jpa-undeterminejdbctype-annotatedrelationship">
115+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
116+
<class>com.baeldung.jpa.undeterminejdbctype.Address</class>
117+
<class>com.baeldung.jpa.undeterminejdbctype.EmployeeWithAnnotation</class>
118+
<class>com.baeldung.jpa.undeterminejdbctype.Department</class>
119+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
120+
<properties>
121+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
122+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
123+
<property name="javax.persistence.jdbc.user" value="sa"/>
124+
<property name="javax.persistence.jdbc.password" value=""/>
125+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
126+
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
127+
<property name="hibernate.format_sql" value="true"/>
128+
<property name="hibernate.show_sql" value="true"/>
129+
</properties>
130+
</persistence-unit>
131+
132+
<persistence-unit name="jpa-undeterminejdbctype-enum">
133+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
134+
<class>com.baeldung.jpa.undeterminejdbctype.Address</class>
135+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
136+
<properties>
137+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
138+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
139+
<property name="javax.persistence.jdbc.user" value="sa"/>
140+
<property name="javax.persistence.jdbc.password" value=""/>
141+
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
142+
<property name="hibernate.hbm2ddl.auto" value="none"/>
143+
<property name="hibernate.format_sql" value="true"/>
144+
<property name="hibernate.show_sql" value="true"/>
145+
</properties>
146+
</persistence-unit>
62147
</persistence>

0 commit comments

Comments
 (0)