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

Posted

技术标签:

【中文标题】Spring Data JPA 查询不起作用,列不存在【英文标题】:Spring Data JPA query doesn't work, column does not exists 【发布时间】:2016-08-26 13:24:14 【问题描述】:

我在我的网络应用程序中使用 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 接口。 当我在 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)
)

【问题讨论】:

我猜你的表中存在该列? 是的,我不知道前缀“user0_”在哪里。被拍了。 只是hibernate用来命名你的用户表的别名 是否有任何理由将与 id 相关的注释放入 getter 方法而不是字段本身? @Birthright 请不要使用像User 这样的表名。你会遇到很多问题。只需users 【参考方案1】:

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

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

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

命名策略可以用于这种方法。以此为例:Hibernate5NamingStrategy

前缀示例: StrategyOptions

【讨论】:

你是对的。我删除了“用户”中的这些注释和重命名表以及所有工作。 @Birthright 非常好。检查命名策略,我们的团队使用它来避免此类问题。列命名也容易出错。 “用户是PostgreSQL中的保留关键字。”...浪费了半天...谢谢救援... @PetrDvořák 欢迎您。也许你会在下辈子帮助我:)

以上是关于Spring Data JPA 查询不起作用,列不存在的主要内容,如果未能解决你的问题,请参考以下文章

Spring Data JPA自定义查询限制功能不起作用[重复]

Spring Data JPA - 带有“@Param Date”的自定义@Query 不起作用

Spring-data-jpa:批量插入不起作用

Spring -data-jpa ,存储库类不起作用

Spring Data JPA主键违规约束不起作用

从父类更新日期字段在 Spring Data Jpa 中不起作用