twigmac
twigmac

Reputation: 1813

How can I let tables be created automatically from entity classes with PostgreSQL and JPA?

I need to know whether automatic table creation is possible using PostgreSQL instead of Derby.

The story: I successfully set up a NetBeans Java project with Hibernate and Derby. I have created JPA controller classes from my entity classes. To test the project I created JUnit test classes for those controller classes.

When running those test classes for the first time, the tables will be automatically created in Derby. After executing the test, I can see the tables in the database browser.

In the persistence.xml there's a line like this

<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/MySampleDb;create=true"/>

setting up the connection and indicating the tables to be created when needed (note the ";create=true" at the end of the line).

When using PostgreSQL instead of Derby. This doesn't work anymore. Adding ";create=true" breaks the code. Is this automatic table creation possible with PostgreSQL at all? Should I use a different persistence library other than Hibernate?

Upvotes: 0

Views: 3782

Answers (1)

magomi
magomi

Reputation: 6685

Yes. This is possible. You need to set the hibernate.hbm2ddl.auto propery to create

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

Upvotes: 9

Related Questions