Cannot use id column key generation with <union-subclass> (TABLE_PER_CLASS)

com.something.SuperClass:

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class SuperClass implements Serializable { private static final long serialVersionUID = -695503064509648117L; long confirmationCode; @Id @GeneratedValue(strategy = GenerationType.AUTO) // Causes exception!!! public long getConfirmationCode() { return confirmationCode; } public void setConfirmationCode(long confirmationCode) { this.confirmationCode = confirmationCode; } } 

com.something.SubClass:

 @Entity public abstract class Subclass extends SuperClass { private static final long serialVersionUID = 8623159397061057722L; String name; @Column(nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Gives me this exception:

 Caused by: org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: com.something.SuperClass 

What is the best and most convenient way to create identifiers? I do not want to change my inheritance strategy.

+81
inheritance hibernate jpa class-table-inheritance
May 27 '09 at 14:49
source share
6 answers

The problem is that you are mixing table-per-class inheritance and GenerationType.Auto . Consider the identity column in MsSQL. These are the columns. In the table by class strategy, you use one table for each class, and each has an identifier.

Try:

@GeneratedValue(strategy = GenerationType.TABLE)

+195
May 28 '09 at 10:29
source share

I wonder if this is the problem of the specific dialect of the database, because while watching the youtube tutorial with PostgreSQL as the base database, I saw that the video creator launches the default application @GeneratedValue by default. In my case (the base database is MySQL) I had to change @GeneratedValue's strategy to GenerationType.TABLE exactly the same as zoidbeck suggests.

Here is the video: https://www.youtube.com/watch?v=qIdM4KQOtH8

+8
Mar 10 '13 at 15:52
source share

In our case, we use the PostreSQL database for dev and production and the in-memory hsqldb database for tests. We use a sequence in both cases to generate an id. Apparently GenerationType.AUTO uses SEQUENCE for postgres by default, but failed in our local tests (default for hsqldb).

So, the solution that worked for us explicitly uses GenerationType.SEQUENCE .

+2
Aug 06 '14 at
source share

Agree with zoidbeck's answer. You need to change the strategy:

 @GeneratedValue(strategy = GenerationType.TABLE) 

But thatโ€™s not all, you need to create a new table that will contain the sequence of primary keys of the abstract table. Change the display to

 @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "ConfirmationCodeGenerator") @TableGenerator(table = "SEQUENCES", name = "ConfirmationCodeGenerator") public long getConfirmationCode() { return confirmationCode; } 

And the new table in the database should look like this: enter image description here

When you start the application, Hibernate inserts a line in which sequence_name will be the name of the entity ( SuperClass in this example), and the value of sequence_next_hi_value will automatically increase and be used for new records of all tables implementing subclasses.

+2
Jun 21 '15 at 13:32
source share

There is a standard SQL mapping between MySQL and PostgreSQL. PostgreSQL Postgres understands a good subset of SQL92 / 99 plus some object-oriented functions for these subsets. Postgres is capable of handling complex procedures and rules like declarative SQL queries, subqueries, views, multi-user support, transactions, query optimization, inheritance, and arrays. Does not support data selection in different databases.

MySQL MySQL uses SQL92 as the basis. It runs on countless platforms. Mysql can create queries that can join tables from different databases. Supports both left and right outer joins using ANSI and ODBC syntax. Starting with MySQL 4.1 from this version, MySQL processes subqueries. Views are supported since version 5.

Detailed description, please visit. http://www-css.fnal.gov/dsg/external/freeware/pgsql-vs-mysql.html

0
Sep 05 '14 at 15:14
source share

you can use @MappedSuperclass for inheritance

0
Apr 7 '17 at 2:08 on
source share



All Articles