Add additional tests
This commit is contained in:
parent
1a1a6c22f1
commit
99ed18c228
|
@ -6,15 +6,7 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.orm.test.notfound;
|
package org.hibernate.orm.test.notfound;
|
||||||
|
|
||||||
import jakarta.persistence.ConstraintMode;
|
import org.hibernate.ObjectNotFoundException;
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import jakarta.persistence.FetchType;
|
|
||||||
import jakarta.persistence.ForeignKey;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.JoinColumn;
|
|
||||||
import jakarta.persistence.ManyToOne;
|
|
||||||
|
|
||||||
import org.hibernate.annotations.NotFound;
|
import org.hibernate.annotations.NotFound;
|
||||||
import org.hibernate.annotations.NotFoundAction;
|
import org.hibernate.annotations.NotFoundAction;
|
||||||
import org.hibernate.proxy.HibernateProxy;
|
import org.hibernate.proxy.HibernateProxy;
|
||||||
|
@ -26,6 +18,15 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import jakarta.persistence.ConstraintMode;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
|
import jakarta.persistence.ForeignKey;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.ManyToOne;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
@ -130,6 +131,39 @@ public class EagerProxyNotFoundTest {
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProxyInSessionEagerNonExistingLazyAssociation(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
final Task task = new Task();
|
||||||
|
task.id = 1;
|
||||||
|
session.persist( task );
|
||||||
|
|
||||||
|
session.flush();
|
||||||
|
|
||||||
|
session.createNativeQuery( "update Task set lazyEmployeeId = 6 where id = 1" )
|
||||||
|
.executeUpdate();
|
||||||
|
} );
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
final Task task = session.createQuery( "from Task", Task.class ).getSingleResult();
|
||||||
|
assertNotNull( task );
|
||||||
|
assertNull( task.employeeEagerNotFoundIgnore );
|
||||||
|
Employee employeeLazy = task.employeeLazy;
|
||||||
|
assertNotNull( employeeLazy );
|
||||||
|
assertTrue( HibernateProxy.class.isInstance( employeeLazy ) );
|
||||||
|
try {
|
||||||
|
employeeLazy.getId();
|
||||||
|
employeeLazy.getName();
|
||||||
|
fail( "ObjectNotFoundException should have been thrown because Task.employeeLazy.location is not found " +
|
||||||
|
"and is not mapped with @NotFound(IGNORE)" );
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException expected) {
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExistingProxyWithNonExistingAssociation(SessionFactoryScope scope) {
|
public void testExistingProxyWithNonExistingAssociation(SessionFactoryScope scope) {
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
|
@ -162,6 +196,39 @@ public class EagerProxyNotFoundTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExistingProxyWithNoAssociation(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
final Employee employee = new Employee();
|
||||||
|
employee.id = 1;
|
||||||
|
session.persist( employee );
|
||||||
|
|
||||||
|
final Task task = new Task();
|
||||||
|
task.id = 2;
|
||||||
|
task.employeeEagerNotFoundIgnore = employee;
|
||||||
|
session.persist( task );
|
||||||
|
session.persist( task );
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
try {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
session.load( Employee.class, 1 );
|
||||||
|
Task task = session.createQuery( "from Task", Task.class ).getSingleResult();
|
||||||
|
assertNotNull( task );
|
||||||
|
Employee employeeEagerNotFoundIgnore = task.getEmployeeEagerNotFoundIgnore();
|
||||||
|
assertNotNull( employeeEagerNotFoundIgnore );
|
||||||
|
assertNull( employeeEagerNotFoundIgnore.getLocation() );
|
||||||
|
assertNull( task.getEmployeeLazy() );
|
||||||
|
} );
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEnityWithNotExistingAssociation(SessionFactoryScope scope) {
|
public void testEnityWithNotExistingAssociation(SessionFactoryScope scope) {
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
|
@ -193,6 +260,36 @@ public class EagerProxyNotFoundTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnityWithNoAssociation(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
final Employee employee = new Employee();
|
||||||
|
employee.id = 1;
|
||||||
|
session.persist( employee );
|
||||||
|
|
||||||
|
final Task task = new Task();
|
||||||
|
task.id = 2;
|
||||||
|
task.employeeEagerNotFoundIgnore = employee;
|
||||||
|
session.persist( task );
|
||||||
|
|
||||||
|
session.flush();
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
try {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
Employee employee = session.createQuery( "from Employee", Employee.class )
|
||||||
|
.getSingleResult();
|
||||||
|
assertNotNull( employee );
|
||||||
|
assertNull( employee.getLocation() );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
catch (EntityNotFoundException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void deleteData(SessionFactoryScope scope) {
|
public void deleteData(SessionFactoryScope scope) {
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
|
@ -223,6 +320,14 @@ public class EagerProxyNotFoundTest {
|
||||||
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
|
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
|
||||||
)
|
)
|
||||||
private Employee employeeLazy;
|
private Employee employeeLazy;
|
||||||
|
|
||||||
|
public Employee getEmployeeEagerNotFoundIgnore() {
|
||||||
|
return employeeEagerNotFoundIgnore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee getEmployeeLazy() {
|
||||||
|
return employeeLazy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "Employee")
|
@Entity(name = "Employee")
|
||||||
|
@ -243,6 +348,14 @@ public class EagerProxyNotFoundTest {
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Location getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "Location")
|
@Entity(name = "Location")
|
||||||
|
|
Loading…
Reference in New Issue