Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public boolean supportsBulkInsertionIdentifierGeneration() {
public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect) {
return null;
}

@Override
public boolean supportsJdbcBatchInserts() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.hibernate.id;

import java.io.Serializable;

import javax.persistence.GeneratedValue;

import org.hibernate.HibernateException;
Expand Down Expand Up @@ -60,4 +59,13 @@ public interface IdentifierGenerator {
* @throws HibernateException Indicates trouble generating the identifier
*/
Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException;

/**
* Check if JDBC batch inserts are supported.
*
* @return JDBC batch inserts are supported.
*/
default boolean supportsJdbcBatchInserts() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@
import org.hibernate.type.TypeHelper;
import org.hibernate.type.VersionType;

import static org.hibernate.internal.util.StringHelper.safeInterning;

/**
* Basic functionality for persisting an entity via JDBC
* through either generated or custom SQL
Expand All @@ -154,7 +152,7 @@
*/
public abstract class AbstractEntityPersister
implements OuterJoinLoadable, Queryable, ClassMetadata, UniqueKeyLoadable,
SQLLoadable, LazyPropertyInitializer, PostInsertIdentityPersister, Lockable {
SQLLoadable, LazyPropertyInitializer, PostInsertIdentityPersister, Lockable {

private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractEntityPersister.class );

Expand Down Expand Up @@ -3161,8 +3159,11 @@ protected void insert(
// TODO : shouldn't inserts be Expectations.NONE?
final Expectation expectation = Expectations.appropriateExpectation( insertResultCheckStyles[j] );
final int jdbcBatchSizeToUse = session.getConfiguredJdbcBatchSize();
final boolean useBatch = expectation.canBeBatched() && jdbcBatchSizeToUse > 1;
if ( useBatch && inserBatchKey == null) {
final boolean useBatch = expectation.canBeBatched() &&
jdbcBatchSizeToUse > 1 &&
getIdentifierGenerator().supportsJdbcBatchInserts();

if ( useBatch && inserBatchKey == null ) {
inserBatchKey = new BasicBatchKey(
getEntityName() + "#INSERT",
expectation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
Expand Down Expand Up @@ -113,8 +113,8 @@ public void doBatchInsertUpdateJoined(int nEntities, int nBeforeFlush) {
doInHibernate( this::sessionFactory, s -> {
int i = 0;
ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );

while ( sr.next() ) {
Employee e = (Employee) sr.get( 0 );
Expand All @@ -125,8 +125,8 @@ public void doBatchInsertUpdateJoined(int nEntities, int nBeforeFlush) {
doInHibernate( this::sessionFactory, s -> {
int i = 0;
ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );

while ( sr.next() ) {
Employee e = (Employee) sr.get( 0 );
Expand All @@ -135,6 +135,76 @@ public void doBatchInsertUpdateJoined(int nEntities, int nBeforeFlush) {
} );
}

@Test
public void testAssertSubclassInsertedSuccessfullyAfterCommit() {
final int nEntities = 10;

doInHibernate( this::sessionFactory, s -> {
for ( int i = 0; i < nEntities; i++ ) {
Employee e = new Employee();
e.setName( "Mark" );
e.setTitle( "internal sales" );
e.setSex( 'M' );
e.setAddress( "buckhead" );
e.setZip( "30305" );
e.setCountry( "USA" );
s.save( e );
}
} );

doInHibernate( this::sessionFactory, s -> {
long numberOfInsertedEmployee = (long) s.createQuery( "select count(e) from Employee e" ).uniqueResult();
Assert.assertEquals( nEntities, numberOfInsertedEmployee );
} );

doInHibernate( this::sessionFactory, s -> {
int i = 0;
ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );

while ( sr.next() ) {
Employee e = (Employee) sr.get( 0 );
s.delete( e );
}
} );

}

@Test
public void testAssertSubclassInsertedSuccessfullyAfterFlush() {

doInHibernate( this::sessionFactory, s -> {
Employee e = new Employee();
e.setName( "Mark" );
e.setTitle( "internal sales" );
e.setSex( 'M' );
e.setAddress( "buckhead" );
e.setZip( "30305" );
e.setCountry( "USA" );
s.save( e );
s.flush();

long numberOfInsertedEmployee = (long) s.createQuery( "select count(e) from Employee e" ).uniqueResult();
Assert.assertEquals( 1L, numberOfInsertedEmployee );
} );


doInHibernate( this::sessionFactory, s -> {
int i = 0;
ScrollableResults sr = s.createQuery(
"select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY );

while ( sr.next() ) {
Employee e = (Employee) sr.get( 0 );
s.delete( e );
}
} );

}


@Embeddable
public static class Address implements Serializable {

Expand Down
Loading