gpt4 book ai didi

java - 实体设计JPA

转载 作者:行者123 更新时间:2023-12-02 11:12:52 25 4
gpt4 key购买 nike

会有很多用户和很多商店。用户可以评论、 Collection 、评价商店。我必须为这个要求设计实体。

我已经创建了用户实体、评论实体和商店实体。

下面简要说明了使商店最受欢迎的实体设计

   @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;

-----------------------------
}

创建商店 Collection 夹的方法如下。

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 在这些最喜欢的实体中查看用户是否最喜欢的商店。

我可以使用 favouriteRepository.findAccessoryFavourite(storeId, userId); 解决这个问题,对于每个 storeId,我应该调用数据库来获取 favoriteEntity。从中我可以找到用户是否喜欢这家商店。

但我想知道,解决这个问题的最佳方法是什么?

我还必须处理商店的评论和评级。

最佳答案

(我没有足够的积分来发表评论,所以我将其作为答案发布)

您可以拥有此架构。

考虑 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)

正如马特提到的,不要附加太多“实体”。称它们为“用户”、“商店”、“Collection 夹”和“评论”。

关于java - 实体设计JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50494247/

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