2013-03-01 29 views
1

在我的項目中,實體的結構如下。用IdClass加入表格

public class FriendId { 
    private String profileFrom; 
    private String profileTo; 
    ... 
} 

For Friend實體用作PK - FriendId。

@Entity 
@Table(name = "FRIEND") 
@IdClass(FriendId.class) 
public class Friend { 
    @Id 
    @ManyToOne 
    @JoinColumn(name = "PROFILE_UUID_1", nullable = false) 
    private Profile profileFrom; 

    @Id 
    @ManyToOne 
    @JoinColumn(name = "PROFILE_UUID_2", nullable = false) 
    private Profile profileTo; 

    ... 
} 

問題是我不能加入方法findFriendsByUser

public List<User> findFriendsByUser(String userUuid) { 
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); 
    CriteriaQuery<User> query = builder.createQuery(User.class); 
    Root<User> userRoot = query.from(User.class); 
    Join<User, Profile> profileJoin = userRoot.join(PROFILES); 
    Join<Profile, Friend> friendsJoin = profileJoin.join(FRIENDS); 
    Join<Friend, Profile> profileFriendsJoin = friendsJoin.join(PROFILE_TO); 
    Join<Profile, User> userFriendsJoin = profileFriendsJoin.join(USER); 
    Predicate userPredicate = builder.equal(userFriendsJoin.get(UUID), userUuid); 
    query.select(userRoot).where(userPredicate); 
    return getEntityManager().createQuery(query).getResultList(); 
} 

我捕獲這個異常:

java.lang.RuntimeException: java.lang.IllegalArgumentException: Unable to resolve attribute [profileTo] against path [null] 

唯一的解決方案使用註釋@Embeddable?

回答