Re-enabled more tests
This commit is contained in:
parent
468989a0f3
commit
843813cd5a
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* 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.lazyload;
|
||||||
|
|
||||||
|
import org.hibernate.Hibernate;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
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.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
Parent.class,
|
||||||
|
Child.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
@ServiceRegistry(
|
||||||
|
settings = {
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = Environment.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = AvailableSettings.JTA_PLATFORM, value = "org.hibernate.testing.jta.TestingJtaPlatformImpl"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = "javax.persistence.transactionType", value = "JTA"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = AvailableSettings.JTA_PLATFORM, value = "Atomikos"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public class AtomikosJtaLazyLoadingTest {
|
||||||
|
|
||||||
|
private static final int CHILDREN_SIZE = 3;
|
||||||
|
private Long parentID;
|
||||||
|
private Long lastChildID;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void prepareTest(SessionFactoryScope scope)
|
||||||
|
throws Exception {
|
||||||
|
scope.inTransaction( session -> {
|
||||||
|
Parent p = new Parent();
|
||||||
|
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
||||||
|
final Child child = p.makeChild();
|
||||||
|
session.persist( child );
|
||||||
|
lastChildID = child.getId();
|
||||||
|
}
|
||||||
|
session.persist( p );
|
||||||
|
parentID = p.getId();
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-7971")
|
||||||
|
public void testLazyCollectionLoadingAfterEndTransaction(SessionFactoryScope scope) {
|
||||||
|
Parent loadedParent = scope.fromTransaction(
|
||||||
|
session ->
|
||||||
|
session.load( Parent.class, parentID )
|
||||||
|
);
|
||||||
|
|
||||||
|
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for ( Child child : loadedParent.getChildren() ) {
|
||||||
|
i++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, i );
|
||||||
|
|
||||||
|
Child loadedChild = scope.fromTransaction(
|
||||||
|
session ->
|
||||||
|
session.load( Child.class, lastChildID )
|
||||||
|
);
|
||||||
|
|
||||||
|
Parent p = loadedChild.getParent();
|
||||||
|
int j = 0;
|
||||||
|
for ( Child child : p.getChildren() ) {
|
||||||
|
j++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, j );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.lazyload;
|
package org.hibernate.orm.test.lazyload;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
|
@ -0,0 +1,110 @@
|
||||||
|
/*
|
||||||
|
* 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.lazyload;
|
||||||
|
|
||||||
|
import org.hibernate.Hibernate;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
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.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Oleksander Dukhno
|
||||||
|
*/
|
||||||
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
Parent.class,
|
||||||
|
Child.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
@ServiceRegistry(
|
||||||
|
settings = {
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = Environment.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = AvailableSettings.JTA_PLATFORM, value = "org.hibernate.testing.jta.TestingJtaPlatformImpl"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"
|
||||||
|
),
|
||||||
|
@ServiceRegistry.Setting(
|
||||||
|
name = "javax.persistence.transactionType", value = "jta"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public class JtaLazyLoadingTest {
|
||||||
|
|
||||||
|
private static final int CHILDREN_SIZE = 3;
|
||||||
|
private Long parentID;
|
||||||
|
private Long lastChildID;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void prepareTest(SessionFactoryScope scope)
|
||||||
|
throws Exception {
|
||||||
|
Parent p = new Parent();
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
||||||
|
final Child child = p.makeChild();
|
||||||
|
session.persist( child );
|
||||||
|
lastChildID = child.getId();
|
||||||
|
}
|
||||||
|
session.persist( p );
|
||||||
|
parentID = p.getId();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-7971")
|
||||||
|
public void testLazyCollectionLoadingAfterEndTransaction(SessionFactoryScope scope) {
|
||||||
|
Parent loadedParent = scope.fromTransaction(
|
||||||
|
session ->
|
||||||
|
session.load( Parent.class, parentID )
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for ( Child child : loadedParent.getChildren() ) {
|
||||||
|
i++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, i );
|
||||||
|
|
||||||
|
Child loadedChild = scope.fromTransaction(
|
||||||
|
session ->
|
||||||
|
session.load( Child.class, lastChildID )
|
||||||
|
);
|
||||||
|
|
||||||
|
Parent p = loadedChild.getParent();
|
||||||
|
int j = 0;
|
||||||
|
for ( Child child : p.getChildren() ) {
|
||||||
|
j++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, j );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,12 +4,11 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.lazyload;
|
package org.hibernate.orm.test.lazyload;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.OneToOne;
|
import javax.persistence.OneToOne;
|
||||||
|
@ -18,29 +17,32 @@ import org.hibernate.LazyInitializationException;
|
||||||
import org.hibernate.internal.AbstractSharedSessionContract;
|
import org.hibernate.internal.AbstractSharedSessionContract;
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.orm.junit.DomainModel;
|
||||||
import org.junit.Test;
|
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||||
|
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vlad Mihalcea
|
* @author Vlad Mihalcea
|
||||||
*/
|
*/
|
||||||
public class LazyLoadingLoggingTest
|
@DomainModel(
|
||||||
extends BaseCoreFunctionalTestCase {
|
annotatedClasses = {
|
||||||
|
LazyLoadingLoggingTest.Client.class,
|
||||||
|
LazyLoadingLoggingTest.Address.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
public class LazyLoadingLoggingTest {
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
@BeforeEach
|
||||||
return new Class<?>[] {
|
public void setUp(SessionFactoryScope scope) {
|
||||||
Client.class,
|
scope.inTransaction( session -> {
|
||||||
Address.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void afterSessionFactoryBuilt() {
|
|
||||||
doInHibernate( this::sessionFactory, session -> {
|
|
||||||
Address address = new Address();
|
Address address = new Address();
|
||||||
address.setId( 1L );
|
address.setId( 1L );
|
||||||
address.setStreet( "Marea albastra" );
|
address.setStreet( "Marea albastra" );
|
||||||
|
@ -54,12 +56,23 @@ public class LazyLoadingLoggingTest
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
session.createQuery( "delete from Client" ).executeUpdate();
|
||||||
|
session.createQuery( "delete from Address" ).executeUpdate();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-12484")
|
@TestForIssue(jiraKey = "HHH-12484")
|
||||||
public void testNoSession() {
|
public void testNoSession(SessionFactoryScope scope) {
|
||||||
Address address = doInHibernate( this::sessionFactory, s -> {
|
Address address = scope.fromTransaction(
|
||||||
return s.load( Address.class, 1L );
|
session ->
|
||||||
} );
|
session.load( Address.class, 1L )
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
address.getClient().getName();
|
address.getClient().getName();
|
||||||
|
@ -68,7 +81,7 @@ public class LazyLoadingLoggingTest
|
||||||
catch (LazyInitializationException expected) {
|
catch (LazyInitializationException expected) {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"could not initialize proxy " +
|
"could not initialize proxy " +
|
||||||
"[org.hibernate.test.lazyload.LazyLoadingLoggingTest$Address#1] " +
|
"[org.hibernate.orm.test.lazyload.LazyLoadingLoggingTest$Address#1] " +
|
||||||
"- no Session",
|
"- no Session",
|
||||||
expected.getMessage()
|
expected.getMessage()
|
||||||
);
|
);
|
||||||
|
@ -77,30 +90,27 @@ public class LazyLoadingLoggingTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-12484")
|
@TestForIssue(jiraKey = "HHH-12484")
|
||||||
public void testDisconnect() {
|
public void testDisconnect(SessionFactoryScope scope) {
|
||||||
doInHibernate( this::sessionFactory, session -> {
|
scope.inTransaction(
|
||||||
Address address = session.load( Address.class, 1L );
|
session -> {
|
||||||
AbstractSharedSessionContract sessionContract = (AbstractSharedSessionContract) session;
|
Address address = session.load( Address.class, 1L );
|
||||||
sessionContract.getJdbcCoordinator().close();
|
AbstractSharedSessionContract sessionContract = (AbstractSharedSessionContract) session;
|
||||||
|
sessionContract.getJdbcCoordinator().close();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
address.getClient().getName();
|
address.getClient().getName();
|
||||||
fail( "Should throw LazyInitializationException" );
|
fail( "Should throw LazyInitializationException" );
|
||||||
}
|
}
|
||||||
catch (LazyInitializationException expected) {
|
catch (LazyInitializationException expected) {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"could not initialize proxy " +
|
"could not initialize proxy " +
|
||||||
"[org.hibernate.test.lazyload.LazyLoadingLoggingTest$Address#1] " +
|
"[org.hibernate.orm.test.lazyload.LazyLoadingLoggingTest$Address#1] " +
|
||||||
"- the owning Session is disconnected",
|
"- the owning Session is disconnected",
|
||||||
expected.getMessage()
|
expected.getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
session.getTransaction().markRollbackOnly();
|
session.getTransaction().markRollbackOnly();
|
||||||
} );
|
} );
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean rebuildSessionFactoryOnError() {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "Address")
|
@Entity(name = "Address")
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* 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.lazyload;
|
||||||
|
|
||||||
|
import org.hibernate.LazyInitializationException;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
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.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikolay Golubev
|
||||||
|
*/
|
||||||
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
Parent.class,
|
||||||
|
Child.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
@ServiceRegistry(settings = @ServiceRegistry.Setting(name = Environment.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"))
|
||||||
|
public class LazyLoadingNotFoundTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-11179")
|
||||||
|
public void testNonExistentLazyInitOutsideTransaction(SessionFactoryScope scope) {
|
||||||
|
Child loadedChild = scope.fromTransaction(
|
||||||
|
session -> session.load( Child.class, -1L )
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
loadedChild.getParent();
|
||||||
|
fail( "lazy init did not fail on non-existent proxy" );
|
||||||
|
}
|
||||||
|
catch (LazyInitializationException e) {
|
||||||
|
assertNotNull( e.getMessage() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,291 @@
|
||||||
|
/*
|
||||||
|
* 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.lazyload;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
|
import org.hibernate.Hibernate;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
|
||||||
|
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.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Oleksander Dukhno
|
||||||
|
*/
|
||||||
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
Parent.class,
|
||||||
|
Child.class,
|
||||||
|
LazyLoadingTest.Client.class,
|
||||||
|
LazyLoadingTest.Address.class,
|
||||||
|
LazyLoadingTest.Account.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
@ServiceRegistry(
|
||||||
|
settings = {
|
||||||
|
@ServiceRegistry.Setting(name = Environment.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"),
|
||||||
|
@ServiceRegistry.Setting(name = Environment.USE_SECOND_LEVEL_CACHE, value = "false"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public class LazyLoadingTest {
|
||||||
|
|
||||||
|
private static final int CHILDREN_SIZE = 3;
|
||||||
|
private Long parentID;
|
||||||
|
private Long lastChildID;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void prepareTest(SessionFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
Parent p = new Parent();
|
||||||
|
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
||||||
|
final Child child = p.makeChild();
|
||||||
|
session.persist( child );
|
||||||
|
lastChildID = child.getId();
|
||||||
|
}
|
||||||
|
session.persist( p );
|
||||||
|
parentID = p.getId();
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-7971")
|
||||||
|
public void testLazyCollectionLoadingAfterEndTransaction(SessionFactoryScope scope) {
|
||||||
|
Parent loadedParent = scope.fromTransaction(
|
||||||
|
session ->
|
||||||
|
session.load( Parent.class, parentID )
|
||||||
|
);
|
||||||
|
|
||||||
|
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for ( Child child : loadedParent.getChildren() ) {
|
||||||
|
i++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, i );
|
||||||
|
|
||||||
|
Child loadedChild = scope.fromTransaction(
|
||||||
|
sesison ->
|
||||||
|
sesison.load( Child.class, lastChildID )
|
||||||
|
);
|
||||||
|
|
||||||
|
Parent p = loadedChild.getParent();
|
||||||
|
int j = 0;
|
||||||
|
for ( Child child : p.getChildren() ) {
|
||||||
|
j++;
|
||||||
|
assertNotNull( child );
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals( CHILDREN_SIZE, j );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-11838")
|
||||||
|
public void testGetIdOneToOne(SessionFactoryScope scope) {
|
||||||
|
final Object clientId = scope.fromTransaction(
|
||||||
|
session -> {
|
||||||
|
Address address = new Address();
|
||||||
|
session.save( address );
|
||||||
|
Client client = new Client( address );
|
||||||
|
return session.save( client );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
final Long addressId = scope.fromTransaction(
|
||||||
|
session -> {
|
||||||
|
Client client = session.get( Client.class, clientId );
|
||||||
|
Address address = client.getAddress();
|
||||||
|
address.getId();
|
||||||
|
assertThat( Hibernate.isInitialized( address ), is( true ) );
|
||||||
|
address.getStreet();
|
||||||
|
assertThat( Hibernate.isInitialized( address ), is( true ) );
|
||||||
|
return address.getId();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
scope.inTransaction( session -> {
|
||||||
|
Address address = session.get( Address.class, addressId );
|
||||||
|
Client client = address.getClient();
|
||||||
|
client.getId();
|
||||||
|
assertThat( Hibernate.isInitialized( client ), is( false ) );
|
||||||
|
client.getName();
|
||||||
|
assertThat( Hibernate.isInitialized( client ), is( true ) );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean rebuildSessionFactoryOnError() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-11838")
|
||||||
|
public void testGetIdManyToOne(SessionFactoryScope scope) {
|
||||||
|
Serializable accountId = scope.fromTransaction( session -> {
|
||||||
|
Address address = new Address();
|
||||||
|
session.save( address );
|
||||||
|
Client client = new Client( address );
|
||||||
|
Account account = new Account();
|
||||||
|
client.addAccount( account );
|
||||||
|
session.save( account );
|
||||||
|
session.save( client );
|
||||||
|
return account.getId();
|
||||||
|
} );
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
Account account = session.load( Account.class, accountId );
|
||||||
|
Client client = account.getClient();
|
||||||
|
client.getId();
|
||||||
|
assertThat( Hibernate.isInitialized( client ), is( false ) );
|
||||||
|
client.getName();
|
||||||
|
assertThat( Hibernate.isInitialized( client ), is( true ) );
|
||||||
|
} );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "Account")
|
||||||
|
public static class Account {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "id_client")
|
||||||
|
private Client client;
|
||||||
|
|
||||||
|
public Client getClient() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClient(Client client) {
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "Address")
|
||||||
|
public static class Address {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String street;
|
||||||
|
|
||||||
|
@OneToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "id_client")
|
||||||
|
private Client client;
|
||||||
|
|
||||||
|
public String getStreet() {
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStreet(String street) {
|
||||||
|
this.street = street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Client getClient() {
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClient(Client client) {
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "Client")
|
||||||
|
public static class Client {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "client")
|
||||||
|
private List<Account> accounts = new ArrayList<>();
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "client", fetch = FetchType.LAZY)
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
public Client() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Client(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
address.setClient( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAccount(Account account) {
|
||||||
|
accounts.add( account );
|
||||||
|
account.setClient( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,24 +4,23 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.lazyload;
|
package org.hibernate.orm.test.lazyload;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vlad Mihalcea
|
* @author Vlad Mihalcea
|
||||||
*/
|
*/
|
||||||
|
@ServiceRegistry(
|
||||||
|
settings = @ServiceRegistry.Setting(name = AvailableSettings.JPA_PROXY_COMPLIANCE, value = "true")
|
||||||
|
)
|
||||||
public class ManyToOneLazyLoadingByIdJpaComplianceTest extends ManyToOneLazyLoadingByIdTest {
|
public class ManyToOneLazyLoadingByIdJpaComplianceTest extends ManyToOneLazyLoadingByIdTest {
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void addMappings(Map settings) {
|
|
||||||
settings.put( AvailableSettings.JPA_PROXY_COMPLIANCE, Boolean.TRUE );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void assertProxyState(Continent continent) {
|
protected void assertProxyState(Continent continent) {
|
||||||
assertEquals( "Europe", continent.getName() );
|
assertEquals( "Europe", continent.getName() );
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.lazyload;
|
package org.hibernate.orm.test.lazyload;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
|
@ -12,45 +12,48 @@ import javax.persistence.Id;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
import org.hibernate.LazyInitializationException;
|
import org.hibernate.LazyInitializationException;
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
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.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Vlad Mihalcea
|
* @author Vlad Mihalcea
|
||||||
*/
|
*/
|
||||||
public class ManyToOneLazyLoadingByIdTest extends BaseEntityManagerFunctionalTestCase {
|
@DomainModel(
|
||||||
|
annotatedClasses = {
|
||||||
|
ManyToOneLazyLoadingByIdTest.Continent.class,
|
||||||
|
ManyToOneLazyLoadingByIdTest.Country.class
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@SessionFactory
|
||||||
|
public class ManyToOneLazyLoadingByIdTest {
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[] {
|
|
||||||
Continent.class,
|
|
||||||
Country.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLazyLoadById() {
|
public void testLazyLoadById(SessionFactoryScope scope) {
|
||||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
scope.inTransaction( session -> {
|
||||||
Continent continent = new Continent();
|
Continent continent = new Continent();
|
||||||
continent.setId( 1L );
|
continent.setId( 1L );
|
||||||
continent.setName( "Europe" );
|
continent.setName( "Europe" );
|
||||||
|
|
||||||
entityManager.persist( continent );
|
session.persist( continent );
|
||||||
|
|
||||||
Country country = new Country();
|
Country country = new Country();
|
||||||
country.setId( 1L );
|
country.setId( 1L );
|
||||||
country.setName( "Romania" );
|
country.setName( "Romania" );
|
||||||
country.setContinent( continent );
|
country.setContinent( continent );
|
||||||
|
|
||||||
entityManager.persist( country );
|
session.persist( country );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
Continent continent = doInJPA( this::entityManagerFactory, entityManager -> {
|
Continent continent = scope.fromTransaction( session -> {
|
||||||
Country country = entityManager.find( Country.class, 1L );
|
Country country = session.find( Country.class, 1L );
|
||||||
|
|
||||||
country.getContinent().getId();
|
country.getContinent().getId();
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.lazyload;
|
package org.hibernate.orm.test.lazyload;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,98 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.test.lazyload;
|
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
import org.hibernate.engine.transaction.jta.platform.internal.AtomikosJtaPlatform;
|
|
||||||
import org.hibernate.persister.entity.CustomSqlSchemaResolvingTest;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
|
||||||
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Vlad Mihalcea
|
|
||||||
*/
|
|
||||||
public class AtomikosJtaLazyLoadingTest
|
|
||||||
extends BaseCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
private static final int CHILDREN_SIZE = 3;
|
|
||||||
private Long parentID;
|
|
||||||
private Long lastChildID;
|
|
||||||
|
|
||||||
protected void configure(Configuration cfg) {
|
|
||||||
super.configure( cfg );
|
|
||||||
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
|
|
||||||
|
|
||||||
TestingJtaBootstrap.prepare( cfg.getProperties() );
|
|
||||||
cfg.setProperty( AvailableSettings.JTA_PLATFORM, "Atomikos" );
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[] {
|
|
||||||
Parent.class,
|
|
||||||
Child.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void prepareTest()
|
|
||||||
throws Exception {
|
|
||||||
doInHibernate( this::sessionFactory, session -> {
|
|
||||||
Parent p = new Parent();
|
|
||||||
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
|
||||||
final Child child = p.makeChild();
|
|
||||||
session.persist( child );
|
|
||||||
lastChildID = child.getId();
|
|
||||||
}
|
|
||||||
session.persist( p );
|
|
||||||
parentID = p.getId();
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue(jiraKey = "HHH-7971")
|
|
||||||
public void testLazyCollectionLoadingAfterEndTransaction() {
|
|
||||||
Parent loadedParent = doInHibernate( this::sessionFactory, session -> {
|
|
||||||
return session.load( Parent.class, parentID );
|
|
||||||
} );
|
|
||||||
|
|
||||||
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for ( Child child : loadedParent.getChildren() ) {
|
|
||||||
i++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, i );
|
|
||||||
|
|
||||||
Child loadedChild = doInHibernate( this::sessionFactory, session -> {
|
|
||||||
return session.load( Child.class, lastChildID );
|
|
||||||
} );
|
|
||||||
|
|
||||||
Parent p = loadedChild.getParent();
|
|
||||||
int j = 0;
|
|
||||||
for ( Child child : p.getChildren() ) {
|
|
||||||
j++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, j );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,102 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.test.lazyload;
|
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.jta.TestingJtaBootstrap;
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Oleksander Dukhno
|
|
||||||
*/
|
|
||||||
public class JtaLazyLoadingTest
|
|
||||||
extends BaseCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
private static final int CHILDREN_SIZE = 3;
|
|
||||||
private Long parentID;
|
|
||||||
private Long lastChildID;
|
|
||||||
|
|
||||||
protected void configure(Configuration cfg) {
|
|
||||||
super.configure( cfg );
|
|
||||||
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
|
|
||||||
|
|
||||||
TestingJtaBootstrap.prepare( cfg.getProperties() );
|
|
||||||
cfg.setProperty( Environment.TRANSACTION_COORDINATOR_STRATEGY, "jta" );
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[] {
|
|
||||||
Parent.class,
|
|
||||||
Child.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void prepareTest()
|
|
||||||
throws Exception {
|
|
||||||
Session s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
|
|
||||||
Parent p = new Parent();
|
|
||||||
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
|
||||||
final Child child = p.makeChild();
|
|
||||||
s.persist( child );
|
|
||||||
lastChildID = child.getId();
|
|
||||||
}
|
|
||||||
s.persist( p );
|
|
||||||
parentID = p.getId();
|
|
||||||
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.clear();
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue(jiraKey = "HHH-7971")
|
|
||||||
public void testLazyCollectionLoadingAfterEndTransaction() {
|
|
||||||
Session s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
Parent loadedParent = (Parent) s.load( Parent.class, parentID );
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for ( Child child : loadedParent.getChildren() ) {
|
|
||||||
i++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, i );
|
|
||||||
|
|
||||||
s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
Child loadedChild = (Child) s.load( Child.class, lastChildID );
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
Parent p = loadedChild.getParent();
|
|
||||||
int j = 0;
|
|
||||||
for ( Child child : p.getChildren() ) {
|
|
||||||
j++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, j );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.test.lazyload;
|
|
||||||
|
|
||||||
import org.hibernate.LazyInitializationException;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Nikolay Golubev
|
|
||||||
*/
|
|
||||||
public class LazyLoadingNotFoundTest extends BaseCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
protected void configure(Configuration cfg) {
|
|
||||||
super.configure(cfg);
|
|
||||||
cfg.setProperty(Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[]{
|
|
||||||
Parent.class,
|
|
||||||
Child.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue(jiraKey = "HHH-11179")
|
|
||||||
public void testNonExistentLazyInitOutsideTransaction() {
|
|
||||||
Session s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
Child loadedChild = s.load(Child.class, -1L);
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
try {
|
|
||||||
loadedChild.getParent();
|
|
||||||
fail("lazy init did not fail on non-existent proxy");
|
|
||||||
} catch (LazyInitializationException e) {
|
|
||||||
assertNotNull(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,296 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.test.lazyload;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.FetchType;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.JoinColumn;
|
|
||||||
import javax.persistence.ManyToOne;
|
|
||||||
import javax.persistence.OneToMany;
|
|
||||||
import javax.persistence.OneToOne;
|
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Oleksander Dukhno
|
|
||||||
*/
|
|
||||||
public class LazyLoadingTest
|
|
||||||
extends BaseCoreFunctionalTestCase {
|
|
||||||
|
|
||||||
private static final int CHILDREN_SIZE = 3;
|
|
||||||
private Long parentID;
|
|
||||||
private Long lastChildID;
|
|
||||||
|
|
||||||
protected void configure(Configuration cfg) {
|
|
||||||
super.configure( cfg );
|
|
||||||
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
|
|
||||||
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class<?>[] {
|
|
||||||
Parent.class,
|
|
||||||
Child.class,
|
|
||||||
Client.class,
|
|
||||||
Address.class,
|
|
||||||
Account.class
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void prepareTest()
|
|
||||||
throws Exception {
|
|
||||||
Session s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
|
|
||||||
Parent p = new Parent();
|
|
||||||
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
|
||||||
final Child child = p.makeChild();
|
|
||||||
s.persist( child );
|
|
||||||
lastChildID = child.getId();
|
|
||||||
}
|
|
||||||
s.persist( p );
|
|
||||||
parentID = p.getId();
|
|
||||||
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.clear();
|
|
||||||
s.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue(jiraKey = "HHH-7971")
|
|
||||||
public void testLazyCollectionLoadingAfterEndTransaction() {
|
|
||||||
Session s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
Parent loadedParent = (Parent) s.load( Parent.class, parentID );
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) );
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for ( Child child : loadedParent.getChildren() ) {
|
|
||||||
i++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, i );
|
|
||||||
|
|
||||||
s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
Child loadedChild = (Child) s.load( Child.class, lastChildID );
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
Parent p = loadedChild.getParent();
|
|
||||||
int j = 0;
|
|
||||||
for ( Child child : p.getChildren() ) {
|
|
||||||
j++;
|
|
||||||
assertNotNull( child );
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals( CHILDREN_SIZE, j );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-11838")
|
|
||||||
public void testGetIdOneToOne() {
|
|
||||||
final Object clientId = sessionFactory().fromTransaction(
|
|
||||||
session -> {
|
|
||||||
Address address = new Address();
|
|
||||||
session.save(address);
|
|
||||||
Client client = new Client(address);
|
|
||||||
return session.save( client );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
final Long addressId = sessionFactory().fromTransaction(
|
|
||||||
session -> {
|
|
||||||
Client client = session.get( Client.class, clientId );
|
|
||||||
Address address = client.getAddress();
|
|
||||||
address.getId();
|
|
||||||
assertThat( Hibernate.isInitialized( address ), is( true ) );
|
|
||||||
address.getStreet();
|
|
||||||
assertThat( Hibernate.isInitialized( address ), is( true ) );
|
|
||||||
return address.getId();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
doInHibernate(this::sessionFactory, s -> {
|
|
||||||
Address address = s.get(Address.class, addressId);
|
|
||||||
Client client = address.getClient();
|
|
||||||
client.getId();
|
|
||||||
assertThat(Hibernate.isInitialized(client), is(false));
|
|
||||||
client.getName();
|
|
||||||
assertThat(Hibernate.isInitialized(client), is(true));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean rebuildSessionFactoryOnError() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@TestForIssue( jiraKey = "HHH-11838")
|
|
||||||
public void testGetIdManyToOne() {
|
|
||||||
Serializable accountId = doInHibernate(this::sessionFactory, s -> {
|
|
||||||
Address address = new Address();
|
|
||||||
s.save(address);
|
|
||||||
Client client = new Client(address);
|
|
||||||
Account account = new Account();
|
|
||||||
client.addAccount(account);
|
|
||||||
s.save(account);
|
|
||||||
s.save(client);
|
|
||||||
return account.getId();
|
|
||||||
});
|
|
||||||
|
|
||||||
doInHibernate(this::sessionFactory, s -> {
|
|
||||||
Account account = s.load(Account.class, accountId);
|
|
||||||
Client client = account.getClient();
|
|
||||||
client.getId();
|
|
||||||
assertThat(Hibernate.isInitialized(client), is(false));
|
|
||||||
client.getName();
|
|
||||||
assertThat(Hibernate.isInitialized(client), is(true));
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Entity(name = "Account")
|
|
||||||
public static class Account {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
|
||||||
@JoinColumn(name = "id_client")
|
|
||||||
private Client client;
|
|
||||||
|
|
||||||
public Client getClient() {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setClient(Client client) {
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Entity(name = "Address")
|
|
||||||
public static class Address {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
private String street;
|
|
||||||
|
|
||||||
@OneToOne(fetch = FetchType.LAZY)
|
|
||||||
@JoinColumn(name = "id_client")
|
|
||||||
private Client client;
|
|
||||||
|
|
||||||
public String getStreet() {
|
|
||||||
return street;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStreet(String street) {
|
|
||||||
this.street = street;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Client getClient() {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setClient(Client client) {
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Entity(name = "Client")
|
|
||||||
public static class Client {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@OneToMany(mappedBy = "client")
|
|
||||||
private List<Account> accounts = new ArrayList<>();
|
|
||||||
|
|
||||||
@OneToOne(mappedBy = "client", fetch = FetchType.LAZY)
|
|
||||||
private Address address;
|
|
||||||
|
|
||||||
public Client() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Client(Address address) {
|
|
||||||
this.address = address;
|
|
||||||
address.setClient(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addAccount(Account account) {
|
|
||||||
accounts.add(account);
|
|
||||||
account.setClient(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Address getAddress() {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAddress(Address address) {
|
|
||||||
this.address = address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,6 +23,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.engine.spi.SessionImplementor;
|
import org.hibernate.engine.spi.SessionImplementor;
|
||||||
import org.hibernate.internal.util.StringHelper;
|
import org.hibernate.internal.util.StringHelper;
|
||||||
import org.hibernate.resource.jdbc.spi.StatementInspector;
|
import org.hibernate.resource.jdbc.spi.StatementInspector;
|
||||||
|
import org.hibernate.resource.transaction.spi.TransactionStatus;
|
||||||
import org.hibernate.tool.schema.Action;
|
import org.hibernate.tool.schema.Action;
|
||||||
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
|
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
|
||||||
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping;
|
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping;
|
||||||
|
@ -340,9 +341,20 @@ public class SessionFactoryExtension
|
||||||
action.accept( session );
|
action.accept( session );
|
||||||
log.trace( "Called action - in txn" );
|
log.trace( "Called action - in txn" );
|
||||||
|
|
||||||
log.trace( "Committing transaction" );
|
if ( !txn.getRollbackOnly() ) {
|
||||||
txn.commit();
|
log.trace( "Committing transaction" );
|
||||||
log.trace( "Committed transaction" );
|
txn.commit();
|
||||||
|
log.trace( "Committed transaction" );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
log.trace( "Rollback transaction marked for rollback only" );
|
||||||
|
txn.rollback();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
log.error( "Rollback failure", e );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.tracef(
|
log.tracef(
|
||||||
|
|
Loading…
Reference in New Issue