Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - updating an embedded entity generates invalid SQL

I am trying to update an embedded entity and JPA seems to generate the wrong SQL.

I have a Company entity with an embedded Logo entity

@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
}

In my DAO method I am trying to update the embedded logo like this:

@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 actually) generates the following SQL.

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

Hibernate seems to understand it must update the three embedded logo fields, but it generates invalid SQL for it. The generated SQL results in an error.

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

Any idea how I should update the embedded entity?

like image 350
Julius Avatar asked Dec 31 '12 16:12

Julius


1 Answers

A bit old but just had the same issue - you should fully resolve properties of embedded classes in JPQL:

update Company c
SET c.logo.fileName = :fileName
    ,c.logo.fileExtension = :fileExtension
    ,c.logo.imageDataType= :imageDataType
where c.id = :companyId
like image 175
szczepanpp Avatar answered Oct 02 '22 02:10

szczepanpp