gpt4 book ai didi

java - 使用 OneToMany 的 Spring Boot JPA 自定义查询

转载 作者:行者123 更新时间:2023-11-30 23:44:05 30 4
gpt4 key购买 nike

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

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

account_following 表是一个 @OneToMany来自 AccountAccount正如您在下面的代码中看到的那样。我不确定如何放置 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 .

Tables

最佳答案

尝试将查询更改为 native 查询。

@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 中遇到该错误。

关于java - 使用 OneToMany 的 Spring Boot JPA 自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291824/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com