BAEL-4723 Defining Indexes in JPA (#10247)
* BAEL-4723 Defining Indexes in JPA * unit -> integration test Co-authored-by: mateusz.szablak <mateusz.szablak@accenture.com>
This commit is contained in:
parent
7881b8e9c2
commit
9bce552d6d
|
@ -0,0 +1,70 @@
|
|||
package com.baeldung.jpa.index;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Index;
|
||||
import javax.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(indexes = {
|
||||
@Index(columnList = "firstName"),
|
||||
@Index(name = "fn_index", columnList = "id"),
|
||||
@Index(name = "multiIndex1", columnList = "firstName, lastName"),
|
||||
@Index(name = "multiIndex2", columnList = "lastName, firstName"),
|
||||
@Index(name = "multiSortIndex", columnList = "firstName, lastName DESC"),
|
||||
@Index(name = "uniqueIndex", columnList = "firstName", unique = true),
|
||||
@Index(name = "uniqueMultiIndex", columnList = "firstName, lastName", unique = true)
|
||||
})
|
||||
public class Student implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Student student = (Student) o;
|
||||
return Objects.equals(id, student.id) &&
|
||||
Objects.equals(firstName, student.firstName) &&
|
||||
Objects.equals(lastName, student.lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, firstName, lastName);
|
||||
}
|
||||
}
|
|
@ -40,4 +40,19 @@
|
|||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
<persistence-unit name="jpa-index">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.index.Student</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
||||
<property name="javax.persistence.jdbc.user" value="sa" />
|
||||
<property name="javax.persistence.jdbc.password" value="" />
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
<property name="show_sql" value="true" />
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.hibernate.SQL" level="DEBUG" />
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.jpa.index;
|
||||
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.util.Optional;
|
||||
|
||||
public class IndexIntegrationTest {
|
||||
private static EntityManagerFactory factory;
|
||||
private static EntityManager entityManager;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
factory = Persistence.createEntityManagerFactory("jpa-index");
|
||||
entityManager = factory.createEntityManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStudent_whenPersistStudentWithSameFirstName_thenConstraintViolationException() {
|
||||
Student student = new Student();
|
||||
student.setFirstName("FirstName");
|
||||
student.setLastName("LastName");
|
||||
|
||||
Student student2 = new Student();
|
||||
student2.setFirstName("FirstName");
|
||||
student2.setLastName("LastName2");
|
||||
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(student);
|
||||
entityManager.getTransaction().commit();
|
||||
|
||||
Assert.assertEquals(1L, (long) student.getId());
|
||||
|
||||
entityManager.getTransaction().begin();
|
||||
try {
|
||||
entityManager.persist(student2);
|
||||
entityManager.getTransaction().commit();
|
||||
Assert.fail("Should raise an exception - unique key violation");
|
||||
} catch (Exception ex) {
|
||||
Assert.assertTrue(Optional.of(ex).map(Throwable::getCause).map(Throwable::getCause).filter(x -> x instanceof ConstraintViolationException).isPresent());
|
||||
} finally {
|
||||
entityManager.getTransaction().rollback();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue