From 54f433541671da649d3ca8fe28c8fbbcd3c32f04 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 4 Sep 2012 15:00:48 -0500 Subject: [PATCH] HHH-7203 - IdentityGenerator fails with JOINED Inheritance when inserting entity to PosgtreSQL --- .../org/hibernate/id/IdentityGenerator.java | 6 +- .../id/SequenceIdentityGenerator.java | 2 +- ...ssHierarchyWithIdentityGenerationTest.java | 59 +++++++++++++++++++ .../idgen/identity/joinedSubClass/Sub.java | 36 +++++++++++ .../idgen/identity/joinedSubClass/Super.java | 48 +++++++++++++++ 5 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/JoinedSubclassHierarchyWithIdentityGenerationTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Sub.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Super.java diff --git a/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java index 0c90f1604e..57a60166ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IdentityGenerator.java @@ -99,7 +99,7 @@ public class IdentityGenerator extends AbstractPostInsertGenerator { rs = insert.getGeneratedKeys(); return IdentifierGeneratorHelper.getGeneratedIdentity( rs, - persister.getIdentifierColumnNames()[0], + persister.getRootTableKeyColumnNames()[0], persister.getIdentifierType() ); } @@ -150,7 +150,7 @@ public class IdentityGenerator extends AbstractPostInsertGenerator { try { return IdentifierGeneratorHelper.getGeneratedIdentity( rs, - persister.getIdentifierColumnNames()[0], + persister.getRootTableKeyColumnNames()[0], persister.getIdentifierType() ); } @@ -194,7 +194,7 @@ public class IdentityGenerator extends AbstractPostInsertGenerator { SessionImplementor session, ResultSet rs, Object object) throws SQLException { - return IdentifierGeneratorHelper.getGeneratedIdentity( rs, null, persister.getIdentifierType() ); + return IdentifierGeneratorHelper.getGeneratedIdentity( rs, persister.getRootTableKeyColumnNames()[0], persister.getIdentifierType() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/id/SequenceIdentityGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/SequenceIdentityGenerator.java index c7e1427f3f..8aa4aa85da 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/SequenceIdentityGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/SequenceIdentityGenerator.java @@ -111,7 +111,7 @@ public class SequenceIdentityGenerator insert.executeUpdate(); return IdentifierGeneratorHelper.getGeneratedIdentity( insert.getGeneratedKeys(), - getPersister().getIdentifierColumnNames()[0], + getPersister().getRootTableKeyColumnNames()[0], getPersister().getIdentifierType() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/JoinedSubclassHierarchyWithIdentityGenerationTest.java b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/JoinedSubclassHierarchyWithIdentityGenerationTest.java new file mode 100644 index 0000000000..3f27d7f9b8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/JoinedSubclassHierarchyWithIdentityGenerationTest.java @@ -0,0 +1,59 @@ +/* + * 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.idgen.identity.joinedSubClass; + +import org.hibernate.Session; + +import org.junit.Test; + +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +/** + * @author Andrey Vlasov + * @author Steve Ebersole + */ +@RequiresDialectFeature( DialectChecks.SupportsIdentityColumns.class ) +public class JoinedSubclassHierarchyWithIdentityGenerationTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Sub.class }; + } + + @Test + public void shouldPersistDebtorAccountWhenParentServiceAgreementPersisted() { + Session s = openSession(); + s.beginTransaction(); + s.save( new Sub() ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + s.createQuery( "delete Sub" ).executeUpdate(); + s.getTransaction().commit(); + s.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Sub.java b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Sub.java new file mode 100644 index 0000000000..db6c9cfc13 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Sub.java @@ -0,0 +1,36 @@ +/* + * 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.idgen.identity.joinedSubClass; + +import javax.persistence.Entity; +import javax.persistence.PrimaryKeyJoinColumn; + +/** + * @author Andrey Vlasov + * @author Steve Ebersole + */ +@Entity +@PrimaryKeyJoinColumn(name = "super_id") +public class Sub extends Super { +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Super.java b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Super.java new file mode 100644 index 0000000000..41f320e828 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/idgen/identity/joinedSubClass/Super.java @@ -0,0 +1,48 @@ +/* + * 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.idgen.identity.joinedSubClass; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; + +import static javax.persistence.GenerationType.IDENTITY; +import static javax.persistence.InheritanceType.JOINED; + +/** + * @author Andrey Vlasov + * @author Steve Ebersole + */ +@Entity +@Inheritance(strategy = JOINED) +public class Super { + @Id + @GeneratedValue(strategy = IDENTITY) + private Long id; + + @Column + private Long value; +}