HQL

2013-08-24 3 views
2

와 연관 테이블을 쿼리 나는 oneToMany 관계와 두 개의 엔티티 클래스가 :HQL

FROM Matchs WHERE User.username=:username 

이 쿼리 :

나는 특정 User.This에 대한 모든 Matchs을 얻으려면
@Entity 
@XmlRootElement 
@DynamicInsert (value = true) 
@DynamicUpdate (value = true) 
public class Matchs { 

    @Id @GeneratedValue 
    private Long matchId; 
    private int size; 

    @OneToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "Match_Users",[email protected](name="match_id"), 
    [email protected](name="user_id")) 
    private List<User> users; 

     //Getters & Setters 
} 

@Entity 
@XmlRootElement 
@DynamicInsert (value = true) 
@DynamicUpdate (value = true) 
public class User { 

    @Id 
    private String username; 
    private String lastName,firstName,password,email; 

    private Date dateOfBirth; 
     //Getters & Setters 
    } 

내 쿼리입니다 org.hibernate.QueryException을 던집니다. HQL을 사용하여이를 어떻게 수행 할 수 있습니까?

답변

1

Matchs 테이블에없는 열의 데이터를 가져 오는 것 같습니다. 나는, 엔티티 (사용자)를 대상으로하지 당신이 관계 이름 (사용자)를 지정해야 username HQL에서

1

FROM Matchs WHERE users.username=:username
에 대한 귀하의 Matchs 클래스의 모든 열이나 관계가 표시되지 않습니다;

public class Matchs { 

    @Id @GeneratedValue 
    private Long matchId; 
    private int size; 

    @OneToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "Match_Users",[email protected](name="match_id"), 
    [email protected](name="user_id")) 
    private List<User> users; 

    @OneToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "Match_winners",[email protected](name="match_id"), 
    [email protected](name="user_id")) 
    private List<User> winners; 

     //Getters & Setters 
} 

방법, 원래 쿼리에서, 엔티티 이름 (User)이 아닌 관계 이름을 사용 winners에서 users을 차별화 할 수 있습니다 무슨 일 당신이 무엇입니까?

1
List<Matchs> list = session.createQuery("from Matchs matchs where matchs.users.username=:username").setParameter("username","myname").list(); 
+0

이 코드의 용도와 이유에 대한 설명을 제공해주십시오. – hexafraction

+0

두 번째 접근법을 시도한 결과 다음과 같은 예외가 발생했습니다. org.hibernate.QueryException : {[synthetic-alias} {non-qualified-property-ref} 사용자]를 요소 참조 [username] [FROM com .kyrogaming.models.Matchs 어디 users.username = : 사용자] – user2054833