当前位置: 首页 > 知识库问答 >
问题:

Spring Data JPA查询不工作,列不存在

曹沛
2023-03-14

我在我的Web应用程序中使用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
}
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
<?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)
)

共有1个答案

胡玉书
2023-03-14

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

不知道为什么@table(name=“user”,schema=“public”)会起作用。也许PostgreSQL没有将public.user视为与user相反的关键字。

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

对于这种方法,可以使用命名策略。将此作为示例:Hibernate5NamingStrategy

前缀示例:StrategyOptions

 类似资料:
  • 我正在使用ApacheLucene5.0。0并在使用QueryParser时遇到问题。我试图创建一个查询,但得到一个ParseException。 以下是我的代码: 这是我得到的例外: 如果有帮助,我已经包含了以下jar文件: lucene-analyzers-common-5.0.0.jar lucene-core-5.0.0.jar lucene-queries-5.0.0.jar lucen

  • 它的灵感来自spring-data-jpa-examples/src/main/resources/caching-repository-context.xml 看日志;每次请求时,我都会看到查询被执行。 下次我请求时,我看到以下查询;

  • 我的查询没有使用参数对我的帖子进行排序。 一点背景: 我在foreach语句中,该语句循环遍历“category”id的自定义分类法。在该foreach语句中,我试图调用一个新的WP_查询,从该foreach循环的每个“类别”获取帖子。我的args数组如下所示: 和都是此post\u类型中我的自定义分类中的数组。 和根本不起作用,我想不出原因。

  • 我正在尝试将最近的搜索添加到工具栏上的SearchView: 编辑: btn_search项目:

  • 我正在尝试使用本机查询方法获取数据。我能够使用JPA存储库声明的spring数据方法(findAll()等)和JPQL查询来获取数据。 当我使用本机查询方法时,“select*from”正在工作。但当我指定“select username from”时,方法不起作用。表示指定列名时,它不起作用 我像这样添加我的代码, 但是使用select*from users的查询没有问题。这是本机查询性质吗?或

  • 我试图更新一个列,我没有修改列值,你能帮我吗? 我正在尝试的代码:- 这就是我得到的错误: