3

I use spring data jpa in my web-app, i have entity user

@Entity public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String password; private String email; private Boolean enabled; private String name; private String lastname; private String userRole; public User() { } public User(String password, String email, Boolean enabled, String name, String lastname, String userRole) { this.password = password; this.email = email; this.enabled = enabled; this.name = name; this.lastname = lastname; this.userRole = userRole; } @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq") @SequenceGenerator(name="users_id_seq", sequenceName="users_id_seq", allocationSize = 1) @Column(name = "id", nullable = false) public Long getId() { return id; } //Other columns } 

And i have UserRepository interface which extends CrudRepository. When i call method findAll in my Controller, I get this error

01-May-2016 22:45:58.674 WARN [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 0, SQLState: 42703 01-May-2016 22:45:58.675 ERROR [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Error: column user0_.id does not exist Position: 8 

My spring-config.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="com.birthright.repository"/> <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.birthright.entity"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <tx:annotation-driven/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="url" value="jdbc:postgresql://localhost:5432/AutoService"/> <property name="username" value="postgres"/> <property name="password" value="root"/> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="myEmf"/> </bean> </beans> 

My table in postgresql

CREATE TABLE public."user" ( id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass), password character varying, email character varying, enabled boolean, name character varying, lastname character varying, user_role character varying, CONSTRAINT users_pkey PRIMARY KEY (id) ) 
6
  • I guess the column exists in your table? Commented May 1, 2016 at 20:04
  • Yes, and i don't know where the prefix "user0_." is taken. Commented May 1, 2016 at 20:11
  • It's just the alias used by hibernate to name your user table Commented May 1, 2016 at 20:18
  • Is there any reason why you've got the id-related annotations into the getter method and not in the field itself? Commented May 1, 2016 at 20:27
  • 1
    @Birthright Please, never use table names like User. You will have a lot of problems. Just users. Commented May 1, 2016 at 20:39

1 Answer 1

9

User is a reserved keyword in PostgreSQL. With a default naming strategy you, probably, had User table name.

Don't know why @Table(name = "user", schema = "public") works. Maybe PostgreSQL doesn't consider public.user as a keyword opposite User.

Please use plural names for tables. And using a system or subsystem prefix for a table name (xxx_users) is a good idea too.

A naming strategy can be used for such approach. Refer this as an example: Hibernate5NamingStrategy

An example of prefixes: StrategyOptions

Sign up to request clarification or add additional context in comments.

4 Comments

You're right. I remove these annotation and renamed table in the "users" and also all the work.
@Birthright It is very good. Check a naming strategy, our team uses it to don't have such problems. Column naming can be error prone too.
"User is a reserved keyword in PostgreSQL."... half a day wasted... Thank you for the rescue...
@PetrDvořák You are welcome. Maybe you will help me in the next life :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.