HHH-15604 Test identically-named association in entity root and elementcollection of embeddables

This commit is contained in:
Yoann Rodière 2022-10-14 14:44:30 +02:00 committed by Christian Beikov
parent b5d58e69ae
commit cfc7b7ba66
6 changed files with 1113 additions and 0 deletions

View File

@ -0,0 +1,191 @@
package org.hibernate.orm.test.mapping.embeddable;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddableWithToOneAssociation2Test.Employee.class,
EmbeddableWithToOneAssociation2Test.ParkingSpot.class
}
)
@SessionFactory(
statementInspectorClass = SQLStatementInspector.class
)
public class EmbeddableWithToOneAssociation2Test {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee employee = new Employee( 1, "Fab" );
ParkingSpot parkingSpot = new ParkingSpot( 1, "park 1", employee );
ParkingSpot parkingSpot2 = new ParkingSpot( 2, "park 2", null );
employee.setParkingSpot( parkingSpot2 );
LocationDetails details = new LocationDetails( 1, parkingSpot );
employee.setLocation( details );
session.persist( employee );
session.persist( parkingSpot );
session.persist( parkingSpot2 );
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
session -> {
Employee employee = session.get( Employee.class, 1 );
assertThat( employee ).isNotNull();
ParkingSpot parkingSpot = employee.getLocation().getParkingSpot();
assertThat( parkingSpot ).isNotNull();
assertThat( parkingSpot.getAssignedTo() ).isEqualTo( employee );
assertThat( employee.getParkingSpot() ).isNotEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 3 );
}
);
statementInspector.clear();
scope.inTransaction(
session -> {
ParkingSpot parkingSpot = session.get( ParkingSpot.class, 1 );
assertThat( parkingSpot ).isNotNull();
Employee employee = parkingSpot.getAssignedTo();
assertThat( employee ).isNotNull();
assertThat( employee.getLocation().getParkingSpot() ).isEqualTo( parkingSpot );
assertThat( employee.getParkingSpot() ).isNotEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 2 );
assertThat( statementInspector.getNumberOfJoins( 1 ) ).isEqualTo( 3 );
}
);
}
@Entity(name = "Employee")
public static class Employee {
@Id
int id;
String name;
@ManyToOne
@JoinColumn(name = "parking_spot_id1")
ParkingSpot parkingSpot;
@Embedded
LocationDetails location;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public LocationDetails getLocation() {
return location;
}
public void setLocation(LocationDetails location) {
this.location = location;
}
public ParkingSpot getParkingSpot() {
return parkingSpot;
}
public void setParkingSpot(ParkingSpot parkingSpot) {
this.parkingSpot = parkingSpot;
}
}
@Embeddable
public static class LocationDetails {
int officeNumber;
@OneToOne
@JoinColumn(name = "parking_spot_id2")
ParkingSpot parkingSpot;
public LocationDetails() {
}
public LocationDetails(int officeNumber, ParkingSpot parkingSpot) {
this.officeNumber = officeNumber;
this.parkingSpot = parkingSpot;
}
public int getOfficeNumber() {
return officeNumber;
}
public ParkingSpot getParkingSpot() {
return parkingSpot;
}
}
@Entity(name = "ParkingSpot")
public static class ParkingSpot {
@Id
int id;
String garage;
@OneToOne(mappedBy = "location.parkingSpot")
Employee assignedTo;
public ParkingSpot() {
}
public ParkingSpot(int id, String garage, Employee assignedTo) {
this.id = id;
this.garage = garage;
this.assignedTo = assignedTo;
}
public int getId() {
return id;
}
public String getGarage() {
return garage;
}
public Employee getAssignedTo() {
return assignedTo;
}
}
}

View File

@ -0,0 +1,173 @@
package org.hibernate.orm.test.mapping.embeddable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddableWithToOneAssociation3Test.Employee.class,
EmbeddableWithToOneAssociation3Test.ParkingSpot.class
}
)
@SessionFactory(
statementInspectorClass = SQLStatementInspector.class
)
public class EmbeddableWithToOneAssociation3Test {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee employee = new Employee( 1, "Fab" );
ParkingSpot parkingSpot = new ParkingSpot( 1, "park 1", employee );
LocationDetails details = new LocationDetails( 1, parkingSpot );
employee.setLocation( details );
session.persist( employee );
session.persist( parkingSpot );
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
session -> {
Employee employee = session.get( Employee.class, 1 );
assertThat( employee ).isNotNull();
ParkingSpot parkingSpot = employee.getLocation().getParkingSpot();
assertThat( parkingSpot ).isNotNull();
assertThat( parkingSpot.getAssignedTo() ).isEqualTo( employee );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
statementInspector.clear();
scope.inTransaction(
session -> {
ParkingSpot parkingSpot = session.get( ParkingSpot.class, 1 );
assertThat( parkingSpot ).isNotNull();
Employee employee = parkingSpot.getAssignedTo();
assertThat( employee ).isNotNull();
assertThat( employee.getLocation().getParkingSpot() ).isEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
}
@Entity(name = "Employee")
public static class Employee {
@Id
int id;
String name;
@Embedded
LocationDetails location;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public LocationDetails getLocation() {
return location;
}
public void setLocation(LocationDetails location) {
this.location = location;
}
}
@Embeddable
public static class LocationDetails {
int officeNumber;
@OneToOne(mappedBy = "assignedTo")
ParkingSpot parkingSpot;
public LocationDetails() {
}
public LocationDetails(int officeNumber, ParkingSpot parkingSpot) {
this.officeNumber = officeNumber;
this.parkingSpot = parkingSpot;
}
public int getOfficeNumber() {
return officeNumber;
}
public ParkingSpot getParkingSpot() {
return parkingSpot;
}
}
@Entity(name = "ParkingSpot")
public static class ParkingSpot {
@Id
//int id;
Integer id;
String garage;
@OneToOne
Employee assignedTo;
public ParkingSpot() {
}
public ParkingSpot(int id, String garage, Employee assignedTo) {
this.id = id;
this.garage = garage;
this.assignedTo = assignedTo;
}
public int getId() {
return id;
}
public String getGarage() {
return garage;
}
public Employee getAssignedTo() {
return assignedTo;
}
}
}

View File

@ -0,0 +1,184 @@
package org.hibernate.orm.test.mapping.embeddable;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddableWithToOneAssociation4Test.Employee.class,
EmbeddableWithToOneAssociation4Test.ParkingSpot.class
}
)
@SessionFactory(
statementInspectorClass = SQLStatementInspector.class
)
public class EmbeddableWithToOneAssociation4Test {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee employee = new Employee( 1, "Fab" );
ParkingSpot parkingSpot = new ParkingSpot( 1, "park 1", employee );
LocationDetails details = new LocationDetails( 1, parkingSpot );
employee.setLocation( details );
session.persist( employee );
session.persist( parkingSpot );
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
session -> {
Employee employee = session.get( Employee.class, 1 );
assertThat( employee ).isNotNull();
ParkingSpot parkingSpot = employee.getLocation().getParkingSpot();
assertThat( parkingSpot ).isNotNull();
assertThat( parkingSpot.getDetails().getAssignedTo() ).isEqualTo( employee );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
statementInspector.clear();
scope.inTransaction(
session -> {
ParkingSpot parkingSpot = session.get( ParkingSpot.class, 1 );
assertThat( parkingSpot ).isNotNull();
Employee employee = parkingSpot.getDetails().getAssignedTo();
assertThat( employee ).isNotNull();
assertThat( employee.getLocation().getParkingSpot() ).isEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
}
@Entity(name = "Employee")
public static class Employee {
@Id
int id;
String name;
@Embedded
LocationDetails location;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public LocationDetails getLocation() {
return location;
}
public void setLocation(LocationDetails location) {
this.location = location;
}
}
@Embeddable
public static class LocationDetails {
int officeNumber;
@OneToOne(mappedBy = "details.assignedTo")
ParkingSpot parkingSpot;
public LocationDetails() {
}
public LocationDetails(int officeNumber, ParkingSpot parkingSpot) {
this.officeNumber = officeNumber;
this.parkingSpot = parkingSpot;
}
public int getOfficeNumber() {
return officeNumber;
}
public ParkingSpot getParkingSpot() {
return parkingSpot;
}
}
@Entity(name = "ParkingSpot")
public static class ParkingSpot {
@Id
//int id;
Integer id;
@Embedded
ParkingSpotDetails details;
public ParkingSpot() {
}
public ParkingSpot(int id, String garage, Employee assignedTo) {
this.id = id;
this.details = new ParkingSpotDetails(garage, assignedTo);
}
public int getId() {
return id;
}
public ParkingSpotDetails getDetails() {
return details;
}
}
@Embeddable
public static class ParkingSpotDetails {
String garage;
@OneToOne
Employee assignedTo;
public ParkingSpotDetails() {
}
public ParkingSpotDetails(String garage, Employee assignedTo) {
this.garage = garage;
this.assignedTo = assignedTo;
}
public String getGarage() {
return garage;
}
public Employee getAssignedTo() {
return assignedTo;
}
}
}

View File

@ -0,0 +1,167 @@
package org.hibernate.orm.test.mapping.embeddable;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddableWithToOneAssociationTest.Employee.class,
EmbeddableWithToOneAssociationTest.ParkingSpot.class
}
)
@SessionFactory(
statementInspectorClass = SQLStatementInspector.class
)
public class EmbeddableWithToOneAssociationTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee employee = new Employee( 1, "Fab" );
ParkingSpot parkingSpot = new ParkingSpot( 1, "park 1", employee );
LocationDetails details = new LocationDetails( 1, parkingSpot );
employee.setLocation( details );
session.persist( employee );
session.persist( parkingSpot );
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
session -> {
Employee employee = session.get( Employee.class, 1 );
assertThat( employee ).isNotNull();
ParkingSpot parkingSpot = employee.getLocation().getParkingSpot();
assertThat( parkingSpot ).isNotNull();
assertThat( parkingSpot.getAssignedTo() ).isEqualTo( employee );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
statementInspector.clear();
scope.inTransaction(
session -> {
ParkingSpot parkingSpot = session.get( ParkingSpot.class, 1 );
assertThat( parkingSpot ).isNotNull();
Employee employee = parkingSpot.getAssignedTo();
assertThat( employee ).isNotNull();
assertThat( employee.getLocation().getParkingSpot() ).isEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
}
@Entity(name = "Employee")
public static class Employee {
@Id
int id;
String name;
@Embedded
LocationDetails location;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public LocationDetails getLocation() {
return location;
}
public void setLocation(LocationDetails location) {
this.location = location;
}
}
@Embeddable
public static class LocationDetails {
int officeNumber;
@OneToOne
ParkingSpot parkingSpot;
public LocationDetails() {
}
public LocationDetails(int officeNumber, ParkingSpot parkingSpot) {
this.officeNumber = officeNumber;
this.parkingSpot = parkingSpot;
}
public int getOfficeNumber() {
return officeNumber;
}
public ParkingSpot getParkingSpot() {
return parkingSpot;
}
}
@Entity(name = "ParkingSpot")
public static class ParkingSpot {
@Id
int id;
String garage;
@OneToOne(mappedBy = "location.parkingSpot")
Employee assignedTo;
public ParkingSpot() {
}
public ParkingSpot(int id, String garage, Employee assignedTo) {
this.id = id;
this.garage = garage;
this.assignedTo = assignedTo;
}
public int getId() {
return id;
}
public String getGarage() {
return garage;
}
public Employee getAssignedTo() {
return assignedTo;
}
}
}

View File

@ -0,0 +1,191 @@
package org.hibernate.orm.test.mapping.embeddable.elementcollection;
import java.util.Set;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddableWithToOneAssociationTest.Employee.class,
EmbeddableWithToOneAssociationTest.ParkingSpot.class
}
)
@SessionFactory(
statementInspectorClass = SQLStatementInspector.class
)
public class EmbeddableWithToOneAssociationTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Employee employee = new Employee( 1, "Fab" );
ParkingSpot parkingSpot = new ParkingSpot( 1, "park 1", employee );
LocationDetails details = new LocationDetails( 1, parkingSpot );
employee.setLocation( details );
session.persist( employee );
session.persist( parkingSpot );
}
);
}
@Test
public void testGet(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
scope.inTransaction(
session -> {
Employee employee = session.get( Employee.class, 1 );
assertThat( employee ).isNotNull();
Set<ParkingSpot> parkingSpots = employee.getLocation().getParkingSpots();
assertThat( parkingSpots.size() ).isEqualTo( 1 );
assertThat( parkingSpots.iterator().next().getDetails().getAssignedTo() ).isEqualTo( employee );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
}
);
statementInspector.clear();
scope.inTransaction(
session -> {
ParkingSpot parkingSpot = session.get( ParkingSpot.class, 1 );
assertThat( parkingSpot ).isNotNull();
Employee employee = parkingSpot.getDetails().getAssignedTo();
assertThat( employee ).isNotNull();
assertThat( employee.getLocation().getParkingSpots().size() ).isEqualTo( 1 );
assertThat( employee.getLocation().getParkingSpots().iterator().next() ).isEqualTo( parkingSpot );
assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( statementInspector.getNumberOfJoins( 0 ) ).isEqualTo( 1 );
assertThat( statementInspector.getNumberOfJoins( 1 ) ).isEqualTo( 0 );
}
);
}
@Entity(name = "Employee")
public static class Employee {
@Id
int id;
String name;
@Embedded
LocationDetails location;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public LocationDetails getLocation() {
return location;
}
public void setLocation(LocationDetails location) {
this.location = location;
}
}
@Embeddable
public static class LocationDetails {
int officeNumber;
@OneToMany(mappedBy = "details.assignedTo", fetch = FetchType.EAGER)
Set<ParkingSpot> parkingSpots;
public LocationDetails() {
}
public LocationDetails(int officeNumber, ParkingSpot parkingSpot) {
this.officeNumber = officeNumber;
this.parkingSpots = Set.of( parkingSpot );
}
public int getOfficeNumber() {
return officeNumber;
}
public Set<ParkingSpot> getParkingSpots() {
return parkingSpots;
}
}
@Entity(name = "ParkingSpot")
public static class ParkingSpot {
@Id
//int id;
Integer id;
@Embedded
ParkingSpotDetails details;
public ParkingSpot() {
}
public ParkingSpot(int id, String garage, Employee assignedTo) {
this.id = id;
this.details = new ParkingSpotDetails(garage, assignedTo);
}
public int getId() {
return id;
}
public ParkingSpotDetails getDetails() {
return details;
}
}
@Embeddable
public static class ParkingSpotDetails {
String garage;
@ManyToOne
Employee assignedTo;
public ParkingSpotDetails() {
}
public ParkingSpotDetails(String garage, Employee assignedTo) {
this.garage = garage;
this.assignedTo = assignedTo;
}
public String getGarage() {
return garage;
}
public Employee getAssignedTo() {
return assignedTo;
}
}
}

View File

@ -0,0 +1,207 @@
package org.hibernate.orm.test.mapping.embeddable.elementcollection;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.OrderColumn;
import static org.assertj.core.api.Assertions.assertThat;
@DomainModel(
annotatedClasses = {
EmbeddedElementCollectionWithIdenticallyNamedAssociationTest.EntityA.class,
EmbeddedElementCollectionWithIdenticallyNamedAssociationTest.EntityB.class,
EmbeddedElementCollectionWithIdenticallyNamedAssociationTest.EmbeddableB.class
}
)
@SessionFactory
@TestForIssue(jiraKey = "HHH-15604")
public class EmbeddedElementCollectionWithIdenticallyNamedAssociationTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
EntityA a1 = new EntityA();
a1.setId( 1 );
EntityB b1 = new EntityB();
b1.setId( 1 );
b1.setIdenticallyNamedAssociation( a1 );
a1.setB( b1 );
EntityA a2 = new EntityA();
a2.setId( 2 );
EntityB b2 = new EntityB();
b2.setId( 2 );
b2.setIdenticallyNamedAssociation( a2 );
a2.setB( b2 );
EmbeddableB embeddableB = new EmbeddableB();
embeddableB.setIdenticallyNamedAssociation( a2 );
b1.getElementCollection().add( embeddableB );
scope.inTransaction( session -> {
session.persist( a1 );
session.persist( a2 );
session.persist( b1 );
session.persist( b2 );
} );
assertThat( b1.getIdenticallyNamedAssociation() ).isEqualTo( a1 );
assertThat( b1.getElementCollection() ).hasSize( 1 );
assertThat( b1.getElementCollection().get( 0 ).getIdenticallyNamedAssociation() ).isEqualTo( a2 );
assertThat( a2.getB() ).isEqualTo( b2 );
}
@Test
public void testGetEntityA(SessionFactoryScope scope) {
scope.inTransaction( session -> {
EntityA a1 = session.get( EntityA.class, 1 );
EntityB b1 = a1.getB();
assertThat( b1.getId() ).isEqualTo( 1 );
assertThat( b1.getElementCollection() ).hasSize( 1 );
assertThat( b1.getIdenticallyNamedAssociation()).isEqualTo( a1 );
EntityA identicallyNamedAssociation = b1.getElementCollection().get( 0 ).getIdenticallyNamedAssociation();
assertThat( identicallyNamedAssociation.getId() ).isEqualTo( 2 );
EntityB b = identicallyNamedAssociation.getB();
assertThat( b.getId() ).isEqualTo( 2 );
assertThat( b.getElementCollection().size()).isEqualTo( 0 );
} );
}
@Test
public void testGetEntities(SessionFactoryScope scope) {
scope.inTransaction( session -> {
EntityA a1 = session.get( EntityA.class, 1 );
EntityA a2 = session.get( EntityA.class, 2 );
EntityB b1 = session.get( EntityB.class, 1 );
EntityB b2 = session.get( EntityB.class, 2 );
assertThat( a1 ).isNotNull();
assertThat( a2 ).isNotNull();
assertThat( b1 ).isNotNull();
assertThat( b1.getIdenticallyNamedAssociation() ).isEqualTo( a1 );
assertThat( a1.getB() ).isEqualTo( b1 );
assertThat( b1.getElementCollection() ).hasSize( 1 );
EntityA identicallyNamedAssociation = b1.getElementCollection().get( 0 ).getIdenticallyNamedAssociation();
assertThat( identicallyNamedAssociation ).isEqualTo( a2 );
assertThat( identicallyNamedAssociation.getB() ).isEqualTo( b2 );
} );
}
@Entity(name = "entityA")
public static class EntityA {
@Id
private Integer id;
private String name;
@OneToOne(mappedBy = "identicallyNamedAssociation", fetch = FetchType.EAGER)
private EntityB b;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public EntityB getB() {
return b;
}
public void setB(EntityB b) {
this.b = b;
}
@Override
public String toString() {
return "EntityA{" +
"id=" + id +
'}';
}
}
@Entity(name = "entityB")
public static class EntityB {
@Id
private Integer id;
private String name;
@OneToOne
@JoinColumn(name = "entityA_id")
private EntityA identicallyNamedAssociation;
@ElementCollection(fetch = FetchType.EAGER)
@OrderColumn(name = "idx")
@CollectionTable(name = "elemcollect")
private List<EmbeddableB> elementCollection = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public EntityA getIdenticallyNamedAssociation() {
return identicallyNamedAssociation;
}
public void setIdenticallyNamedAssociation(EntityA a) {
this.identicallyNamedAssociation = a;
}
public List<EmbeddableB> getElementCollection() {
return elementCollection;
}
@Override
public String toString() {
return "EntityB{" +
"id=" + id +
", identicallyNamedAssociation=" + identicallyNamedAssociation +
", elementCollection=" + elementCollection +
'}';
}
}
@Embeddable
public static class EmbeddableB {
@OneToOne
@JoinColumn(name = "emb_entityA_id")
private EntityA identicallyNamedAssociation;
public EntityA getIdenticallyNamedAssociation() {
return identicallyNamedAssociation;
}
public void setIdenticallyNamedAssociation(EntityA a) {
this.identicallyNamedAssociation = a;
}
}
}