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.engine.spi.PersistenceContext;
|
||||||
import org.hibernate.internal.CoreLogging;
|
import org.hibernate.internal.CoreLogging;
|
||||||
import org.hibernate.loader.plan.exec.process.spi.CollectionReferenceInitializer;
|
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.exec.spi.CollectionReferenceAliases;
|
||||||
import org.hibernate.loader.plan.spi.CollectionReference;
|
import org.hibernate.loader.plan.spi.CollectionReference;
|
||||||
import org.hibernate.loader.plan.spi.Fetch;
|
import org.hibernate.loader.plan.spi.Fetch;
|
||||||
|
@ -85,6 +84,7 @@ public class CollectionReferenceInitializerImpl implements CollectionReferenceIn
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
final Serializable optionalKey = findCollectionOwnerKey( context );
|
final Serializable optionalKey = findCollectionOwnerKey( context );
|
||||||
|
|
||||||
if ( optionalKey != null ) {
|
if ( optionalKey != null ) {
|
||||||
// we did not find a collection element in the result set, so we
|
// we did not find a collection element in the result set, so we
|
||||||
// ensure that a collection is created with the owner's identifier,
|
// 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) {
|
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 collectionReference.getCollectionPersister().getCollectionType().getKeyOfOwner(
|
||||||
return null;
|
owner,
|
||||||
}
|
context.getSession()
|
||||||
return ownerState.getEntityKey().getIdentifier();
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -46,7 +46,6 @@ import org.hibernate.persister.entity.Joinable;
|
||||||
import org.hibernate.pretty.MessageHelper;
|
import org.hibernate.pretty.MessageHelper;
|
||||||
import org.hibernate.proxy.HibernateProxy;
|
import org.hibernate.proxy.HibernateProxy;
|
||||||
import org.hibernate.proxy.LazyInitializer;
|
import org.hibernate.proxy.LazyInitializer;
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -203,7 +202,7 @@ public abstract class CollectionType extends AbstractType implements Association
|
||||||
return "<uninitialized>";
|
return "<uninitialized>";
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<String> list = new ArrayList<String>();
|
final List<String> list = new ArrayList<>();
|
||||||
Type elemType = getElementType( factory );
|
Type elemType = getElementType( factory );
|
||||||
Iterator itr = getElementsIterator( value );
|
Iterator itr = getElementsIterator( value );
|
||||||
while ( itr.hasNext() ) {
|
while ( itr.hasNext() ) {
|
||||||
|
@ -397,7 +396,9 @@ public abstract class CollectionType extends AbstractType implements Association
|
||||||
// NOTE VERY HACKISH WORKAROUND!!
|
// NOTE VERY HACKISH WORKAROUND!!
|
||||||
// TODO: Fix this so it will work for non-POJO entity mode
|
// TODO: Fix this so it will work for non-POJO entity mode
|
||||||
Type keyType = getPersister( session ).getKeyType();
|
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(
|
id = keyType.semiResolve(
|
||||||
entityEntry.getLoadedValue( foreignKeyPropertyName ),
|
entityEntry.getLoadedValue( foreignKeyPropertyName ),
|
||||||
session,
|
session,
|
||||||
|
|
|
@ -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