Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: syntax error at or near "user"

Tags:

I'm using Hibernate and have a persistent class called "User". Since this is a keyword, I marked the @Entity attribute with a different name (I have seen, for example, this question: Unable to use table named "user" in postgresql hibernate)

However, I still run into trouble because this class extends another, and it looks like hibernate is still trying to use "user" as a column name and getting messed up:

@Entity( name = "XonamiUser" ) public class User extends PropertyContainer {     ... 

and

@MappedSuperclass public abstract class PropertyContainer implements Serializable {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     protected long key;     /** tags */     @ElementCollection     protected Set<String> tags;      @ElementCollection     protected Set<String> searchList;     ... 

My other classes that extend PropertyContainer seem to work fine.

Is this a bug in hibernate? Is there some way around this short of refactoring? Thanks!

Here are the errors I'm seeing (slightly cleaned up):

WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 42601 ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into user_search_list (user, search_list) values ('1', 'bjorn') was aborted.  Call getNextException to see the cause. WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 42601 ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: syntax error at or near "user"   Position: 31 
like image 827
Bjorn Roche Avatar asked Jan 27 '12 15:01

Bjorn Roche


People also ask

What Is syntax error at or near?

This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: Using a database-specific SQL for the wrong database (eg BigQuery supports DATE_ADD, but Redshift supports DATEADD) Typo in the SQL (missing comma, misspelled word, etc)


1 Answers

user is a reserved keyword in PostgreSQL. It is allowed only as quoted identifier.

You have to force Hibernate to use quoted "user" in this INSERT command.

I'm not Hibernate expert but maybe this Hibernate saving User model to Postgres will help?

like image 71
filiprem Avatar answered Sep 19 '22 19:09

filiprem