HHH-16423 Add tests for issue

This commit is contained in:
Andrea Boriero 2023-04-11 14:21:12 +02:00 committed by Christian Beikov
parent d49229f5d0
commit f1f74b06cc
7 changed files with 1475 additions and 6 deletions

View File

@ -0,0 +1,223 @@
package org.hibernate.orm.test.bytecode.enhancement.bag;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(BytecodeEnhancerRunner.class)
public class BagAndSetFetchTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
EntityA.class,
EntityB.class,
EntityC.class,
EntityD.class,
};
}
@Test
public void testIt() {
inTransaction(
session -> {
EntityB b = new EntityB( 1l, "b" );
EntityC c = new EntityC( 1l, "c" );
EntityC c1 = new EntityC( 2l, "c1" );
b.addAttribute( c );
b.addAttribute( c1 );
EntityD d = new EntityD( 1l, "d" );
EntityD d1 = new EntityD( 2l, "d1" );
EntityD d2 = new EntityD( 3l, "d2" );
b.addEntityD( d );
b.addEntityD( d1 );
b.addEntityD( d2 );
EntityB b1 = new EntityB( 2l, "b1" );
EntityC c2 = new EntityC( 3l, "c2" );
EntityC c3 = new EntityC( 4l, "c3" );
EntityC c4 = new EntityC( 5l, "c4" );
b1.addAttribute( c2 );
b1.addAttribute( c3 );
b1.addAttribute( c4 );
EntityA a = new EntityA( 1l, "a" );
a.addAttribute( b );
a.addAttribute( b1 );
session.save( c );
session.save( c1 );
session.save( c2 );
session.save( c3 );
session.save( c4 );
session.save( d );
session.save( d1 );
session.save( d2 );
session.save( b );
session.save( b1 );
session.save( a );
}
);
inTransaction(
session -> {
EntityA entityA = session.find( EntityA.class, 1l );
Collection<EntityB> attributes = entityA.attributes;
assertThat( attributes.size() ).isEqualTo( 2 );
boolean findB = false;
boolean findB1 = false;
for ( EntityB entityB : attributes ) {
Collection<EntityC> entityCS = entityB.attributes;
Set<EntityD> entityDS = entityB.entityDS;
if ( entityB.getName().equals( "b" ) ) {
assertThat( entityCS.size() ).isEqualTo( 2 );
assertThat( entityDS.size() ).isEqualTo( 3 );
findB = true;
}
else {
assertThat( entityCS.size() ).isEqualTo( 3 );
assertThat( entityDS.size() ).isEqualTo( 0 );
findB1 = true;
}
}
assertTrue( findB );
assertTrue( findB1 );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityB> attributes = new LinkedList<>();
public EntityA() {
}
public EntityA(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityB> getAttributes() {
return attributes;
}
public void addAttribute(EntityB entityB) {
attributes.add( entityB );
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityC> attributes = new LinkedList<>();
@OneToMany(fetch = FetchType.EAGER)
Set<EntityD> entityDS = new HashSet<>();
public EntityB() {
}
public EntityB(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityC> getAttributes() {
return attributes;
}
public void addAttribute(EntityC entityC) {
this.attributes.add( entityC );
}
public void addEntityD(EntityD entityD) {
this.entityDS.add( entityD );
}
}
@Entity(name = "EntityC")
public static class EntityC {
@Id
private Long id;
private String name;
public EntityC() {
}
public EntityC(Long id, String name) {
this.id = id;
this.name = name;
}
}
@Entity(name = "EntityD")
public static class EntityD {
@Id
private Long id;
private String name;
public EntityD() {
}
public EntityD(Long id, String name) {
this.id = id;
this.name = name;
}
}
}

View File

@ -0,0 +1,200 @@
package org.hibernate.orm.test.bytecode.enhancement.bag;
import java.util.Collection;
import java.util.LinkedList;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(BytecodeEnhancerRunner.class)
public class EagerBagsTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
EntityA.class,
EntityB.class,
EntityC.class,
EntityD.class,
};
}
@Test
public void testIt() {
inTransaction(
session -> {
EntityB b = new EntityB( 1l, "b" );
EntityC c = new EntityC( 1l, "c" );
EntityC c1 = new EntityC( 2l, "c1" );
b.addAttribute( c );
b.addAttribute( c1 );
EntityB b1 = new EntityB( 2l, "b1" );
EntityC c2 = new EntityC( 3l, "c2" );
EntityC c3 = new EntityC( 4l, "c3" );
EntityC c4 = new EntityC( 5l, "c4" );
b1.addAttribute( c2 );
b1.addAttribute( c3 );
b1.addAttribute( c4 );
EntityA a = new EntityA( 1l, "a" );
a.addAttribute( b );
a.addAttribute( b1 );
session.save( c );
session.save( c1 );
session.save( c2 );
session.save( c3 );
session.save( c4 );
session.save( b );
session.save( b1 );
session.save( a );
}
);
inTransaction(
session -> {
EntityA entityA = session.find( EntityA.class, 1l );
Collection<EntityB> attributes = entityA.attributes;
assertThat( attributes.size() ).isEqualTo( 2 );
boolean findB = false;
boolean findB1 = false;
for ( EntityB entityB : attributes ) {
Collection<EntityC> entityCS = entityB.attributes;
if ( entityB.getName().equals( "b" ) ) {
assertThat( entityCS.size() ).isEqualTo( 2 );
findB = true;
}
else {
assertThat( entityCS.size() ).isEqualTo( 3 );
findB1 = true;
}
}
assertTrue( findB );
assertTrue( findB1 );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityB> attributes = new LinkedList<>();
public EntityA() {
}
public EntityA(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityB> getAttributes() {
return attributes;
}
public void addAttribute(EntityB entityB) {
attributes.add( entityB );
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityC> attributes = new LinkedList<>();
public EntityB() {
}
public EntityB(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityC> getAttributes() {
return attributes;
}
public void addAttribute(EntityC entityC) {
this.attributes.add( entityC );
}
}
@Entity(name = "EntityC")
public static class EntityC {
@Id
private Long id;
private String name;
public EntityC() {
}
public EntityC(Long id, String name) {
this.id = id;
this.name = name;
}
}
@Entity(name = "EntityD")
public static class EntityD {
@Id
private Long id;
private String name;
public EntityD() {
}
public EntityD(Long id, String name) {
this.id = id;
this.name = name;
}
}
}

View File

@ -9,6 +9,7 @@ import java.util.Set;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -41,20 +42,29 @@ public class RefreshTest extends BaseCoreFunctionalTestCase {
}; };
} }
@Before @After
public void setUp() { public void trearDown() {
inTransaction( inTransaction(
session -> { session -> {
RealmEntity realm = new RealmEntity(); RealmEntity find = session.find( RealmEntity.class, "id" );
realm.setId( REALM_ID ); if(find != null) {
realm.setName( "realm" ); session.remove( find );
session.persist( realm ); }
} }
); );
} }
@Test @Test
public void testRefresh() { public void testRefresh() {
inTransaction(
session -> {
RealmEntity realm = new RealmEntity();
realm.setId( REALM_ID );
realm.setName( "realm" );
session.persist( realm );
}
);
inTransaction( inTransaction(
session -> { session -> {
RealmEntity realm = session.find( RealmEntity.class, REALM_ID ); RealmEntity realm = session.find( RealmEntity.class, REALM_ID );
@ -68,6 +78,120 @@ public class RefreshTest extends BaseCoreFunctionalTestCase {
); );
} }
@Test
public void testRefresh2() {
inTransaction(
session -> {
RealmEntity realm = new RealmEntity();
realm.setId( "id" );
realm.setName( "realm" );
RealmAttributeEntity attribute1 = new RealmAttributeEntity();
attribute1.setName( "a1" );
RealmAttributeEntity attribute2 = new RealmAttributeEntity();
attribute2.setName( "a2" );
realm.addAttribute( attribute1 );
realm.addAttribute( attribute2 );
session.persist( attribute1 );
session.persist( attribute2 );
session.persist( realm );
}
);
inTransaction(
session -> {
RealmEntity realm = session.find( RealmEntity.class, REALM_ID );
session.refresh( realm );
assertThat( realm.getComponents().size() ).isEqualTo( 0 );
assertThat( realm.getAttributes().size() ).isEqualTo( 2 );
session.remove( realm );
}
);
}
@Test
public void testRefresh3() {
inTransaction(
session -> {
RealmEntity realm = new RealmEntity();
realm.setId( "id" );
realm.setName( "realm" );
ComponentEntity component = new ComponentEntity();
component.setId( "id_comp" );
realm.addComponent( component );
ComponentEntity component2 = new ComponentEntity();
component2.setId( "id_comp_2" );
realm.addComponent( component2 );
session.persist( realm );
}
);
inTransaction(
session -> {
RealmEntity realm = session.find( RealmEntity.class, REALM_ID );
session.refresh( realm );
assertThat( realm.getComponents().size() ).isEqualTo( 2 );
assertThat( realm.getAttributes().size() ).isEqualTo( 0 );
session.remove( realm );
}
);
}
@Test
public void testRefresh4() {
inTransaction(
session -> {
RealmEntity realm = new RealmEntity();
realm.setId( "id" );
realm.setName( "realm" );
ComponentEntity component = new ComponentEntity();
component.setId( "id_comp" );
realm.addComponent( component );
ComponentEntity component2 = new ComponentEntity();
component2.setId( "id_comp_2" );
realm.addComponent( component2 );
RealmAttributeEntity attribute1 = new RealmAttributeEntity();
attribute1.setName( "a1" );
RealmAttributeEntity attribute2 = new RealmAttributeEntity();
attribute2.setName( "a2" );
realm.addAttribute( attribute1 );
realm.addAttribute( attribute2 );
session.persist( attribute1 );
session.persist( attribute2 );
session.persist( realm );
}
);
inTransaction(
session -> {
RealmEntity realm = session.find( RealmEntity.class, REALM_ID );
session.refresh( realm );
assertThat( realm.getComponents().size() ).isEqualTo( 2 );
assertThat( realm.getAttributes().size() ).isEqualTo( 2 );
session.remove( realm );
}
);
}
@Table(name = "REALM") @Table(name = "REALM")
@Entity(name = "RealmEntity") @Entity(name = "RealmEntity")
public static class RealmEntity { public static class RealmEntity {
@ -111,6 +235,11 @@ public class RefreshTest extends BaseCoreFunctionalTestCase {
this.attributes = attributes; this.attributes = attributes;
} }
public void addAttribute(RealmAttributeEntity attribute){
attribute.setRealm( this );
this.attributes.add( attribute );
}
public Set<ComponentEntity> getComponents() { public Set<ComponentEntity> getComponents() {
if ( components == null ) { if ( components == null ) {
components = new HashSet<>(); components = new HashSet<>();
@ -121,6 +250,11 @@ public class RefreshTest extends BaseCoreFunctionalTestCase {
public void setComponents(Set<ComponentEntity> components) { public void setComponents(Set<ComponentEntity> components) {
this.components = components; this.components = components;
} }
public void addComponent(ComponentEntity component){
this.components.add( component );
component.setRealm( this );
}
} }
@Entity(name = "ComponentEntity") @Entity(name = "ComponentEntity")

View File

@ -0,0 +1,236 @@
package org.hibernate.orm.test.collection.bag;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
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.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertTrue;
@DomainModel(
annotatedClasses = {
BagAndSetFetchTest.EntityA.class,
BagAndSetFetchTest.EntityB.class,
BagAndSetFetchTest.EntityC.class,
BagAndSetFetchTest.EntityD.class,
}
)
@SessionFactory
public class BagAndSetFetchTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EntityC c = new EntityC( 1l, "c" );
EntityC c1 = new EntityC( 2l, "c1" );
EntityD d = new EntityD( 1l, "d" );
EntityD d1 = new EntityD( 2l, "d1" );
EntityD d2 = new EntityD( 3l, "d2" );
EntityB b = new EntityB( 1l, "b" );
b.addAttribute( c );
b.addAttribute( c1 );
b.addEntityD( d );
b.addEntityD( d1 );
b.addEntityD( d2 );
EntityB b1 = new EntityB( 2l, "b1" );
EntityC c2 = new EntityC( 3l, "c2" );
EntityC c3 = new EntityC( 4l, "c3" );
EntityC c4 = new EntityC( 5l, "c4" );
b1.addAttribute( c2 );
b1.addAttribute( c3 );
b1.addAttribute( c4 );
EntityA a = new EntityA( 1l, "a" );
a.addAttribute( b );
a.addAttribute( b1 );
session.save( c );
session.save( c1 );
session.save( c2 );
session.save( c3 );
session.save( c4 );
session.save( d );
session.save( d1 );
session.save( d2 );
session.save( b );
session.save( b1 );
session.save( a );
}
);
}
@Test
public void testIt(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EntityB entityB = session.find( EntityB.class, 1l );
Collection<EntityC> entityCS = entityB.getAttributes();
Set<EntityD> entityDS = entityB.entityDS;
assertThat( entityB.getName() ).isEqualTo( "b" );
assertThat( entityCS.size() ).isEqualTo( 2 );
assertThat( entityDS.size() ).isEqualTo( 3 );
}
);
scope.inTransaction(
session -> {
EntityA entityA = session.find( EntityA.class, 1l );
Collection<EntityB> attributes = entityA.attributes;
assertThat( attributes.size() ).isEqualTo( 2 );
boolean findB = false;
boolean findB1 = false;
for ( EntityB entityB : attributes ) {
Collection<EntityC> entityCS = entityB.attributes;
Set<EntityD> entityDS = entityB.entityDS;
if ( entityB.getName().equals( "b" ) ) {
assertThat( entityCS.size() ).isEqualTo( 2 );
assertThat( entityDS.size() ).isEqualTo( 3 );
findB = true;
}
else {
assertThat( entityCS.size() ).isEqualTo( 3 );
assertThat( entityDS.size() ).isEqualTo( 0 );
findB1 = true;
}
}
assertTrue( findB );
assertTrue( findB1 );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityB> attributes = new LinkedList<>();
public EntityA() {
}
public EntityA(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityB> getAttributes() {
return attributes;
}
public void addAttribute(EntityB entityB) {
attributes.add( entityB );
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityC> attributes = new LinkedList<>();
@OneToMany(fetch = FetchType.EAGER)
Set<EntityD> entityDS = new HashSet<>();
public EntityB() {
}
public EntityB(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityC> getAttributes() {
return attributes;
}
public void addAttribute(EntityC entityC) {
this.attributes.add( entityC );
}
public void addEntityD(EntityD entityD) {
this.entityDS.add( entityD );
}
}
@Entity(name = "EntityC")
public static class EntityC {
@Id
private Long id;
private String name;
public EntityC() {
}
public EntityC(Long id, String name) {
this.id = id;
this.name = name;
}
}
@Entity(name = "EntityD")
public static class EntityD {
@Id
private Long id;
private String name;
public EntityD() {
}
public EntityD(Long id, String name) {
this.id = id;
this.name = name;
}
}
}

View File

@ -0,0 +1,165 @@
package org.hibernate.orm.test.collection.bag;
import java.util.Collection;
import java.util.LinkedList;
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.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
EagerBagsTest.EntityA.class,
EagerBagsTest.EntityB.class,
EagerBagsTest.EntityC.class
}
)
@SessionFactory
public class EagerBagsTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EntityC c = new EntityC( 1l, "c" );
EntityC c1 = new EntityC( 2l, "c1" );
EntityC c2 = new EntityC( 3l, "c2" );
EntityC c3 = new EntityC( 4l, "c3" );
EntityB b = new EntityB( 1l, "b" );
b.addAttribute( c );
b.addAttribute( c1 );
EntityB b1 = new EntityB( 2l, "b1" );
b1.addAttribute( c2 );
b1.addAttribute( c3 );
EntityA a = new EntityA( 1l, "a" );
a.addAttribute( b );
a.addAttribute( b1 );
session.save( c );
session.save( c1 );
session.save( c2 );
session.save( c3 );
session.save( b );
session.save( b1 );
session.save( a );
}
);
}
@Test
public void testIt(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EntityA entityA = session.find( EntityA.class, 1l );
assertThat( entityA.attributes.size() ).isEqualTo( 2 );
}
);
}
@Entity(name = "EntityA")
public static class EntityA {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityB> attributes = new LinkedList<>();
public EntityA() {
}
public EntityA(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityB> getAttributes() {
return attributes;
}
public void addAttribute(EntityB entityB) {
attributes.add( entityB );
}
}
@Entity(name = "EntityB")
public static class EntityB {
@Id
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER)
Collection<EntityC> attributes = new LinkedList<>();
public EntityB() {
}
public EntityB(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<EntityC> getAttributes() {
return attributes;
}
public void addAttribute(EntityC entityC) {
this.attributes.add( entityC );
}
}
@Entity(name = "EntityC")
public static class EntityC {
@Id
private Long id;
private String name;
public EntityC() {
}
public EntityC(Long id, String name) {
this.id = id;
this.name = name;
}
}
}

View File

@ -0,0 +1,254 @@
package org.hibernate.orm.test.refresh;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Jpa(
annotatedClasses = {
JPARefreshTest.RealmEntity.class,
JPARefreshTest.RealmAttributeEntity.class,
JPARefreshTest.ComponentEntity.class,
}
)
public class JPARefreshTest {
@Test
public void testDelete(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
entityManager.getTransaction().begin();
try {
RealmEntity realm = new RealmEntity();
realm.setId( "id" );
realm.setName( "realm" );
ComponentEntity c1 = new ComponentEntity();
c1.setId( "c1" );
c1.setRealm( realm );
ComponentEntity c2 = new ComponentEntity();
c2.setId( "c2" );
c2.setRealm( realm );
realm.setComponents( Set.of( c1, c2 ) );
entityManager.persist( realm );
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
RealmEntity find = entityManager.find( RealmEntity.class, "id" );
entityManager.refresh( realm );
entityManager.remove( find );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
}
);
}
@Table(name="REALM")
@Entity(name = "RealmEntity")
public static class RealmEntity {
@Id
@Column(name="ID", length = 36)
protected String id;
@Column(name="NAME", unique = true)
protected String name;
@OneToMany(cascade ={ CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm", fetch = FetchType.EAGER)
Collection<RealmAttributeEntity> attributes = new LinkedList<>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.ALL}, orphanRemoval = true, mappedBy = "realm")
Set<ComponentEntity> components = new HashSet<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<RealmAttributeEntity> getAttributes() {
if (attributes == null) {
attributes = new LinkedList<>();
}
return attributes;
}
public void setAttributes(Collection<RealmAttributeEntity> attributes) {
this.attributes = attributes;
}
public Set<ComponentEntity> getComponents() {
if (components == null) {
components = new HashSet<>();
}
return components;
}
public void setComponents(Set<ComponentEntity> components) {
this.components = components;
}
}
@Entity(name = "ComponentEntity")
public static class ComponentEntity {
@Id
@Column(name="ID", length = 36)
protected String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Column(name="NAME")
protected String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmEntity getRealm() {
return realm;
}
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
}
@Table(name="REALM_ATTRIBUTE")
@Entity(name = "RealmAttributeEntity")
@IdClass(Key.class)
public static class RealmAttributeEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Id
@Column(name = "NAME")
protected String name;
@Column(name = "VALUE_COLUMN")
protected String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public RealmEntity getRealm() {
return realm;
}
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
}
public static class Key implements Serializable {
protected RealmEntity realm;
protected String name;
public Key() {
}
public Key(RealmEntity user, String name) {
this.realm = user;
this.name = name;
}
public RealmEntity getRealm() {
return realm;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (name != null ? !name.equals(key.name) : key.name != null) return false;
if (realm != null ? !realm.getId().equals(key.realm != null ? key.realm.getId() : null) : key.realm != null) return false;
return true;
}
@Override
public int hashCode() {
int result = realm != null ? realm.getId().hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
}

View File

@ -0,0 +1,257 @@
package org.hibernate.orm.test.refresh;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
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 jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@DomainModel(
annotatedClasses = {
RefreshTest.RealmEntity.class,
RefreshTest.RealmAttributeEntity.class,
RefreshTest.ComponentEntity.class,
}
)
@SessionFactory
public class RefreshTest {
@Test
public void testIt(SessionFactoryScope scope) {
scope.inSession(
session -> {
session.getTransaction().begin();
try {
RealmEntity realm = new RealmEntity();
realm.setId( "id" );
realm.setName( "realm" );
ComponentEntity c1 = new ComponentEntity();
c1.setId( "c1" );
c1.setRealm( realm );
ComponentEntity c2 = new ComponentEntity();
c2.setId( "c2" );
c2.setRealm( realm );
realm.setComponents( Set.of( c1, c2 ) );
session.persist( realm );
session.getTransaction().commit();
session.getTransaction().begin();
RealmEntity find = session.find( RealmEntity.class, "id" );
session.refresh( realm );
session.remove( find );
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
);
}
@Table(name="REALM")
@Entity(name = "RealmEntity")
public static class RealmEntity {
@Id
@Column(name="ID", length = 36)
protected String id;
@Column(name="NAME", unique = true)
protected String name;
@OneToMany(cascade ={ CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm", fetch = FetchType.EAGER)
Collection<RealmAttributeEntity> attributes = new LinkedList<>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.ALL}, orphanRemoval = true, mappedBy = "realm")
Set<ComponentEntity> components = new HashSet<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<RealmAttributeEntity> getAttributes() {
if (attributes == null) {
attributes = new LinkedList<>();
}
return attributes;
}
public void setAttributes(Collection<RealmAttributeEntity> attributes) {
this.attributes = attributes;
}
public Set<ComponentEntity> getComponents() {
if (components == null) {
components = new HashSet<>();
}
return components;
}
public void setComponents(Set<ComponentEntity> components) {
this.components = components;
}
}
@Entity(name = "ComponentEntity")
public static class ComponentEntity {
@Id
@Column(name="ID", length = 36)
protected String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Column(name="NAME")
protected String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmEntity getRealm() {
return realm;
}
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
}
@Table(name="REALM_ATTRIBUTE")
@Entity(name = "RealmAttributeEntity")
@IdClass(Key.class)
public static class RealmAttributeEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;
@Id
@Column(name = "NAME")
protected String name;
@Column(name = "VALUE_COLUMN")
protected String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public RealmEntity getRealm() {
return realm;
}
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
}
public static class Key implements Serializable {
protected RealmEntity realm;
protected String name;
public Key() {
}
public Key(RealmEntity user, String name) {
this.realm = user;
this.name = name;
}
public RealmEntity getRealm() {
return realm;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (name != null ? !name.equals(key.name) : key.name != null) return false;
if (realm != null ? !realm.getId().equals(key.realm != null ? key.realm.getId() : null) : key.realm != null) return false;
return true;
}
@Override
public int hashCode() {
int result = realm != null ? realm.getId().hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
}