HHH-13456 refactor ForeignGenerator to allow for the possibility of StatelessSession

This commit is contained in:
Nathan Xu 2019-11-28 23:04:04 -05:00 committed by Steve Ebersole
parent 8ce1404754
commit 3a69b74894
3 changed files with 89 additions and 7 deletions

View File

@ -11,6 +11,7 @@
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.TransientObjectException;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -21,7 +22,6 @@
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import static org.hibernate.internal.CoreLogging.logger;
import static org.hibernate.internal.CoreLogging.messageLogger;
/**
@ -80,9 +80,6 @@ public void configure(Type type, Properties params, ServiceRegistry serviceRegis
@Override
public Serializable generate(SharedSessionContractImplementor sessionImplementor, Object object) {
// needs to be a Session for the #save and #contains calls below...
final Session session = ( Session ) sessionImplementor;
final EntityPersister persister = sessionImplementor.getFactory().getMetamodel().entityPersister( entityName );
Object associatedObject = persister.getPropertyValue( object, propertyName );
if ( associatedObject == null ) {
@ -117,10 +114,20 @@ public Serializable generate(SharedSessionContractImplementor sessionImplementor
foreignValueSourceType.getAssociatedEntityName()
);
}
id = session.save( foreignValueSourceType.getAssociatedEntityName(), associatedObject );
if (sessionImplementor instanceof Session) {
id = ((Session) sessionImplementor)
.save(foreignValueSourceType.getAssociatedEntityName(), associatedObject);
}
else if (sessionImplementor instanceof StatelessSession) {
id = ((StatelessSession) sessionImplementor)
.insert(foreignValueSourceType.getAssociatedEntityName(), associatedObject);
}
else {
throw new IdentifierGenerationException("sessionImplementor is neither Session nor StatelessSession");
}
}
if ( session.contains( entityName, object ) ) {
if ( sessionImplementor instanceof Session && ((Session) sessionImplementor).contains( entityName, object ) ) {
//abort the save (the object is already saved by a circular cascade)
return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");

View File

@ -0,0 +1,70 @@
package org.hibernate.test.id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Nathan Xu
*/
public class ForeignGeneratorTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Product.class,
ProductDetails.class
};
}
@Test
@TestForIssue( jiraKey = "HHH-13456")
public void testForeignGeneratorInStatelessSession() {
inStatelessSession(statelessSession -> {
Transaction tx = statelessSession.beginTransaction();
Product product = new Product();
ProductDetails productDetails = new ProductDetails( product );
statelessSession.insert( productDetails );
tx.commit();
});
}
@Entity(name = "Product")
public static class Product {
@Id
@GeneratedValue
private Long id;
}
@Entity(name = "ProductDetails")
public static class ProductDetails {
@Id
@GeneratedValue
private Long id;
@OneToOne
@MapsId
private Product product;
public ProductDetails() {
}
public ProductDetails( Product product ) {
this.product = product;
}
}
}

View File

@ -20,6 +20,7 @@
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
@ -387,7 +388,7 @@ protected void cleanupCache() {
sessionFactory.getCache().evictAllRegions();
}
}
protected boolean isCleanupTestDataRequired() {
return false;
}
@ -517,4 +518,8 @@ protected void inTransaction(SessionImplementor session, Consumer<SessionImpleme
protected void inSession(Consumer<SessionImplementor> action) {
TransactionUtil2.inSession( sessionFactory(), action );
}
protected void inStatelessSession(Consumer<StatelessSession> action) {
TransactionUtil2.inStatelessSession( sessionFactory(), action );
}
}