0

I have

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist

when session.save(item1); executes.
So why is that and how to make this simple hibernate application work?
I use Hibernate 5.6.3 and postgresSQL as RDBMS, postgres driver 42.3.1 and Lombok.
hibernate.cfg.xml stays in a resources folder.

Edit: I tried to change GenerationType.SEQUENCE -> GenerationType.IDENTITY as said in this question, but I have

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: org.postgresql.util.PSQLException: ERROR: relation "items" does not exist

also for session.save(item1); line.

DBInit.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import hibernatetest.entity.Item;

public class DBInit
{
    public static void main(String[] args)
    {
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        
        try (Session session = sessionFactory.openSession())
        {
            Item item1 = new Item(12);
            session.beginTransaction();
            session.save(item1);
            session.getTransaction().commit();
        }
    }
}

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/orm/cfg">
    <session-factory>
        <property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">123</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
        <property name="show_sql">true</property>
        <property name="hdm2ddl.auto">create</property>
        <property name="hibernate.format_sql">true</property>

        <mapping class="hibernatetest.entity.Item"/>
    </session-factory>
</hibernate-configuration>

Item.java

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;

@Entity
@Table(name = "items")
@NoArgsConstructor
public class Item
{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column
    @Getter
    @Setter
    private int id;
    
    @Getter
    @Setter
    @Column
    private int amount;
    
    public Item(int amount)
    {
        this.amount = amount;
    }
}
Denys_newbie
  • 1,140
  • 5
  • 15
  • Look at: https://stackoverflow.com/questions/29771730/postgres-error-in-batch-insert-relation-hibernate-sequence-does-not-exist-po – Romain Hippeau Feb 08 '22 at 12:56
  • 1
    `hdm2ddl.auto` should be `hibernate.hdm2ddl.auto`. The same for `dialect` and `show_sql`. I believe those are the old (probably ancient) names of the properties. Due to the property not matching, the schema isn't created. Instead of `create` I would suggest `update` as a value and not to use that in production but only for testing/development. – M. Deinum Feb 08 '22 at 13:19
  • @M.Deinum I changed `hdm2ddl.auto` -> `hibernate.hdm2ddl.auto` but nothing have changed, not for `GenerationType.SEQUENCE` and `GenerationType.IDENTITY` – Denys_newbie Feb 08 '22 at 13:24
  • Did you change from `create` to `update`? as a schema can be created only once. – M. Deinum Feb 08 '22 at 13:26
  • @M.Deinum Yes I changed `hdm2ddl.auto` -> `hibernate.hdm2ddl.auto` and `dialect` -> `hibernate.dialect` and `show_sql` -> `hibernate.show_sql` and `create` -> `update` but nothing changed so far (not for `GenerationType.SEQUENCE` and `GenerationType.IDENTITY`) – Denys_newbie Feb 08 '22 at 13:29
  • @M.Deinum It seems `xmlns="http://www.hibernate.org/xsd/orm/cfg"` namespace should make just `hdm2ddl.auto` work (without '.hibernate'), but I am not sure so I added `.hibernate' – Denys_newbie Feb 08 '22 at 13:33
  • 1
    you should add `hibernate.` not `.hibernate`. Your property is also wrongly named it should be `hibernate.hbm2ddl.auto` **not** `hibernate.hdm2ddl.auto`. Notice the `b` instead of a `d`! – M. Deinum Feb 08 '22 at 13:35
  • Thanks a lot, I changed `hdm` -> `hbm` and it works. I am so sorry for such a stupid question and for my inattention – Denys_newbie Feb 08 '22 at 13:39

0 Answers0