gpt4 book ai didi

mysql - JPA- hibernate : How to select the key/value of a map attribute using KEY/VALUE syntax with JPQL

转载 作者:行者123 更新时间:2023-11-30 00:41:20 29 4
gpt4 key购买 nike

我有以下具有相应 getter/setter 的实体:

@Entity
public class Movie {
@Id@GeneratedValue
private int id;
private String title;
//getters and setters
}

@Entity
public class VideoStore {

@Id@GeneratedValue
private int id;
private String name;

@ElementCollection
@CollectionTable(name="INVENTORY",joinColumns=@JoinColumn(name="STORE_ID"))
@Column(name="COPIES_IN_STOCK")
@MapKeyJoinColumn(name="MOVIE",referencedColumnName="ID")
Map<Movie,Integer>videoInventory;
//getters and setters
}

我尝试从主方法执行以下 JPQL 查询:

    EntityManagerFactory emf=Persistence.createEntityManagerFactory("Tutorial");
EntityManager em=emf.createEntityManager();
String queryMap1 = "SELECT v " +
"FROM VideoStore v JOIN v.videoInventory m " +
"WHERE KEY(m) = 1";
@SuppressWarnings("rawtypes")
List results= em.createQuery(queryMap1).getResultList();

我收到以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'KEY(videoinven1_.COPIES_IN_STOCK)=1' at line 1

此外,包含 KEY/VALUE/ENTRY 语法的查询的每个变体都会出现不同类型的错误

我使用 JPA 与 Hibernate 和 Mysql 作为数据库提供程序。我找不到此问题的答案,因此非常感谢您的帮助。

最佳答案

您似乎将 KEY(m)(这是一个 Movie)的返回值与数字进行比较。

关于mysql - JPA- hibernate : How to select the key/value of a map attribute using KEY/VALUE syntax with JPQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749652/

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