1 回答
TA贡献1887条经验 获得超5个赞
我设法解决了这个问题,但我不确定这是否是正确的方法。无论如何,这是我的解决方案。考虑以下 3 个表:
create table USER_INFO (
USER_ID int not null primary key,
USER_NAME varchar(16),
PASSWORD varchar(64)
);
create table CONNECTION_TYPE (
CONNECTION_TYPE_ID int not null primary key,
CONNECTION_TYPE_NAME varchar(16) not null,
CONNECTION_TYPE_DESCRIPTION varchar(128),
unique (CONNECTION_TYPE_NAME)
);
create table CONNECTION (
CONNECTION_ID int not null primary key,
CONNECTION_TYPE_ID int,
RELATED_USER_ID int,
RELATING_USER_ID int,
foreign key (CONNECTION_TYPE_ID) references CONNECTION_TYPE(CONNECTION_TYPE_ID),
foreign key (RELATED_USER_ID) references USER_INFO(USER_ID),
foreign key (RELATING_USER_ID) references USER_INFO(USER_ID)
通过上述 3 个表,我想提供一种功能来根据连接类型获取任何给定用户的连接。为此,我创建了 3 个实体,如下所示:
@Entity
@Table(name = "CONNECTION_TYPE")
public class ConnectionType implements Serializable {
@Id
@NotNull
@Column(name = "CONNECTION_TYPE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer connectionTypeId;
@NotNull
@Column(name = "CONNECTION_TYPE_NAME", unique = true)
private String connectionTypeName;
@Column(name = "CONNECTION_TYPE_DESCRIPTION")
private String connectionTypeDescription;
...
}
这里没有什么特别有趣的地方,我省略了构造函数、getter、setter 等,并且从 ConnectionType 中我不想为该类型的所有连接提供映射,因此不存在方向。
@Entity
@Table(name = "CONNECTION")
public class Connection implements Serializable {
@Id
@NotNull
@Column(name = "CONNECTION_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer connectionId;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CONNECTION_TYPE_ID", referencedColumnName = "CONNECTION_TYPE_ID")
private ConnectionType connectionType;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RELATED_USER_ID", referencedColumnName = "USER_ID")
private User relatedUser;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RELATING_USER_ID", referencedColumnName = "USER_ID")
private User relatingUser;
...
}
如果对其他人来说,至少对我而言,这更有趣。这将是我的交集表实体。对于使用的 ConnectionType 与 ManyToOne 存在单向映射,因为一个 Connection 只能具有一个 ConnectionType,而同一 ConnectionType 可以重复用于任意数量的 Connection。其他 2 个用户映射我确信我已经搞砸了,但在此之前这是用户实体:
@Entity
@Table(name = "USER_INFO")
public class User implements Serializable {
@Id
@NotNull
@Column(name = "USER_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
@NotNull
@Column(name = "USER_NAME")
private String userName;
@NotNull
@Column(name = "PASSWORD")
private char[] password;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "relatedUser", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Connection> connections;
}
现在我更加确定我完全搞砸了,但我会显示实际的错误。我的存储库很简单:
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
我有一个带有简化的 findAllConnectionsForUserById 函数的 UserService :
@Service
public interface UserService {
List<User> findAllConnectionsForUserById(Integer userId);
}
该方法的实现非常简单:
@Override
@Transactional
public List<User> findAllConnectionsForUserById(Integer userId) {
Optional<User> _user = userRepository.findById(userId);
// omitted exception handling...
User user = _user.get();
List<Connection> connections = user.getConnections();
return connections.strea.map(Connection::getRelatingUser).collect(Collectors.toList());
这样,对于简单的情况,它似乎工作得很好,并且如果我也采用 ConnectionType:
connections.stream().filter(c -> c.getConnectionType().getConnectionTypeName().equals("FRIEND")).map(Connection::getRelatingUser).collect(Collectors.toList());
它似乎也有效。同样,不确定这是否是正确的方法,但至少它可以完成工作。
添加回答
举报