Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use table named "user" in postgresql hibernate

When I try to persist an entity called "user" with JPA/hibernate it does not work. The table is not created and it is because user is a reserved word in postgresql. Is there any way other than naming the table something else to make this work?

like image 393
Piotr Avatar asked Dec 03 '10 23:12

Piotr


People also ask

Does hibernate work with PostgreSQL?

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

Is user a keyword in PostgreSQL?

user is a pseudo-function keyword.

Does Postgres have user table?

PostgreSQL: Find Users in PostgreSQL Answer: In PostgreSQL, there is a system table called pg_user. You can run a query against this system table that returns all of the Users that have been created in PostgreSQL as well as information about these Users.


1 Answers

To quote an identifier, use back ticks:

@Table(name="`users`") 

See this example from Hibernate's test suite:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/hibernate/test/quote/User.java#L31

Hibernate will automatically detect it and convert to the appropriate quote for the database you are using.

like image 90
jpkrohling Avatar answered Nov 13 '22 15:11

jpkrohling