111

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's the best and most convenient way for me to generate the ID's? I do not want to change my inheritance strategy.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

5 Answers5

251

The problem here is that you mix "table-per-class" inheritance and GenerationType.Auto. Consider an identity column in MsSQL. It is column based. In a "table-per-class" strategy you use one table per class and each one has an ID.

Try:

@GeneratedValue(strategy = GenerationType.TABLE)

Bart
  • 17,070
  • 5
  • 61
  • 80
zoidbeck
  • 4,091
  • 1
  • 26
  • 25
  • 2
    Perfect Solution. Even Hibernate forums didnot seem to have this solution, and they were going around the topic https://forum.hibernate.org/viewtopic.php?p=2319244&sid=4493aa54d27d3f81a0e27ecbdda075ae – Spring Monkey Aug 14 '09 at 15:00
  • 1
    Is this issue is with MySql only or its regular as I watch one of a video for Table per class approach and it was working fine in that postgres was used – Prashant Jan 22 '14 at 05:47
  • 1
    I ran into this recently when testing a Dropwizard application. In my case I addressed it by making sure to use the same configuration options used by DW to create the session factory. I'm pretty sure setting the property "hibernate.id.new_generator_mappings" to true is what fixed it. This is DW 0.7.0, Hibernate 4.3.1, DB was H2. – sfitts Jun 12 '14 at 20:51
  • 2
    It works perfectly. However, It is highly recommended not to prefer using 'TABLE' id generation strategy as it triggers lots of queries (select, insert, and update) and maintains locks to generate unique values for primary key. So what would be an alternate solution to this If we have a kinda big Inheritance scenario? It will certainly impact the performance at great extent. – Ram Aug 02 '18 at 19:24
8

I wonder if this is a database dialect specific problem, since watching a youtube tutorial with PostgreSQL as the underlying database I saw that the creator of the video run succefully an app with the default @GeneratedValue. In my case (the underlying database is MySQL) I had to modify the @GeneratedValue strategy to GenerationType.TABLE exactly as zoidbeck proposes.

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

skiabox
  • 3,449
  • 12
  • 63
  • 95
  • Postgres is able to do inheritance so you might be right that this is database specific: http://www.postgresql.org/docs/8.1/static/ddl-inherit.html The video does not explain (or i missed it) how the schema is generated. So maybe the NHibernate postgres dialect is able to do it by its own or instead you have to add the 'INHERITS' manually. Actually i can not tell. – zoidbeck Mar 20 '13 at 15:09
  • 7
    On PostgreSQL, Hibernate defaults to use `GenerationType.SEQUENCE`. That's why it works automatically there. It has absolutely _nothing_ to do with PostgreSQLs `INHERITS`. – Henning Apr 07 '14 at 10:33
  • i have been using the same tutorial & using @Generated was causing issue as i am using MySql.Spent a lot of time debugging until i saw this post – Deen John Jul 15 '16 at 16:59
8

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

@GeneratedValue(strategy = GenerationType.TABLE)

But that's not all, you need to create a new table, what will hold your abstract's table primary key sequence. Modify your mapping to

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

And a new table in database should look like following: enter image description here

When you ran your application, Hibernate will insert a row where sequence_name will be the entity name (SuperClass in this example) and sequence_next_hi_value value will be automatically incremented and used for new records of all implementing subclasses's tables.

Gondy
  • 4,925
  • 4
  • 40
  • 46
3

you can use @MappedSuperclass for inheritance

leimbag
  • 239
  • 2
  • 3
2

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

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

Ryan Walls
  • 6,962
  • 1
  • 39
  • 42