Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Mar 25, 2022
1 parent 1a1a6c2 commit 99ed18c
Showing 1 changed file with 123 additions and 10 deletions.
Expand Up @@ -6,15 +6,7 @@
*/
package org.hibernate.orm.test.notfound;

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 org.hibernate.ObjectNotFoundException;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.proxy.HibernateProxy;
Expand All @@ -26,6 +18,15 @@
import org.junit.jupiter.api.AfterEach;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -36,7 +37,7 @@
/**
* @author Gail Badner
*/
@JiraKey( "HHH-14537" )
@JiraKey("HHH-14537")
@DomainModel(
annotatedClasses = {
EagerProxyNotFoundTest.Task.class,
Expand Down Expand Up @@ -130,6 +131,39 @@ public void testProxyInSessionEagerIgnoreLazyProxy(SessionFactoryScope scope) {
} );
}

@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
public void testExistingProxyWithNonExistingAssociation(SessionFactoryScope scope) {
scope.inTransaction(
Expand Down Expand Up @@ -162,6 +196,39 @@ public void testExistingProxyWithNonExistingAssociation(SessionFactoryScope scop
}
}

@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
public void testEnityWithNotExistingAssociation(SessionFactoryScope scope) {
scope.inTransaction(
Expand Down Expand Up @@ -193,6 +260,36 @@ public void testEnityWithNotExistingAssociation(SessionFactoryScope scope) {
}
}

@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
public void deleteData(SessionFactoryScope scope) {
scope.inTransaction(
Expand Down Expand Up @@ -223,6 +320,14 @@ public static class Task {
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
)
private Employee employeeLazy;

public Employee getEmployeeEagerNotFoundIgnore() {
return employeeEagerNotFoundIgnore;
}

public Employee getEmployeeLazy() {
return employeeLazy;
}
}

@Entity(name = "Employee")
Expand All @@ -243,6 +348,14 @@ public int getId() {
public void setId(int id) {
this.id = id;
}

public Location getLocation() {
return location;
}

public String getName() {
return name;
}
}

@Entity(name = "Location")
Expand Down

0 comments on commit 99ed18c

Please sign in to comment.