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;
}
}