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

时间:2012-12-31 16:09:48

标签: 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
}

在我的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

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

1 个答案:

答案 0 :(得分:2)

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

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