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

Jul*_*ius 5 sql entity updating

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

我有一个带有嵌入式徽标实体的公司实体

@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
}
Run Code Online (Sandbox Code Playgroud)

在我的DAO方法中,我试图像这样更新嵌入式徽标:

@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();
}
Run Code Online (Sandbox Code Playgroud)

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

update px_company set file_extension, file_name, file_type=(?, ?, ?) where id=?
Run Code Online (Sandbox Code Playgroud)

Hibernate似乎了解它必须更新三个嵌入式徽标字段,但是会为此生成无效的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
Run Code Online (Sandbox Code Playgroud)

知道如何更新嵌入式实体吗?

szc*_*npp 6

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

update Company c
SET c.logo.fileName = :fileName
    ,c.logo.fileExtension = :fileExtension
    ,c.logo.imageDataType= :imageDataType
where c.id = :companyId
Run Code Online (Sandbox Code Playgroud)