0

Whenever I run this code, hibernate will drop the current tables/ create a new one and fill it again:

public static void main(String[] args) {
   UserRepository userRepository = UserRepository.getInstance();
   UserEntity user = new UserEntity("foo", "foo@example.com", 4321);
   userRepository.save(user);
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/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_1.xsd">
  <persistence-unit name="web" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/web"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.password" value="1234"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="javax.persistence.schema-generation.database.action" value="update"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

For example if I run the main once, I will find in the the database:

id    |     username     |    e-mail        |    bar
1     |        foo       | foo@example.org  |   4321

As expected, however if I change the UserEntity user = new UserEntity("bar", "bar@example.com", 4321); and run again:

The database will only show:

id    |     username     |    e-mail        |    bar
1     |        bar       | bar@example.org  |   4321

Shouldn't hibernate keep the databases since I am using?

<property name="hibernate.hbm2ddl.auto" value="update"/>

Mansueli
  • 6,223
  • 8
  • 33
  • 57

2 Answers2

1

You do not need to use the property javax.persistence.schema-generation.database.action. This property sets the generation policy for schema i.e. drop/create behaviour, as stated in https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm

Yuval Zilberstein
  • 175
  • 1
  • 4
  • 11
1

You have this property javax.persistence.schema-generation.database.action

The values for this property are none, create, drop-and-create, drop.

update is not a valid value for this property.

Additionally, you already have hibernate.hbm2ddl.auto property, so I suggest just remove the first one.

Ish
  • 3,992
  • 1
  • 17
  • 23