2012-12-31 51 views
3

我正嘗試更新嵌入式實體,並且JPA似乎生成錯誤的SQL。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(休眠實際上)生成遵循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 

任何想法如何更新嵌入式實體?

回答

2

有點老,但只是有同樣的問題 - 你應該完全解決JPQL嵌入式類的屬性:

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