当前位置: 首页 > Java博客 > SpringBoot
Spring Boot项目 - 编写JPQL(Intellij)的问题
发布日期:2018-09-29 21:49:20  来源:http://www.javaxiangmu.com   阅读:3221   作者:lrh0211
所以我设置了一个spring boot项目,我想用它来创建一些特定的查询。 很简单,我只想执行一个select语句:

所以我设置了一个spring boot项目,我想用它来创建一些特定的查询。

很简单,我只想执行一个select语句:

public class UserRepositoryCustomImpl implements UserRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;


    public User findUserByEmail(String email) {
        Query nativeQuery = entityManager.createQuery("select e from User");
        nativeQuery.getFirstResult();
        return new User();
    }

}

在Intellij中,当我将鼠标悬停在用户上时,它说不能resolve symbol for User,我有一个谷歌,大多数答案说你需要使用实体的名称,当然我做了:

@Entity(name = "PG_user")
public class User extends AbstractEntity {

    @Column
    private String email;

    @Column
    private String password;

    public User(){}

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这也是我的POM,如果这有帮助:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>0.3.5</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.2.2.jre8</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.webjars.bower/angular-bootstrap-contextmenu -->
        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>angular-bootstrap-contextmenu</artifactId>
            <version>0.9.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-crypto</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>


    </dependencies>

原因:

但是我的实体名称是PG_user

@Entity(name = "PG_user")
public class User extends AbstractEntity {

name属性定义必须用于查询的实体的名称。

但我假设您要设置表的名称。

那就是

@Table(name = "PG_user")
@Entity
public class User extends AbstractEntity {

本文编辑:lrh0211





  • | 联系我们
Javaxiangmu官方群01:Javaxiangmu官方群01
Javaxiangmu官方群01:105049007
QQ客服:1376806388
微信公众号:
备案号:冀ICP备17014229号
  • | 友情链接