Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres not creating tables properly with hibernate?

I've setup my hibernate app to use the following properties:

spring.datasource.url: jdbc:postgresql://localhost:5432/users
spring.datasource.username=james
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=create-drop
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database: MySql
spring.jpa.database =  postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

I've also created a postgres database users, but when I run my application, it throws all these unsuccessful alter table errors?!

Hibernate: drop table if exists user cascade
2016-06-23 10:46:05.406 ERROR 3364 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop table if exists user cascade
2016-06-23 10:46:05.407 ERROR 3364 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: syntax error at or near "user"
  Position: 22

Hibernate: alter table user_credential drop constraint FK_14ncv1m0gqncrbiagrs4uaqo8
2016-06-23 10:36:36.199 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_credential drop constraint FK_14ncv1m0gqncrbiagrs4uaqo8
2016-06-23 10:36:36.199 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_credential" does not exist
Hibernate: alter table user_roles drop constraint FK_5q4rc4fh1on6567qk69uesvyf
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint FK_5q4rc4fh1on6567qk69uesvyf
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_roles" does not exist
Hibernate: alter table user_roles drop constraint FK_g1uebn6mqk9qiaw45vnacmyo2
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint FK_g1uebn6mqk9qiaw45vnacmyo2
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_roles" does not exist

Everything works fine on mysql, it creates the tables without any dramas, I was using the following properties with mysql:

#spring.datasource.url: jdbc:mysql://localhost/users
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.hibernate.ddl-auto=create-drop
##spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database: MySql
#spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
##spring.jpa.database-platform=org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect

Could it be because I'm using a Point data type from vividsolutions?

import com.vividsolutions.jts.geom.Point;
@Column(name = "coords", columnDefinition="Geometry", nullable = true)
private Point location;

Do I have todo anything else to allow hibernate to use postgres? I've setup postgis on my postgres server as well.

I removed the Point declaration, and it still throws the same errors...So that's not the problem.

like image 461
James111 Avatar asked Jun 23 '16 00:06

James111


People also ask

Does hibernate work with PostgreSQL?

Out of the box, Hibernate works pretty well with PostgreSQL databases.

Does hibernate create tables automatically?

Hibernate framework can be used to create tables in the database automatically.

How configure hibernate PostgreSQL?

Connect Hibernate to PostgreSQL Data Switch to the Hibernate Configurations perspective: Window -> Open Perspective -> Hibernate. Right-click on the Hibernate Configurations panel and click Add Configuration. Set the Hibernate version to 5.2. Click the Browse button and select the project.

Why does Postgres need vacuum?

VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM is done. Therefore it's necessary to do VACUUM periodically, especially on frequently-updated tables.


1 Answers

It turns out you cannot have a table named user in postgres. So simply changing the table name from user to user_entity fixed this.

like image 96
James111 Avatar answered Dec 09 '22 13:12

James111