2

In the following code I have a field shopType which is actually a enum , In my scenario one shop has multiple type for example shop ABC have type grocery as well as pharmacy , so I want to store a list of enum in my database in separate table in which two column present one is shop_id and other is shop_type so that one shop can have multiple types How can i do this ?

here is my code

ShopDetail.java

@Entity
public class ShopDetail {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String address;

    private Double latitude;

    private Double longitude;

    private float  rating;

    private Time openingTime;

    private Time closingTime;



    @Enumerated(EnumType.STRING)
     private Collection<ShopType> shopType;


    @Column(columnDefinition=" bit(1)default 1")
    private boolean shopEnabled = true;

    //getters and setters
    }

ShopType

public enum ShopType {

    GROCERY,
    PHARAMACY
}
SFAH
  • 624
  • 15
  • 35

1 Answers1

5

This should work:

@ElementCollection(targetElement = ShopType.class)
@JoinTable(name = "tblShopTypes", joinColumns = @JoinColumn(name = "id"))
@Column(name = "shopType", nullable = false)
@Enumerated(EnumType.STRING)
Collection<ShopType> shopTypes;

ref: this answer

Leonard
  • 783
  • 3
  • 22