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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With