【问题标题】:JPA - updating an embedded entity generates invalid SQLJPA - 更新嵌入式实体会生成无效的 SQL
【发布时间】:2012-12-15 17:43:15
【问题描述】:

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

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

【问题讨论】:

    标签: sql entity updating


    【解决方案1】:

    有点老了,但也遇到了同样的问题 - 您应该完全解析 JPQL 中嵌入类的属性:

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

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 2011-06-09
      • 1970-01-01
      • 2021-05-10
      • 2011-10-14
      • 2014-08-13
      • 1970-01-01
      • 2016-08-01
      • 2017-02-02
      相关资源
      最近更新 更多