【问题标题】:Spring Boot JPA custom Query with OneToMany使用 OneToMany 的 Spring Boot JPA 自定义查询
【发布时间】:2019-02-16 21:31:50
【问题描述】:

我正在 Spring Boot 中构建一个类似 Twitter 的应用程序。我正在尝试为用户生成时间线。我为此做了一个自定义查询并将其添加到我的repository 中,它扩展了CrudRepository<Account, Long>。当我尝试启动应用程序时,我收到以下异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: account_following is not mapped

account_following 表是从AccountAccount@OneToMany,如下面的代码所示。我不确定如何将account_following 放入查询中。

AccountRepository.java

@Repository
public interface AccountRepository extends CrudRepository<Account, Long> {

    @Query("SELECT t FROM Tweet t, account_following k WHERE k.following_id = t.owner_id AND k.account_ID = :account_ID AND k.following_id IN (SELECT following_ID FROM account_following WHERE account_id = :account_ID) ORDER BY unixdate DESC")
    public List<Tweet> generateTimeline(@Param("account_ID") Long account_ID);

}

Account.java

@Entity
public class Account implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique=true)
    private String username;

    @Transient
    private String password;

    private String location;

    private String description;

    private String website;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Account> following;

    @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
    private List<Tweet> tweets;
}

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>kwetter</artifactId>
    <groupId>org.springframework</groupId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>

        <!--Spring Boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <type>jar</type>
        </dependency>

        <!--JAVAX-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.8.RELEASE</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <type>jar</type>
        </dependency>

        <!--DB-->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

account_following 存在于数据库中,如下图所示。我正在使用MySQL version 8.0.12

【问题讨论】:

  • 在hql查询中不写表名在查询中写下你的实体类名
  • @Karthik 但这是一个@OneToMany 映射,那我该怎么写?我找不到它。

标签: java mysql spring-boot jpa


【解决方案1】:

尝试将查询更改为原生查询。

@Repository
public interface AccountRepository extends CrudRepository<Account, Long> {

@Query(value= "SELECT t FROM Tweet t, account_following k WHERE k.following_id = t.owner_id AND k.account_ID = :account_ID AND k.following_id IN (SELECT following_ID FROM account_following WHERE account_id = :account_ID) ORDER BY unixdate DESC",nativeQuery=true)
public List<Tweet> generateTimeline(@Param("account_ID") Long account_ID);
}

由于没有名为 account_following 的实体,您会在 HQL 中收到该错误。

【讨论】:

  • 我不明白这是对我的问题的回答。也许我不够清楚。我想做的是从帐户中获取推文列表。这些帐户必须在查询中具有account_ID 的帐户中的集合following 中。
【解决方案2】:

如果 JPQL:

SELECT t FROM Tweet as t, Account as account WHERE account.following as f on f.id = t.ownerId ...

请注意,以上字段必须与类字段相同,而不是数据库中的列名。

【讨论】:

  • 为什么会有任何不同?他在 JPQL 连接中使用表名而不是实体名。 JPQL 无效
猜你喜欢
  • 2016-04-24
  • 2016-09-18
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 2019-09-28
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多