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"/>