4

I am working on a new project with postgreSQL 13 and the auto generation of the data schema does not work. My approach is to use annotations on my entities as I always did before.
Here is one of my entities and my persistence.xml file:

@Entity
public class User implements Serializable {

    @OneToOne
    private Agent agent;

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idUser;
    
    private ProfilUser profilUser;
    private LocalDate dateCreation;
    private LocalDate dateActivation;
    private LocalDate date_desactivation;
    private LocalDate date_premiereConnexion;
    
 
    @ManyToMany
    private List <ProfilUser> userProfils;
    

    public Long getId() {
        return idUser;
    }

    public void setId(Long id) {
        this.idUser = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idUser != null ? idUser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the idUser fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.weyetech.gestionstock.entities.User[ id=" + idUser + " ]";
    }

    public LocalDate getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(LocalDate dateCreation) {
        this.dateCreation = dateCreation;
    }

    public LocalDate getDateActivation() {
        return dateActivation;
    }

    public void setDateActivation(LocalDate dateActivation) {
        this.dateActivation = dateActivation;
    }

    public LocalDate getDate_desactivation() {
        return date_desactivation;
    }

    public void setDate_desactivation(LocalDate date_desactivation) {
        this.date_desactivation = date_desactivation;
    }

    public LocalDate getDate_premiereConnexion() {
        return date_premiereConnexion;
    }

    public void setDate_premiereConnexion(LocalDate date_premiereConnexion) {
        this.date_premiereConnexion = date_premiereConnexion;
    }

    public Long getIdUser() {
        return idUser;
    }

    public void setIdUser(Long idUser) {
        this.idUser = idUser;
    }

    public ProfilUser getProfilUser() {
        return profilUser;
    }

    public void setProfilUser(ProfilUser profilUser) {
        this.profilUser = profilUser;
    }

    public List<ProfilUser> getUserProfils() {
        return userProfils;
    }

    public void setUserProfils(List<ProfilUser> userProfils) {
        this.userProfils = userProfils;
    }   
}

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <!-- Define Persistence Unit -->
  <persistence-unit name="g_stock" transaction-type="JTA">
    ... <!-- Entities -->
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

I tried to change the dialect of postgre to 10 (since it is the dialect of version 10 and later) but without success.

2 Answers2

0

I recommend not using the auto generate function of Hibernate as it creates unmaintainable database objects. If you are having issues and you need to manually check stuff in the database you will have trouble figuring out stuff. I personally would use liquibase or FlywayDB to handle the database migrations. Both are excellent products.

To quote (from point 8 here):

Hibernate can use the mapping information of your entities to generate a database schema. That’s the easiest approach, and you can see it in several examples on the internet. That might be OK for a small test application, but you shouldn’t use it for a business application. The database schema has a huge influence on the performance and size of your database. You, therefore, should design and optimize the database schema yourself and export it as an SQL script. You can run this script either with an external tool like Flyway or you can use Hibernate to initialize the database at startup. The following snippet shows a persistence.xml file which tells Hibernate to run the create.sql script to setup the database. You can learn more about the different configuration parameters in Standardized schema generation and data loading with JPA 2.1.

Ivo Limmen
  • 3,095
  • 1
  • 26
  • 23
  • I already have my entities beans. I want to generate the database. Generally relational has no problem when the OO programming is well done as far as I know. I never had any problem with MYSQL – user14238440 Aug 25 '21 at 07:58
0

You probably need to use the new official properties for schema generation.

<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>

Or try different settings from this nice overview

Ivo Limmen
  • 3,095
  • 1
  • 26
  • 23