HHH-14943 Add test for issue
This commit is contained in:
parent
60bd1c657b
commit
62ff89414b
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.orm.test.annotations.naturalid;
|
||||
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
import org.hibernate.annotations.NaturalIdCache;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.hibernate.testing.orm.junit.Setting;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
@DomainModel(
|
||||
annotatedClasses = {
|
||||
NaturalIdOnSingleManyToOneTest.NaturalIdOnManyToOne.class,
|
||||
NaturalIdOnSingleManyToOneTest.State.class,
|
||||
NaturalIdOnSingleManyToOneTest.Citizen.class,
|
||||
}
|
||||
)
|
||||
|
||||
@SessionFactory
|
||||
@ServiceRegistry(settings = @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"))
|
||||
@TestForIssue(jiraKey = "HHH-14943")
|
||||
public class NaturalIdOnSingleManyToOneTest {
|
||||
|
||||
@AfterEach
|
||||
public void cleanupData(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.createMutationQuery( "delete NaturalIdOnManyToOne" ).executeUpdate();
|
||||
session.createMutationQuery( "delete Citizen" ).executeUpdate();
|
||||
session.createMutationQuery( "delete State" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManyToOneNaturalLoadByNaturalId(SessionFactoryScope scope) {
|
||||
NaturalIdOnManyToOne singleManyToOne1 = new NaturalIdOnManyToOne();
|
||||
NaturalIdOnManyToOne singleManyToOne2 = new NaturalIdOnManyToOne();
|
||||
|
||||
Citizen c1 = new Citizen();
|
||||
c1.setFirstname( "Emmanuel" );
|
||||
c1.setLastname( "Bernard" );
|
||||
c1.setSsn( "1234" );
|
||||
|
||||
State france = new State();
|
||||
france.setName( "Ile de France" );
|
||||
c1.setState( france );
|
||||
|
||||
singleManyToOne1.setCitizen( c1 );
|
||||
singleManyToOne2.setCitizen( null );
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.persist( france );
|
||||
session.persist( c1 );
|
||||
session.persist( singleManyToOne1 );
|
||||
session.persist( singleManyToOne2 );
|
||||
}
|
||||
);
|
||||
|
||||
scope.getSessionFactory().getCache().evictNaturalIdData(); // we want to go to the database
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
NaturalIdOnManyToOne instance1 = session.byNaturalId( NaturalIdOnManyToOne.class )
|
||||
.using( "citizen", c1 )
|
||||
.load();
|
||||
assertNotNull( instance1 );
|
||||
assertNotNull( instance1.getCitizen() );
|
||||
|
||||
NaturalIdOnManyToOne instance2 = session.byNaturalId( NaturalIdOnManyToOne.class )
|
||||
.using( "citizen", null ).load();
|
||||
|
||||
assertNotNull( instance2 );
|
||||
assertNull( instance2.getCitizen() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Entity(name = "Citizen")
|
||||
@NaturalIdCache
|
||||
public static class Citizen {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
@NaturalId
|
||||
@ManyToOne
|
||||
private State state;
|
||||
@NaturalId
|
||||
private String ssn;
|
||||
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer 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;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getSsn() {
|
||||
return ssn;
|
||||
}
|
||||
|
||||
public void setSsn(String ssn) {
|
||||
this.ssn = ssn;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "State")
|
||||
public static class State {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "NaturalIdOnManyToOne")
|
||||
@NaturalIdCache
|
||||
public static class NaturalIdOnManyToOne {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
int id;
|
||||
|
||||
@NaturalId
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
Citizen citizen;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Citizen getCitizen() {
|
||||
return citizen;
|
||||
}
|
||||
|
||||
public void setCitizen(Citizen citizen) {
|
||||
this.citizen = citizen;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,12 +20,15 @@ import org.hibernate.query.Query;
|
|||
import org.hibernate.stat.Statistics;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Test case for NaturalId annotation
|
||||
|
@ -135,6 +138,56 @@ public class NaturalIdTest extends BaseCoreFunctionalTestCase {
|
|||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManyToOneNaturalLoadByNaturalId() {
|
||||
NaturalIdOnManyToOne singleManyToOne1 = new NaturalIdOnManyToOne();
|
||||
NaturalIdOnManyToOne singleManyToOne2 = new NaturalIdOnManyToOne();
|
||||
|
||||
Citizen c1 = new Citizen();
|
||||
c1.setFirstname( "Emmanuel" );
|
||||
c1.setLastname( "Bernard" );
|
||||
c1.setSsn( "1234" );
|
||||
|
||||
State france = new State();
|
||||
france.setName( "Ile de France" );
|
||||
c1.setState( france );
|
||||
|
||||
singleManyToOne1.setCitizen( c1 );
|
||||
singleManyToOne2.setCitizen( null );
|
||||
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.persist( france );
|
||||
session.persist( c1 );
|
||||
session.persist( singleManyToOne1 );
|
||||
session.persist( singleManyToOne2 );
|
||||
}
|
||||
);
|
||||
|
||||
inSession(
|
||||
session -> {
|
||||
session.getSessionFactory().getCache().evictNaturalIdData(); // we want to go to the database
|
||||
session.beginTransaction();
|
||||
try {
|
||||
NaturalIdOnManyToOne instance1 = session.byNaturalId( NaturalIdOnManyToOne.class )
|
||||
.using( "citizen", c1 )
|
||||
.load();
|
||||
Assertions.assertNotNull( instance1 );
|
||||
Assertions.assertNotNull( instance1.getCitizen() );
|
||||
|
||||
NaturalIdOnManyToOne instance2 = session.byNaturalId( NaturalIdOnManyToOne.class )
|
||||
.using( "citizen", null ).load();
|
||||
|
||||
Assertions.assertNotNull( instance2 );
|
||||
assertNull( instance2.getCitizen() );
|
||||
}
|
||||
finally {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdLoaderCached() {
|
||||
Statistics stats = sessionFactory().getStatistics();
|
||||
|
|
Loading…
Reference in New Issue