[英]Entities design JPA
There will be many users and many stores. 将有许多用户和许多商店。 User can review, make favorite, can rate the store. 用户可以评论,收藏,可以评价商店。 I have to design entities for this requirement. 我必须为此需求设计实体。
I have created User Entity and Review Entity and Store Entity. 我已经创建了用户实体,审阅实体和商店实体。
Entity design to make store favorite is briefly explained below 下面简要说明使商店最喜欢的实体设计
@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;
-----------------------------
}
Method to make store favorite is below. 下面是使商店成为收藏夹的方法。
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);
}
Is it a good design? 这是一个好的设计吗? and when user wants to see all the stores with favorite details, In order to solve this with the current approach , I have to first read all the stores, In each store there will be List of favorite entities, next I have to check for user id among those favorite entities to see user's favorite store or not. 并且当用户想查看所有带有收藏夹详细信息的商店时,为了使用当前方法解决此问题,我必须先阅读所有商店,在每个商店中都会有“收藏夹实体”列表,接下来我必须检查用户这些收藏夹实体中的ID,以查看用户是否收藏夹。
I can solve this issue using favouriteRepository.findAccessoryFavourite(storeId, userId);
我可以使用favouriteRepository.findAccessoryFavourite(storeId, userId);
解决此问题favouriteRepository.findAccessoryFavourite(storeId, userId);
for every storeId I should make a call to DB to get favoriteEntity. 对于每个storeId,我应该调用DB来获取favoriteEntity。 from that I can find user made this store favorite or not. 从中我可以发现用户是否将此商店设为收藏夹。
But I would like to know, what is the best approach to solve this? 但我想知道,解决此问题的最佳方法是什么?
I have to handle reviews and ratings also for store. 我还必须处理商店的评论和评分。
( I dont have enough credits to comment, so I will post this as answer ) (我没有足够的积分来评论,所以我将其发布为答案)
You can have this schema. 您可以拥有此架构。
Consider 4 Entities: UserEntity, StoreEntity, FavouriteEntity, ReviewEntity 考虑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)
As Matt mentioned, don't append 'Entity' too much. 正如Matt所言,不要过多地添加“实体”。 Call them User, Store, Favourite and Review. 称他们为用户,商店,收藏和评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.