HHH-12064 - Issue with unidirectional one-to-many association with a join column that references a column that is not the primary key
This commit is contained in:
parent
88b1e33d72
commit
ef6ddd98bc
|
@ -14,7 +14,6 @@ import org.hibernate.collection.spi.PersistentCollection;
|
|||
import org.hibernate.engine.spi.PersistenceContext;
|
||||
import org.hibernate.internal.CoreLogging;
|
||||
import org.hibernate.loader.plan.exec.process.spi.CollectionReferenceInitializer;
|
||||
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext;
|
||||
import org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases;
|
||||
import org.hibernate.loader.plan.spi.CollectionReference;
|
||||
import org.hibernate.loader.plan.spi.Fetch;
|
||||
|
@ -85,6 +84,7 @@ public class CollectionReferenceInitializerImpl implements CollectionReferenceIn
|
|||
}
|
||||
else {
|
||||
final Serializable optionalKey = findCollectionOwnerKey( context );
|
||||
|
||||
if ( optionalKey != null ) {
|
||||
// we did not find a collection element in the result set, so we
|
||||
// ensure that a collection is created with the owner's identifier,
|
||||
|
@ -138,12 +138,12 @@ public class CollectionReferenceInitializerImpl implements CollectionReferenceIn
|
|||
}
|
||||
|
||||
protected Serializable findCollectionOwnerKey(ResultSetProcessingContextImpl context) {
|
||||
ResultSetProcessingContext.EntityReferenceProcessingState ownerState = context.getOwnerProcessingState( (Fetch) collectionReference );
|
||||
Object owner = context.getOwnerProcessingState( (Fetch) collectionReference ).getEntityInstance();
|
||||
|
||||
if(ownerState == null || ownerState.getEntityKey()==null){
|
||||
return null;
|
||||
}
|
||||
return ownerState.getEntityKey().getIdentifier();
|
||||
return collectionReference.getCollectionPersister().getCollectionType().getKeyOfOwner(
|
||||
owner,
|
||||
context.getSession()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -46,12 +46,11 @@ import org.hibernate.persister.entity.Joinable;
|
|||
import org.hibernate.pretty.MessageHelper;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
import org.hibernate.proxy.LazyInitializer;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* A type that handles Hibernate <tt>PersistentCollection</tt>s (including arrays).
|
||||
*
|
||||
*
|
||||
* @author Gavin King
|
||||
*/
|
||||
public abstract class CollectionType extends AbstractType implements AssociationType {
|
||||
|
@ -203,7 +202,7 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
return "<uninitialized>";
|
||||
}
|
||||
|
||||
final List<String> list = new ArrayList<String>();
|
||||
final List<String> list = new ArrayList<>();
|
||||
Type elemType = getElementType( factory );
|
||||
Iterator itr = getElementsIterator( value );
|
||||
while ( itr.hasNext() ) {
|
||||
|
@ -259,11 +258,11 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
public Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner)
|
||||
throws HibernateException {
|
||||
//remember the uk value
|
||||
|
||||
|
||||
//This solution would allow us to eliminate the owner arg to disassemble(), but
|
||||
//what if the collection was null, and then later had elements added? seems unsafe
|
||||
//session.getPersistenceContext().getCollectionEntry( (PersistentCollection) value ).getKey();
|
||||
|
||||
|
||||
final Serializable key = getKeyOfOwner(owner, session);
|
||||
if (key==null) {
|
||||
return null;
|
||||
|
@ -278,7 +277,7 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
@Override
|
||||
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner)
|
||||
throws HibernateException {
|
||||
//we must use the "remembered" uk value, since it is
|
||||
//we must use the "remembered" uk value, since it is
|
||||
//not available from the EntityEntry during assembly
|
||||
if (cached==null) {
|
||||
return null;
|
||||
|
@ -369,14 +368,14 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
* @return The collection owner's key
|
||||
*/
|
||||
public Serializable getKeyOfOwner(Object owner, SharedSessionContractImplementor session) {
|
||||
|
||||
|
||||
EntityEntry entityEntry = session.getPersistenceContext().getEntry( owner );
|
||||
if ( entityEntry == null ) {
|
||||
// This just handles a particular case of component
|
||||
// projection, perhaps get rid of it and throw an exception
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if ( foreignKeyPropertyName == null ) {
|
||||
return entityEntry.getId();
|
||||
}
|
||||
|
@ -397,11 +396,13 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
// NOTE VERY HACKISH WORKAROUND!!
|
||||
// TODO: Fix this so it will work for non-POJO entity mode
|
||||
Type keyType = getPersister( session ).getKeyType();
|
||||
if ( !keyType.getReturnedClass().isInstance( id ) ) {
|
||||
Class returnedClass = keyType.getReturnedClass();
|
||||
|
||||
if ( !returnedClass.equals( entityEntry.getPersister().getMappedClass() ) && !returnedClass.isInstance( id ) ) {
|
||||
id = keyType.semiResolve(
|
||||
entityEntry.getLoadedValue( foreignKeyPropertyName ),
|
||||
session,
|
||||
owner
|
||||
owner
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -450,10 +451,10 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
@Override
|
||||
public Object resolve(Object value, SharedSessionContractImplementor session, Object owner)
|
||||
throws HibernateException {
|
||||
|
||||
|
||||
return resolveKey( getKeyOfOwner( owner, session ), session, owner );
|
||||
}
|
||||
|
||||
|
||||
private Object resolveKey(Serializable key, SharedSessionContractImplementor session, Object owner) {
|
||||
// if (key==null) throw new AssertionFailure("owner identifier unknown when re-assembling
|
||||
// collection reference");
|
||||
|
@ -497,19 +498,19 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
public String getAssociatedEntityName(SessionFactoryImplementor factory)
|
||||
throws MappingException {
|
||||
try {
|
||||
|
||||
|
||||
QueryableCollection collectionPersister = (QueryableCollection) factory
|
||||
.getCollectionPersister( role );
|
||||
|
||||
|
||||
if ( !collectionPersister.getElementType().isEntityType() ) {
|
||||
throw new MappingException(
|
||||
"collection was not an association: " +
|
||||
collectionPersister.getRole()
|
||||
throw new MappingException(
|
||||
"collection was not an association: " +
|
||||
collectionPersister.getRole()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return collectionPersister.getElementPersister().getEntityName();
|
||||
|
||||
|
||||
}
|
||||
catch (ClassCastException cce) {
|
||||
throw new MappingException( "collection role is not queryable " + role );
|
||||
|
@ -682,7 +683,7 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
// for a null target, or a target which is the same as the original, we
|
||||
// need to put the merged elements in a new collection
|
||||
Object result = target == null || target == original ? instantiateResult( original ) : target;
|
||||
|
||||
|
||||
//for arrays, replaceElements() may return a different reference, since
|
||||
//the array length might not match
|
||||
result = replaceElements( original, result, owner, copyCache, session );
|
||||
|
@ -749,12 +750,12 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
|
||||
// check if collection is currently being loaded
|
||||
PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection( persister, key );
|
||||
|
||||
|
||||
if ( collection == null ) {
|
||||
|
||||
|
||||
// check if it is already completely loaded, but unowned
|
||||
collection = persistenceContext.useUnownedCollection( new CollectionKey(persister, key, entityMode) );
|
||||
|
||||
|
||||
if ( collection == null ) {
|
||||
|
||||
collection = persistenceContext.getCollection( new CollectionKey(persister, key, entityMode) );
|
||||
|
@ -781,15 +782,15 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( LOG.isTraceEnabled() ) {
|
||||
LOG.tracef( "Created collection wrapper: %s",
|
||||
MessageHelper.collectionInfoString( persister, collection,
|
||||
key, session ) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
collection.setOwner(owner);
|
||||
|
||||
return collection.getValue();
|
||||
|
@ -809,13 +810,13 @@ public abstract class CollectionType extends AbstractType implements Association
|
|||
}
|
||||
|
||||
/**
|
||||
* We always need to dirty check the collection because we sometimes
|
||||
* need to incremement version number of owner and also because of
|
||||
* We always need to dirty check the collection because we sometimes
|
||||
* need to incremement version number of owner and also because of
|
||||
* how assemble/disassemble is implemented for uks
|
||||
*/
|
||||
@Override
|
||||
public boolean isAlwaysDirtyChecked() {
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* 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.unidir;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.dialect.MySQL5Dialect;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class UnidirectionalOneToManyNonPkJoinColumnTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Customer.class,
|
||||
Order.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12064" )
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
// Save the entity on the One side
|
||||
Customer customer = new Customer();
|
||||
customer.idCode = "ABC";
|
||||
customer.translationId = 1L;
|
||||
|
||||
entityManager.persist(customer);
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
// Attempt to load the entity saved in the previous session
|
||||
entityManager.find(Customer.class, "ABC");
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "Customer")
|
||||
@Table(name = "tbl_customer")
|
||||
public static class Customer implements Serializable {
|
||||
|
||||
@Id
|
||||
public String idCode;
|
||||
|
||||
public Long translationId;
|
||||
|
||||
@Fetch(FetchMode.JOIN)
|
||||
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
|
||||
@JoinColumn(name = "translationId", referencedColumnName = "translationId")
|
||||
public List<Order> translations;
|
||||
}
|
||||
|
||||
@Entity(name = "Order")
|
||||
@Table(name = "tbl_order")
|
||||
public static class Order {
|
||||
|
||||
@Id
|
||||
public long id;
|
||||
|
||||
public long translationId;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue