Cannot use identity column key generation with ( TABLE_PER_CLASS )

后端 未结 6 2031
囚心锁ツ
囚心锁ツ 2020-12-02 07:12

com.something.SuperClass:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SuperClass implements Seria         


        
相关标签:
6条回答
  • 2020-12-02 07:52

    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.

    0 讨论(0)
  • 2020-12-02 07:57

    There is a SQL standard Compliance in between MySQL and PostgreSQL. PostgreSQL Postgres understands a good subset of SQL92/99 plus some object-oriented features to these subsets. Postgres is capable of handling complex routines and rules as declarative SQL queries, subqueries, views, multi-user support, transactions, query optimization, inheritance, and arrays. Does not support selecting data across different databases.

    MySQL MySQL uses SQL92 as its foundation. Runs on countless platforms. Mysql can construct queries that can join tables from different databases. Supports both left and right outer joins using both ANSI and ODBC syntax. As of MySQL 4.1 from that release on, MySQL will handle subqueries. Views supported as of release 5.

    For a detailed description please visit. http://www-css.fnal.gov/dsg/external/freeware/pgsql-vs-mysql.html

    0 讨论(0)
  • 2020-12-02 07:59

    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)

    0 讨论(0)
  • 2020-12-02 08:09

    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.

    0 讨论(0)
  • 2020-12-02 08:15

    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

    0 讨论(0)
  • 2020-12-02 08:15

    you can use @MappedSuperclass for inheritance

    0 讨论(0)
提交回复
热议问题