1 回答
TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
我設(shè)法解決了這個(gè)問(wèn)題,但我不確定這是否是正確的方法。無(wú)論如何,這是我的解決方案??紤]以下 3 個(gè)表:
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)
通過(guò)上述 3 個(gè)表,我想提供一種功能來(lái)根據(jù)連接類(lèi)型獲取任何給定用戶(hù)的連接。為此,我創(chuàng)建了 3 個(gè)實(shí)體,如下所示:
@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;
...
}
這里沒(méi)有什么特別有趣的地方,我省略了構(gòu)造函數(shù)、getter、setter 等,并且從 ConnectionType 中我不想為該類(lèi)型的所有連接提供映射,因此不存在方向。
@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;
...
}
如果對(duì)其他人來(lái)說(shuō),至少對(duì)我而言,這更有趣。這將是我的交集表實(shí)體。對(duì)于使用的 ConnectionType 與 ManyToOne 存在單向映射,因?yàn)橐粋€(gè) Connection 只能具有一個(gè) ConnectionType,而同一 ConnectionType 可以重復(fù)用于任意數(shù)量的 Connection。其他 2 個(gè)用戶(hù)映射我確信我已經(jīng)搞砸了,但在此之前這是用戶(hù)實(shí)體:
@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;
}
現(xiàn)在我更加確定我完全搞砸了,但我會(huì)顯示實(shí)際的錯(cuò)誤。我的存儲(chǔ)庫(kù)很簡(jiǎn)單:
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
我有一個(gè)帶有簡(jiǎn)化的 findAllConnectionsForUserById 函數(shù)的 UserService :
@Service
public interface UserService {
List<User> findAllConnectionsForUserById(Integer userId);
}
該方法的實(shí)現(xiàn)非常簡(jiǎn)單:
@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());
這樣,對(duì)于簡(jiǎn)單的情況,它似乎工作得很好,并且如果我也采用 ConnectionType:
connections.stream().filter(c -> c.getConnectionType().getConnectionTypeName().equals("FRIEND")).map(Connection::getRelatingUser).collect(Collectors.toList());
它似乎也有效。同樣,不確定這是否是正確的方法,但至少它可以完成工作。
添加回答
舉報(bào)
