Skip to content

Commit e2efbf9

Browse files
committed
JAVA-41259 Moved code of article jpa-unique-constraints from java-jpa-3 to java-jpa
1 parent 8729227 commit e2efbf9

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

persistence-modules/java-jpa-3/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ This module contains articles about the Java Persistence API (JPA) in Java.
1111
- [A Guide to MultipleBagFetchException in Hibernate](https://www.baeldung.com/java-hibernate-multiplebagfetchexception)
1212
- [How to Convert a Hibernate Proxy to a Real Entity Object](https://www.baeldung.com/hibernate-proxy-to-real-entity-object)
1313
- [How to Return Multiple Entities in JPA Query](https://www.baeldung.com/jpa-return-multiple-entities)
14-
- [Defining Unique Constraints in JPA](https://www.baeldung.com/jpa-unique-constraints)
1514
- [Connecting to a Specific Schema in JDBC](https://www.baeldung.com/jdbc-connect-to-schema)

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,7 @@
9797
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
9898
</properties>
9999
</persistence-unit>
100-
101-
<persistence-unit name="jpa-unique-constraints">
102-
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
103-
<class>com.baeldung.jpa.uniqueconstraints.Person</class>
104-
<class>com.baeldung.jpa.uniqueconstraints.Address</class>
105-
<exclude-unlisted-classes>true</exclude-unlisted-classes>
106-
<properties>
107-
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver" />
108-
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:test" />
109-
<property name="jakarta.persistence.jdbc.user" value="sa" />
110-
<property name="jakarta.persistence.jdbc.password" value="" />
111-
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
112-
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
113-
<property name="show_sql" value="true" />
114-
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
115-
</properties>
116-
</persistence-unit>
100+
117101
<persistence-unit name="jpa-h2-return-multiple-entities">
118102
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
119103
<class>com.baeldung.jpa.returnmultipleentities.Channel</class>

persistence-modules/java-jpa/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This module contains articles about the Java Persistence API (JPA) in Java.
1212
- [JPA Query Parameters Usage](https://www.baeldung.com/jpa-query-parameters)
1313
- [Default Column Values in JPA](https://www.baeldung.com/jpa-default-column-values)
1414
- [Returning an Auto-Generated Id with JPA](https://www.baeldung.com/jpa-get-auto-generated-id)
15+
- [Defining Unique Constraints in JPA](https://www.baeldung.com/jpa-unique-constraints)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,22 @@
8888
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
8989
</properties>
9090
</persistence-unit>
91+
92+
<persistence-unit name="jpa-unique-constraints">
93+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
94+
<class>com.baeldung.jpa.uniqueconstraints.Person</class>
95+
<class>com.baeldung.jpa.uniqueconstraints.Address</class>
96+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
97+
<properties>
98+
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver" />
99+
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:test" />
100+
<property name="jakarta.persistence.jdbc.user" value="sa" />
101+
<property name="jakarta.persistence.jdbc.password" value="" />
102+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
103+
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
104+
<property name="show_sql" value="true" />
105+
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
106+
</properties>
107+
</persistence-unit>
91108

92109
</persistence>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import java.util.Optional;
44

5-
import jakarta.persistence.EntityManager;
6-
import jakarta.persistence.EntityManagerFactory;
7-
import jakarta.persistence.Persistence;
8-
9-
import org.hibernate.exception.ConstraintViolationException;
5+
import org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException;
106
import org.junit.Assert;
117
import org.junit.jupiter.api.BeforeAll;
128
import org.junit.jupiter.api.Test;
139

10+
import jakarta.persistence.EntityManager;
11+
import jakarta.persistence.EntityManagerFactory;
12+
import jakarta.persistence.Persistence;
13+
1414
public class UniqueColumnIntegrationTest {
1515

1616
private static EntityManagerFactory factory;
@@ -44,7 +44,7 @@ public void whenPersistPersonWithSameNumber_thenConstraintViolationException() {
4444
} catch (Exception ex) {
4545
Assert.assertTrue(Optional.of(ex)
4646
.map(Throwable::getCause)
47-
.filter(x -> x instanceof ConstraintViolationException)
47+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
4848
.isPresent());
4949
} finally {
5050
entityManager.getTransaction().rollback();
@@ -73,7 +73,7 @@ public void whenPersistPersonWithSameEmail_thenConstraintViolationException() {
7373
} catch (Exception ex) {
7474
Assert.assertTrue(Optional.of(ex)
7575
.map(Throwable::getCause)
76-
.filter(x -> x instanceof ConstraintViolationException)
76+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
7777
.isPresent());
7878
} finally {
7979
entityManager.getTransaction().rollback();
@@ -107,7 +107,7 @@ public void whenPersistPersonWithSameAddress_thenConstraintViolationException()
107107
} catch (Exception ex) {
108108
Assert.assertTrue(Optional.of(ex)
109109
.map(Throwable::getCause)
110-
.filter(x -> x instanceof ConstraintViolationException)
110+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
111111
.isPresent());
112112
} finally {
113113
entityManager.getTransaction().rollback();
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import java.util.Optional;
44

5-
import jakarta.persistence.EntityManager;
6-
import jakarta.persistence.EntityManagerFactory;
7-
import jakarta.persistence.Persistence;
8-
9-
import org.hibernate.exception.ConstraintViolationException;
5+
import org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException;
106
import org.junit.Assert;
117
import org.junit.jupiter.api.BeforeAll;
128
import org.junit.jupiter.api.Test;
139

10+
import jakarta.persistence.EntityManager;
11+
import jakarta.persistence.EntityManagerFactory;
12+
import jakarta.persistence.Persistence;
13+
1414
public class UniqueConstraintIntegrationTest {
1515
private static EntityManagerFactory factory;
1616
private static EntityManager entityManager;
@@ -43,7 +43,7 @@ public void whenPersistPersonWithSameNumberAndStatus_thenConstraintViolationExce
4343
} catch (Exception ex) {
4444
Assert.assertTrue(Optional.of(ex)
4545
.map(Throwable::getCause)
46-
.filter(x -> x instanceof ConstraintViolationException)
46+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
4747
.isPresent());
4848
} finally {
4949
entityManager.getTransaction().rollback();
@@ -72,7 +72,7 @@ public void whenPersistPersonWithSameSCodeAndDecode_thenConstraintViolationExcep
7272
} catch (Exception ex) {
7373
Assert.assertTrue(Optional.of(ex)
7474
.map(Throwable::getCause)
75-
.filter(x -> x instanceof ConstraintViolationException)
75+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
7676
.isPresent());
7777
} finally {
7878
entityManager.getTransaction().rollback();
@@ -104,7 +104,7 @@ public void whenPersistPersonWithSameNumberAndAddress_thenConstraintViolationExc
104104
} catch (Exception ex) {
105105
Assert.assertTrue(Optional.of(ex)
106106
.map(Throwable::getCause)
107-
.filter(x -> x instanceof ConstraintViolationException)
107+
.filter(x -> x instanceof JdbcSQLIntegrityConstraintViolationException)
108108
.isPresent());
109109
} finally {
110110
entityManager.getTransaction().rollback();

0 commit comments

Comments
 (0)