mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-20 01:55:02 +00:00
HHH-12637 : Improvement to fix for HHH-12592
HHH-12637 : Rename/add new tests; remove sequences from IDs
This commit is contained in:
parent
463937fcec
commit
3dcf69c687
@ -28,6 +28,7 @@
|
|||||||
import org.hibernate.engine.spi.Status;
|
import org.hibernate.engine.spi.Status;
|
||||||
import org.hibernate.internal.CoreLogging;
|
import org.hibernate.internal.CoreLogging;
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
|
import org.hibernate.internal.util.StringHelper;
|
||||||
import org.hibernate.persister.collection.CollectionPersister;
|
import org.hibernate.persister.collection.CollectionPersister;
|
||||||
import org.hibernate.persister.collection.QueryableCollection;
|
import org.hibernate.persister.collection.QueryableCollection;
|
||||||
import org.hibernate.persister.entity.EntityPersister;
|
import org.hibernate.persister.entity.EntityPersister;
|
||||||
@ -257,8 +258,14 @@ private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersiste
|
|||||||
final BytecodeEnhancementMetadata bytecodeEnhancementMetadata =
|
final BytecodeEnhancementMetadata bytecodeEnhancementMetadata =
|
||||||
persister.getOwnerEntityPersister().getInstrumentationMetadata();
|
persister.getOwnerEntityPersister().getInstrumentationMetadata();
|
||||||
if ( bytecodeEnhancementMetadata.isEnhancedForLazyLoading() ) {
|
if ( bytecodeEnhancementMetadata.isEnhancedForLazyLoading() ) {
|
||||||
// Figure out the collection property name.
|
// Lazy properties in embeddables/composites are not currently supported for embeddables (HHH-10480),
|
||||||
// TODO: what if the collection is in an embeddable???
|
// so check to make sure the collection is not in an embeddable before checking to see if
|
||||||
|
// the collection is lazy.
|
||||||
|
// TODO: More will probably need to be done here when HHH-10480 is fixed..
|
||||||
|
if ( StringHelper.qualifier( persister.getRole() ).length() ==
|
||||||
|
persister.getOwnerEntityPersister().getEntityName().length() ) {
|
||||||
|
// Assume the collection is not in an embeddable.
|
||||||
|
// Strip off <entityName><dot> to get the collection property name.
|
||||||
final String propertyName = persister.getRole().substring(
|
final String propertyName = persister.getRole().substring(
|
||||||
persister.getOwnerEntityPersister().getEntityName().length() + 1
|
persister.getOwnerEntityPersister().getEntityName().length() + 1
|
||||||
);
|
);
|
||||||
@ -274,6 +281,7 @@ private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersiste
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add to cache if:
|
// add to cache if:
|
||||||
boolean addToCache =
|
boolean addToCache =
|
||||||
|
@ -11,11 +11,9 @@
|
|||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.SequenceGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Chris Cranford
|
* @author Chris Cranford
|
||||||
@ -24,8 +22,7 @@
|
|||||||
@Access(AccessType.FIELD)
|
@Access(AccessType.FIELD)
|
||||||
public class Leaf {
|
public class Leaf {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="LEAF_SEQ")
|
@GeneratedValue
|
||||||
@SequenceGenerator(name = "LEAF_SEQ", sequenceName = "LEAF_SEQ")
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToOne(optional = false)
|
@ManyToOne(optional = false)
|
||||||
|
@ -9,13 +9,12 @@
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.ElementCollection;
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
import javax.persistence.Embedded;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
@ -32,7 +31,7 @@
|
|||||||
*/
|
*/
|
||||||
@TestForIssue(jiraKey = "HHH-12592")
|
@TestForIssue(jiraKey = "HHH-12592")
|
||||||
@RunWith(BytecodeEnhancerRunner.class)
|
@RunWith(BytecodeEnhancerRunner.class)
|
||||||
public class MergeEnhancedDetachedCollectionInEmbeddableTest extends BaseCoreFunctionalTestCase {
|
public class MergeDetachedCascadedCollectionInEmbeddableTest extends BaseCoreFunctionalTestCase {
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
return new Class<?>[] { Heading.class, Grouping.class, Thing.class };
|
return new Class<?>[] { Heading.class, Grouping.class, Thing.class };
|
||||||
@ -96,6 +95,7 @@ public static class Grouping {
|
|||||||
private Set<Thing> things = new HashSet<>();
|
private Set<Thing> things = new HashSet<>();
|
||||||
|
|
||||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn
|
||||||
public Set<Thing> getThings() {
|
public Set<Thing> getThings() {
|
||||||
return things;
|
return things;
|
||||||
}
|
}
|
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* 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.bytecode.enhancement.merge;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
|
||||||
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
@TestForIssue(jiraKey = "HHH-12637")
|
||||||
|
@RunWith(BytecodeEnhancerRunner.class)
|
||||||
|
public class MergeDetachedNonCascadedCollectionInEmbeddableTest extends BaseCoreFunctionalTestCase {
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
|
return new Class<?>[] { Heading.class, Grouping.class, Thing.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMergeDetached() {
|
||||||
|
final Heading heading = doInHibernate( this::sessionFactory, session -> {
|
||||||
|
Heading entity = new Heading();
|
||||||
|
entity.name = "new";
|
||||||
|
entity.setGrouping( new Grouping() );
|
||||||
|
Thing thing = new Thing();
|
||||||
|
entity.getGrouping().getThings().add( thing );
|
||||||
|
session.save( thing );
|
||||||
|
session.save( entity );
|
||||||
|
return entity;
|
||||||
|
} );
|
||||||
|
|
||||||
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
|
heading.name = "updated";
|
||||||
|
Heading headingMerged = (Heading) session.merge( heading );
|
||||||
|
assertNotSame( heading, headingMerged );
|
||||||
|
assertNotSame( heading.grouping, headingMerged.grouping );
|
||||||
|
assertNotSame( heading.grouping.things, headingMerged.grouping.things );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "Heading")
|
||||||
|
public static class Heading {
|
||||||
|
private long id;
|
||||||
|
private String name;
|
||||||
|
private Grouping grouping;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Grouping getGrouping() {
|
||||||
|
return grouping;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGrouping(Grouping grouping) {
|
||||||
|
this.grouping = grouping;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Embeddable
|
||||||
|
public static class Grouping {
|
||||||
|
private Set<Thing> things = new HashSet<>();
|
||||||
|
|
||||||
|
@OneToMany(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn
|
||||||
|
public Set<Thing> getThings() {
|
||||||
|
return things;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThings(Set<Thing> things) {
|
||||||
|
this.things = things;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "Thing")
|
||||||
|
public static class Thing {
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,12 +15,11 @@
|
|||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.SequenceGenerator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @author Chris Cranford
|
* @author Chris Cranford
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@ -31,8 +30,7 @@ public class Root {
|
|||||||
private Set<Leaf> leaves = new HashSet<>();
|
private Set<Leaf> leaves = new HashSet<>();
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ROOT_SEQ")
|
@GeneratedValue
|
||||||
@SequenceGenerator(name = "ROOT_SEQ", sequenceName = "ROOT_SEQ")
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user