Skip to content

Commit

Permalink
HHH-14219 Fix duplicated rows of sequence table
Browse files Browse the repository at this point in the history
if generator is shared by multiple entities
  • Loading branch information
quaff authored and beikov committed Sep 21, 2020
1 parent f136dab commit 6fb52c4
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
Expand Up @@ -249,8 +249,10 @@ public void registerExportables(Database database) {
);

Table table = namespace.locateTable( logicalQualifiedTableName.getObjectName() );
boolean tableCreated = false;
if ( table == null ) {
table = namespace.createTable( logicalQualifiedTableName.getObjectName(), false );
tableCreated = true;
}

this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
Expand All @@ -268,17 +270,19 @@ public void registerExportables(Database database) {
this.updateQuery = "update " + tableNameText +
" set " + valueColumnNameText + "= ?" +
" where " + valueColumnNameText + "=?";

ExportableColumn valueColumn = new ExportableColumn(
database,
table,
valueColumnNameText,
LongType.INSTANCE
);
table.addColumn( valueColumn );

table.addInitCommand(
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
);
if ( tableCreated ) {
ExportableColumn valueColumn = new ExportableColumn(
database,
table,
valueColumnNameText,
LongType.INSTANCE
);

table.addColumn( valueColumn );

table.addInitCommand(
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
);
}
}
}
@@ -0,0 +1,86 @@
/*
* 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.id.enhanced;

import static org.junit.Assert.assertEquals;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.query.Query;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.CustomRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* {@inheritDoc}
*
* @author Yanming Zhou
*/
@TestForIssue(jiraKey = "HHH-14219")
@RunWith(CustomRunner.class)
@RequiresDialect(MySQLDialect.class)
public class HHH14219 {

private SessionFactory sf;

@Before
public void setup() {
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder()

.applySetting("hibernate.show_sql", "true").applySetting("hibernate.format_sql", "true")
.applySetting("hibernate.hbm2ddl.auto", "create-drop");

Metadata metadata = new MetadataSources(srb.build()).addAnnotatedClass(BaseEntity.class)
.addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).buildMetadata();

sf = metadata.buildSessionFactory();
}

@Test
public void testSequenceTableContainsOnlyOneRow() throws Exception {
try (Session session = sf.openSession()) {
@SuppressWarnings("unchecked")
Query<Number> q = session.createNativeQuery("select count(*) from " + BaseEntity.SHARED_SEQ_NAME);
assertEquals(1, q.uniqueResult().intValue());
}
}

@MappedSuperclass
public static class BaseEntity {

public static final String SHARED_SEQ_NAME = "shared_seq";

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = SHARED_SEQ_NAME)
protected Long id;

}

@Entity
public static class Foo extends BaseEntity {

}

@Entity
public static class Bar extends BaseEntity {

}

}

0 comments on commit 6fb52c4

Please sign in to comment.