ERROR: syntax error at or near user

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

However, I still run into problems because this class extends another, and it looks like hibernate is still trying to use "user" as the column name and gets confused:

@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 the PropertyContainer seem to be working fine.

Is this a bug in sleep mode? Is there any way around this refactoring? Thanks!

Here are the errors that I see (slightly cleared):

 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 
+6
source share
4 answers

In the end, I reorganized the class user into XonamiUser. This was not exactly what I wanted, but it worked perfectly.

+1
source

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

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

I'm not a Hibernate expert, but maybe this hibernation model for Postgres will help?

+30
source

I'm a little confused when referring to a column or table named User ... You can use @Table -nnotation to set the table name for the object in JPA:

 @Entity @Table(name = "XonamiUser") public class User extends PropertyContainer { 
+1
source

The Hibernate Reference Guide shows how to determine the table name used to store collection items. And for this, a user object is used, and to define a column, user_id , not the user.

This mapping should be OK, for your case:

 @ElementCollection @CollectionTable(name = "user_search_list", joinColumns = @JoinColumn(name = "user_id")) @Column(name = "search_list") protected Set<String> searchList; 
+1
source

Source: https://habr.com/ru/post/907039/


All Articles