7

我正在尝试更新嵌入式实体,而 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
}

在我的 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();
}

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

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

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

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

4

1 回答 1

9

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

update Company c
SET c.logo.fileName = :fileName
    ,c.logo.fileExtension = :fileExtension
    ,c.logo.imageDataType= :imageDataType
where c.id = :companyId
于 2014-10-29T18:27:53.437 回答