将会有很多用户和商店。用户可以评论,收藏,可以对店铺进行评分。我必须为这个需求设计实体。
我已经创建了用户实体、审核实体和商店实体。
下面将简要说明如何设计实体以使商店成为最受欢迎的商店
@Entity
@Table(name = "favourite")
public class FavouriteEntity{
@ManyToOne
@JoinColumn(name = "user_id", nullable = true)
private UserEntity userEntity;
@Type(type="true_false")
private boolean value;
@ManyToOne
@JoinColumn(name = "accessory_id", nullable = true)
private StoreEntity storeEntity;
public FavouriteEntity(UserEntity user, boolean value, StoreEntity storeEntity) {
this.value = value;
this.storeEntity = accessoryEntity;
this.userEntity = user;
}
}
@Entity
@Table(name = "store")
public class StoreEntity {
@OneToMany(cascade = CascadeType.ALL)
private List<FavouriteEntity> favouriteEntities;
-----------------------------
}使商店收藏的方法如下所示。
public void makeStorefavorite(long storeId, boolean val, long userId) {
StoreEntity accessoryEntity = storeRepository.findOne(storeId);
UserEntity userEntity = userRepository.findOne(userId);
FavouriteEntity fEntity = favouriteRepository.findAccessoryFavourite(storeId, userId);
if (fEntity == null) {
fEntity = new FavouriteEntity(userEntity, val, storeEntity);
} else {
fEntity.setValue(val);
}
storeEntity.getFavouriteEntities().add(fEntity);
storeRepository.save(storeEntity);
}这是一个好的设计吗?当用户想要查看所有具有收藏详细信息的商店时,为了用当前的方法解决这个问题,我必须首先读取所有商店,在每个商店中将有收藏实体列表,然后我必须在这些收藏实体中检查用户id以查看用户的收藏商店。
我可以通过对每个storeId使用favouriteRepository.findAccessoryFavourite(storeId, userId);来解决这个问题,我应该调用DB来获取favoriteEntity。从中我可以发现用户是否最喜欢这个商店。
但我想知道,解决这个问题的最好方法是什么?
我必须处理评论和评级也为商店。
发布于 2018-05-24 05:27:22
(我没有足够的积分来评论,所以我会把这个作为答案)
您可以使用此模式。
考虑4个实体: UserEntity、StoreEntity、FavouriteEntity、ReviewEntity
UserEntity to FavouriteEntity ---> One to Many (to access all favourites without bothering stores)
UserEntity to ReviewEntity ---> One to Many
ReviewEntity to StoreEntity ---> Many to One ( to access all reviews of a store without bothering user)正如Matt提到的,不要附加太多的'Entity‘。称它们为用户、商店、收藏和评论。
https://stackoverflow.com/questions/50494247
复制相似问题