2010-06-18 5 views
2

사용자 지정 쿼리를 만드는 데 문제가 있습니다.CollectionOfElements에 대한 사용자 지정 쿼리를 만드는 방법

@Entity 
public class ApplicationProcess { 

    @CollectionOfElements 
    private Set<Template> defaultTemplates; 
    //more fields 
} 

그리고 Template.java

@Embeddable 
@EqualsAndHashCode(exclude={"used"}) 
public class Template implements Comparable<Template> { 

@Setter private ApplicationProcess applicationProcess; 
@Setter private Boolean used = Boolean.valueOf(false); 

public Template() { 
} 

@Parent 
public ApplicationProcess getApplicationProcess() { 
    return applicationProcess; 
} 

@Column(nullable = false) 
@NotNull 
public String getName() { 
    return name; 
} 

@Column(nullable = true) 
public Boolean isUsed() { 
    return used; 
} 

public int compareTo(Template o) { 
    return getName().compareTo(o.getName()); 
} 
} 

나는 UPDATE 문을 만들려면 :

이 엔티티는 것입니다. 나는 시도했다이 두 :

int v = entityManager.createQuery("update ApplicationProcess_defaultTemplates t set t.used = true " + "WHERE t.applicationProcess.id=:apId").setParameter("apId", ap.getId()) 
    .executeUpdate(); 

ApplicationProcess_defaultTemplates is not mapped [update ApplicationProcess_defaultTemplates t set t.used = true WHERE t.applicationProcess.id=:apId]

그리고이 같은 오류로

int v = entityManager.createQuery("update Template t set t.used = true " + "WHERE t.applicationProcess.id=:apId").setParameter("apId", ap.getId()) 
    .executeUpdate(); 

을 시도 :

Template is not mapped [update Template t set t.used = true WHERE t.applicationProcess.id=:apId]

어떤 아이디어? 포함 된 개체가없는 개체가, 그들은 절에서 나타나지 않을 수 있습니다

UPDATE

어떻게 당신이 원하는 것은 HQL에 수 없습니다 네이티브 쿼리

int v = entityManager.createNativeQuery("update ApplicationProcess_defaultTemplates t set t.used=true where t.ApplicationProcess_id=:apId").setParameter("apId", ap.getId()).executeUpdate(); 

답변

2

를 작성하여 고정 hql 쿼리 중. 그리고 그 객체의 업데이트를하려면 from 절에 있어야합니다.

  • 는 당신의 템플릿 클래스를 리팩토링

    • 중 하나는 SQL 쿼리 (현재 세션 개체에 대한 SQL 업데이트의) 영향의 (부족을 알고주의를) 사용

      그래서 당신은 두 가지 가능성을 가지고 엔티티

  • 0

    나는 그렇게 생각하지 않는다. Hibernate는 embeddables 나 매핑 된 수퍼 클래스가 아닌 엔티티에 대해서만 업데이트를 허용한다. Template을 얻으려면 join 할 필요가 있지만 join은 HQL 갱신을 허용하지 않습니다. 당신은 사용 중 네이티브 SQL로하거나 검색하고 자바 코드에서 필드 값을 변경하려고했습니다

    No joins, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.

    다음은 HQL docs에서 relavent 제한입니다.

    0

    포함 가능 들은 더 Id이 없다 (그리고 소유 클래스에 역 참조를 설정하는 것은 가능하지 않습니다), 그들은 독립적 인 개체 수 없습니다 직접 (대량 여부) 쿼리 할 수 ​​없습니다.

    EntityManager을 통해 소유 객체를 업데이트하거나 Template을 엔티티로 설정하십시오.

    관련 문제