gpt4 book ai didi

sql - JPA - 更新嵌入实体会生成无效的 SQL

转载 作者:行者123 更新时间:2023-12-03 04:22:14 26 4
gpt4 key购买 nike

我正在尝试更新嵌入实体,而 JPA 似乎生成了错误的 SQL。

我有一个带有嵌入 Logo 实体的公司实体

@Entity
public class Company {

private Long id;
@Embedded
private Logo logo;

// Omitted other fields, getters, setters, etc

}

@Embeddable
public class Logo {

private String fileName;
private String fileExtension;
private String imageDataType;

// Omitted getters and setters
}

在我的 DAO 方法中,我尝试更新嵌入的 Logo ,如下所示:

@Override
public void setLogo(Logo logo, Long companyId) {
String q = "update Company c SET c.logo = :logo where c.id = :companyId";
Query query = entityManager.createQuery(q);
query.setParameter("companyId", companyId);
query.setParameter("logo", logo);
query.executeUpdate();
}

JPA(实际上是 Hibernate)生成以下 SQL。

update px_company set file_extension, file_name, file_type=(?, ?, ?) where id=?

Hibernate 似乎明白它必须更新三个嵌入的 Logo 字段,但它会为其生成无效的 SQL。生成的 SQL 结果出错。

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 ' file_name, file_type=('jpg', '7679075394', 0) where id=1' at line 1

知道我应该如何更新嵌入的实体吗?

最佳答案

有点旧,但有同样的问题 - 您应该完全解决 JPQL 中嵌入类的属性:

update Company c
SET c.logo.fileName = :fileName
,c.logo.fileExtension = :fileExtension
,c.logo.imageDataType= :imageDataType
where c.id = :companyId

关于sql - JPA - 更新嵌入实体会生成无效的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14103723/

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