vighneshwar
vighneshwar

Reputation: 18

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.

I can solve this issue using favouriteRepository.findAccessoryFavourite(storeId, userId); for every storeId I should make a call to DB to get 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.

Upvotes: 0

Views: 186

Answers (1)

okcomputer_kid
okcomputer_kid

Reputation: 511

( 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

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. Call them User, Store, Favourite and Review.

Upvotes: 1

Related Questions