微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据前一个

如何解决根据前一个

我试图将其简化为我能想到的最小的例子。在我的示例中,我按州列出了按城市划分的商店位置。一家商店可以有很多水果。水果可以在许多商店中购买。水果可以处于多个州,每个州的成本。一个州可以有很多成果。

我有五个实体:状态,存储,store_fruit,fruit和state_fruit,如下所示:

@Entity
@Table(name = "STATE")
public class State {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "STATE_ID")
    private Integer stateId;
    @Column(name = "STATE_CD")
    private String stateCd;
    @OnetoMany(mappedBy = "state")
    private Set<StateFruit> stateFruits;
    @OnetoMany(mappedBy = "state")
    private Set<Store> stores;

    public State() {}

    @JsonIgnore
    public Integer getStateId() {
        return stateId;
    }

    public void setStateId(Integer stateId) {
        this.stateId = stateId;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("code")
    public String getStateCd() {
        return stateCd;
    }

    public void setStateCd(String stateCd) {
        this.stateCd = stateCd;
    }

    @JsonIgnore
    public Set<StateFruit> getStateFruits() {
        return stateFruits;
    }

    public void setStateFruits(Set<StateFruit> stateFruits) {
        this.stateFruits = stateFruits;
    }

    @JsonIgnore
    public Set<Store> getStores() {
        return stores;
    }

    public void setStores(Set<Store> stores) {
        this.stores = stores;
    }
}
@Entity
@Table(name = "STORE")
public class Store {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "STORE_ID")
    private Integer storeId;
    @Column(name = "STORE_CITY")
    private String storeCity;
    @OnetoMany(mappedBy = "store")
    private Set<StoreFruit> storeFruits;
    @ManyToOne
    @JoinColumn(name = "state_id")
    private State state;

    public Store() {}

    @JsonIgnore
    public Integer getStoreId() {
        return storeId;
    }

    public void setStoreId(Integer storeId) {
        this.storeId = storeId;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("city")
    public String getStoreCity() {
        return storeCity;
    }

    public void setStoreCity(String storeCity) {
        this.storeCity = storeCity;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("fruits")
    public Set<StoreFruit> getStoreFruits() {
        return storeFruits;
    }

    public void setStoreFruits(Set<StoreFruit> storeFruits) {
        this.storeFruits = storeFruits;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("state")
    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
}
@Entity
@Table(name = "STORE_FRUIT")
public class StoreFruit {

    @EmbeddedId
    private StoreFruitId storeFruitId;
    @ManyToOne
    @JoinColumn(name = "STORE_ID",insertable = false,updatable = false)
    private Store store;
    @ManyToOne
    @JoinColumn(name = "FRUIT_ID",updatable = false)
    private Fruit fruit;

    public StoreFruit() {}

    @JsonIgnore
    public StoreFruitId getStoreFruitId() {
        return storeFruitId;
    }

    public void setStoreFruitId(StoreFruitId storeFruitId) {
        this.storeFruitId = storeFruitId;
    }

    @JsonIgnore
    public Store getStore() {
        return store;
    }

    public void setStore(Store store) {
        this.store = store;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("fruit")
    public Fruit getFruit() {
        return fruit;
    }

    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }
}
@Entity
@Table(name = "FRUIT")
public class Fruit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "FRUIT_ID")
    private Integer fruitId;
    @Column(name = "FRUIT_NAME")
    private String fruitName;
    @OnetoMany(mappedBy = "fruit")
    private Set<StoreFruit> storeFruits;
    @OnetoMany(mappedBy = "fruit")
    private Set<StateFruit> stateFruits;

    public Fruit() {}

    @JsonIgnore
    public Integer getFruitId() {
        return fruitId;
    }

    public void setFruitId(Integer fruitId) {
        this.fruitId = fruitId;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("name")
    public String getFruitName() {
        return fruitName;
    }

    public void setFruitName(String fruitName) {
        this.fruitName = fruitName;
    }

    @JsonIgnore
    public Set<StoreFruit> getStoreFruits() {
        return storeFruits;
    }

    public void setStoreFruits(Set<StoreFruit> storeFruits) {
        this.storeFruits = storeFruits;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("states")
    public Set<StateFruit> getStateFruits() {
        return stateFruits;
    }

    public void setStateFruits(Set<StateFruit> stateFruits) {
        this.stateFruits = stateFruits;
    }
}
@Entity
@Table(name = "STATE_FRUIT")
public class StateFruit {

    @EmbeddedId
    private StateFruitId stateFruitId;
    @Column(name = "FRUIT_COST")
    private Double fruitCost;
    @ManyToOne
    @JoinColumn(name = "STATE_ID",updatable = false)
    private State state;
    @ManyToOne
    @JoinColumn(name = "FRUIT_ID",updatable = false)
    private Fruit fruit;

    public StateFruit() {}

    @JsonIgnore
    public StateFruitId getStateFruitId() {
        return stateFruitId;
    }

    public void setStateFruitId(StateFruitId stateFruitId) {
        this.stateFruitId = stateFruitId;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("cost")
    public Double getFruitCost() {
        return fruitCost;
    }

    public void setFruitCost(Double fruitCost) {
        this.fruitCost = fruitCost;
    }

    @JsonView(Views.Info.class)
    @JsonGetter("state")
    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    @JsonIgnore
    public Fruit getFruit() {
        return fruit;
    }

    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }
}

现在,当我在商店存储库中搜索商店ID时,将获得以下JSON输出(使用JsonView):

{
    "city": "Fort Worth","state": {
        "code": "TX"
    },"fruits": [
        {
            "fruit": {
                "name": "Apple","states": [
                    {
                        "cost": 1.75,"state": {
                            "code": "CO"
                        }
                    },{
                        "cost": 1.5,"state": {
                            "code": "TX"
                        }
                    }
                ]
            }
        },{
            "fruit": {
                "name": "Banana","states": [
                    {
                        "cost": 2.25,{
                        "cost": 2.0,"state": {
                            "code": "TX"
                        }
                    }
                ]
            }
        }
    ]
}

一切正常,例外,我想找到某种方法来将水果部分中设置的状态过滤到我要查找的商店的状态。因为商店以相反的方式从状态到state_fruit旋转,所以我认为我可以利用这种关系,所以我还没有遇到过这样的事情。不必编写自定义查询就可以吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。