java - Spring Data JPA 查询不起作用,列不存在

标签 java spring hibernate spring-mvc jpa

我在我的网络应用程序中使用 spring data jpa,我有实体用户

@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
}

我有扩展 CrudRepository 的 UserRepository 接口(interface)。 当我在我的 Controller 中调用方法 findAll 时,出现此错误

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

我的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>

我在 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)
)

最佳答案

User 是 PostgreSQL 中的保留关键字。使用默认命名策略,您可能有 User 表名。

不知道为什么 @Table(name = "user", schema = "public") 有效。也许 PostgreSQL 不会将 public.user 视为与 User 相对的关键字。

请为表格使用复数名称。对表名使用系统或子系统前缀 (xxx_users) 也是一个好主意。

命名策略可以用于这种方法。引用这个例子:Hibernate5NamingStrategy

前缀示例: StrategyOptions

关于java - Spring Data JPA 查询不起作用,列不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36971264/

相关文章:

java - 当我在 for 循环中使用 ArrayList 时,如何将对象添加到 ArrayList 中?

ajax - Spring security session 超时处理 Ajax 调用重定向到登录不起作用

java - 分布式组件生命周期管理

java - 涉及多个关联的 HQL JOIN

java - Tomcat 解密 aes 与 Java 主应用程序不同

java - 有没有办法在 Oracle 11g XE 上安装 java?

oracle - 如何在 Oracle 数据库的 HQL 查询中使用当前日期?

java - Hibernate - MySQL - 连接到数据库

java - Eclipse 不显示 TextView

java - 改造 GSON 将日期从 json 字符串序列化为 long 或 java.lang.Long