HHH-6580 - Discriminator value used as ID when inserting entity to PosgtreSQL

This commit is contained in:
Steve Ebersole 2012-01-25 15:56:08 -06:00
parent f69813fad0
commit 917eef5ad1
11 changed files with 113 additions and 21 deletions

View File

@ -77,9 +77,11 @@ public final class IdentifierGeneratorHelper {
* Get the generated identifier when using identity columns
*
* @param rs The result set from which to extract the the generated identity.
* @param identifier
* @param identifier The name of the identifier column
* @param type The expected type mapping for the identity value.
*
* @return The generated identity value
*
* @throws SQLException Can be thrown while accessing the result set
* @throws HibernateException Indicates a problem reading back a generated identity value.
*/
@ -97,9 +99,11 @@ public final class IdentifierGeneratorHelper {
* and wrp it in the appropriate Java numeric type.
*
* @param rs The result set from which to extract the value.
* @param identifier
* @param identifier The name of the identifier column
* @param type The expected type of the value.
*
* @return The extracted value.
*
* @throws SQLException Indicates problems access the result set
* @throws IdentifierGenerationException Indicates an unknown type.
*/
@ -139,7 +143,8 @@ public final class IdentifierGeneratorHelper {
"unrecognized id type : " + type.getName() + " -> " + clazz.getName()
);
}
} else {
}
else {
if ( clazz == Long.class ) {
return rs.getLong(identifier);
}

View File

@ -92,14 +92,14 @@ public class IdentityGenerator extends AbstractPostInsertGenerator {
.prepareStatement( insertSQL, PreparedStatement.RETURN_GENERATED_KEYS );
}
public Serializable executeAndExtract(PreparedStatement insert, String identifier) throws SQLException {
public Serializable executeAndExtract(PreparedStatement insert) throws SQLException {
insert.executeUpdate();
ResultSet rs = null;
try {
rs = insert.getGeneratedKeys();
return IdentifierGeneratorHelper.getGeneratedIdentity(
rs,
identifier,
persister.getIdentifierColumnNames()[0],
persister.getIdentifierType()
);
}
@ -140,7 +140,7 @@ public class IdentityGenerator extends AbstractPostInsertGenerator {
.prepareStatement( insertSQL, PreparedStatement.NO_GENERATED_KEYS );
}
public Serializable executeAndExtract(PreparedStatement insert, String identifier) throws SQLException {
public Serializable executeAndExtract(PreparedStatement insert) throws SQLException {
if ( !insert.execute() ) {
while ( !insert.getMoreResults() && insert.getUpdateCount() != -1 ) {
// do nothing until we hit the rsult set containing the generated id
@ -148,7 +148,11 @@ public class IdentityGenerator extends AbstractPostInsertGenerator {
}
ResultSet rs = insert.getResultSet();
try {
return IdentifierGeneratorHelper.getGeneratedIdentity( rs, identifier, persister.getIdentifierType() );
return IdentifierGeneratorHelper.getGeneratedIdentity(
rs,
persister.getIdentifierColumnNames()[0],
persister.getIdentifierType()
);
}
finally {
rs.close();

View File

@ -50,6 +50,8 @@ public interface PostInsertIdentityPersister extends EntityPersister {
*/
public String getIdentitySelectString();
public String[] getIdentifierColumnNames();
/**
* The names of the primary key columns in the root table.
*

View File

@ -107,11 +107,12 @@ public class SequenceIdentityGenerator
}
@Override
protected Serializable executeAndExtract(PreparedStatement insert, String identifier) throws SQLException {
protected Serializable executeAndExtract(PreparedStatement insert) throws SQLException {
insert.executeUpdate();
return IdentifierGeneratorHelper.getGeneratedIdentity(
insert.getGeneratedKeys(),
identifier, getPersister().getIdentifierType()
getPersister().getIdentifierColumnNames()[0],
getPersister().getIdentifierType()
);
}
}

View File

@ -46,13 +46,16 @@ public abstract class AbstractReturningDelegate implements InsertGeneratedIdenti
this.persister = persister;
}
public final Serializable performInsert(String insertSQL, String identifiers, SessionImplementor session, Binder binder) {
public final Serializable performInsert(
String insertSQL,
SessionImplementor session,
Binder binder) {
try {
// prepare and execute the insert
PreparedStatement insert = prepare( insertSQL, session );
try {
binder.bindValues( insert );
return executeAndExtract( insert, identifiers );
return executeAndExtract( insert );
}
finally {
releaseStatement( insert, session );
@ -73,7 +76,7 @@ public abstract class AbstractReturningDelegate implements InsertGeneratedIdenti
protected abstract PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException;
protected abstract Serializable executeAndExtract(PreparedStatement insert, String identifiers) throws SQLException;
protected abstract Serializable executeAndExtract(PreparedStatement insert) throws SQLException;
protected void releaseStatement(PreparedStatement insert, SessionImplementor session) throws SQLException {
insert.close();

View File

@ -46,7 +46,10 @@ public abstract class AbstractSelectingDelegate implements InsertGeneratedIdenti
this.persister = persister;
}
public final Serializable performInsert(String insertSQL, String identifier, SessionImplementor session, Binder binder) {
public final Serializable performInsert(
String insertSQL,
SessionImplementor session,
Binder binder) {
try {
// prepare and execute the insert
PreparedStatement insert = session.getTransactionCoordinator()

View File

@ -50,12 +50,13 @@ public interface InsertGeneratedIdentifierDelegate {
* Perform the indicated insert SQL statement and determine the identifier value
* generated.
*
*
* @param insertSQL The INSERT statement string
* @param identifiers
* @param session The session in which we are operating
* @param binder The param binder
*
* @return The generated identifier value.
*/
public Serializable performInsert(String insertSQL, String identifiers, SessionImplementor session, Binder binder);
public Serializable performInsert(String insertSQL, SessionImplementor session, Binder binder);
}

View File

@ -2756,9 +2756,7 @@ public abstract class AbstractEntityPersister
}
};
String[] identifiers = getIdentifierColumnNames();
return identityDelegate.performInsert( sql, identifiers[0], session, binder );
return identityDelegate.performInsert( sql, session, binder );
}
public String getIdentitySelectString() {

View File

@ -1,3 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.inheritancediscriminator;
import javax.persistence.Column;
@ -5,7 +28,7 @@ import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Created by Pawel Stawicki on 8/17/11 11:01 PM
* @author Pawel Stawicki
*/
@Entity
@DiscriminatorValue("1")

View File

@ -1,3 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.inheritancediscriminator;
import javax.persistence.Column;
@ -12,9 +35,8 @@ import static javax.persistence.GenerationType.IDENTITY;
import static javax.persistence.InheritanceType.SINGLE_TABLE;
/**
* Created by Pawel Stawicki on 8/17/11 11:01 PM
* @author Pawel Stawicki
*/
@Entity
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "CLASS_ID", discriminatorType = INTEGER)

View File

@ -1,9 +1,39 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.inheritancediscriminator;
import org.hibernate.Session;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Pawel Stawicki
*/
@RequiresDialect( value = PostgreSQLDialect.class, jiraKey = "HHH-6580" )
public class PersistChildEntitiesWithDiscriminatorTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {