1

I am trying to figure out the different types of generating primary keys for classes that inherit from a superclass, I am using the embedded H2 database, the data is stored in a file. Everything worked well, the entities that I had previously created manually in the database were loaded, but when I tried to save a new entity using Hibernate, I got an error

Table "ID_GENERATOR" not found.

I wonder, should I create the table for "ID_GENERATOR" table generator manually? I thought Hibernate creates it automatically

Here' my superclass:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class FundEvent implements Comparable<FundEvent>{
    @Id
    @TableGenerator(name = "id_generator")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "id_generator")
    protected int id;

    @Column(name = "amount")
    protected int amount;

    @Column(name = "deadline")
    protected Date deadline;

    @ManyToOne
    @JoinColumn(name = "user_id")
    protected User user;

    //getters and setters

And here's Entity class object of which I'm trying to save:

@Entity
@Table(name = "purchases")
public class Purchase extends FundEvent{
    @Column(name = "goal")
    private String goal;

    @Column(name = "bought")
    private boolean bought;

    @Column(name = "removed")
    private boolean removed;

    @Transient
    private int activeAmount;

    //getters and setters

Do I miss something?

UPD: Here's my hibernate.cfg.xml. Note, i'm not using Spring, just Hibernate

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:file:/home/vetal22331122/data_for_purch</property>
        <property name="connection.username"></property>
        <property name="connection.password"></property>
        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">2</property>
        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">none</property>
        <!-- dbcp connection pool configuration -->
        <property name="hibernate.dbcp.initialSize">5</property>
        <property name="hibernate.dbcp.maxTotal">20</property>
        <property name="hibernate.dbcp.maxIdle">10</property>
        <property name="hibernate.dbcp.minIdle">5</property>
        <property name="hibernate.dbcp.maxWaitMillis">-1</property>
        <mapping class="entities.Purchase"/>
        <!--<mapping class="entities.Payment"/>-->
        <mapping class="entities.User"/>
    </session-factory>
</hibernate-configuration>
vetal22331122
  • 65
  • 1
  • 1
  • 8

1 Answers1

0

Javadoc of TableGenerator showing an example of it filled with almost all the properties, try to fill in every property of the annotation and run again: https://docs.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html

wtsiamruk
  • 339
  • 3
  • 16
  • I've tried, result is the same. Besides, all properties except name are optional. So, Hibernate SHOULD create the table for table generator on its own, right? Does my problem has something to do with the fact that Id field is places in superclass? Here's complete exception: Sep 05, 2021 11:01:44 PM org.hibernate.id.enhanced.TableGenerator$1$1 execute ERROR: HHH000351: Could not read or init a hi value org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ID_GEN" not found; SQL statement: select tbl.GEN_VALUE from ID_GEN tbl where tbl.GEN_KEY=? for update [42102-200] – vetal22331122 Sep 05 '21 at 20:05
  • Actually it might, becuase of the strategy you use, as far as i remember ID generators are 1 per table and here your trying to have 1 per 2 tables..does it work if you remove the subsclass? – wtsiamruk Sep 05 '21 at 20:29