gpt4 book ai didi

java - 使用 IdClass 连接表

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:59 26 4
gpt4 key购买 nike

在我的项目中,实体的结构如下。

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

对于用作 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的唯一解决方案?

最佳答案

看看:http://docs.oracle.com/javaee/5/api/javax/persistence/IdClass.html

The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same

这里不是这种情况,类型不同!

关于java - 使用 IdClass 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com