Home > database >  I don't know why my very simple JPA project doesn't work
I don't know why my very simple JPA project doesn't work

Time:12-18

I have org.postgresql.util.PSQLException: ERROR: relation "roles" does not exist, and I don't know why.

Entity class

package com.example.SpringBootTest1.model;

import lombok.*;
import javax.persistence.*;

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "roles")
public class Role
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Setter
    @Getter
    private int id;
    
    @Setter
    @Getter
    @Column(name = "name")
    private String name;
}

main()

public static void main(String[] args)
{
        
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("role_pu");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();
        
    Role role1 = new Role();
    role1.setName("role111");
    entityManager.persist(role1);
    entityManager.getTransaction().commit();
    entityManagerFactory.close();
    entityManager.close();
}

resources/META-INF/persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="role_pu" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>com.example.SpringBootTest1.model.Role</class>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.user" value="postgres"/>
            <property name="javax.persistence.jdbc.password" value="123"/>

            <property name="dialect" value="org.hibernate.dialect.PostgreSQL94Dialect"/>
            <property name="show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hdm2ddl.auto" value="create"/>
        </properties>

    </persistence-unit>
</persistence>

In pom.xml I have
hibernate-core
postgresql
lombok
So why I have such error and why is that? I have read this question and it doesn't help me.

CodePudding user response:

In modern Hibernate versions, the property hdm2ddl.auto should be hibernate.hbm2ddl.auto or javax.persistence.schema-generation.database.action.

https://docs.jboss.org/hibernate/stable/orm/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl

  • Related