从父实体获得子实体(继承策略:JOINED),并从其他实体获得多对多关系

时间:2019-05-12 20:56:20

标签: java sql hibernate jpa

如果我有与JOINED继承策略相关的实体(和基础表)。在我的程序中,我有父实体Item和子实体Book。

//物品实体

package bookstore.entity.item;

@Data
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type_of_item") // not required in JOINED but enahances performance
public class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_seq")
    @SequenceGenerator(name = "item_seq", sequenceName = "item_seq", allocationSize = 1)
    private long id;

    @Column(name = "name")
    private String name;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "item")
    private List<StoreItem> storeItems;

    private int numberOfAvailableItems;

}

//图书实体

打包bookstore.entity.item.books;

/**
 * @author lukasz.krysta.ext
 *
 */
@Entity
@Data
//@DiscriminatorValue("Book")
public class Book extends Item {

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "isbn")
    private String isbn;
}

除此之外,我还有一个实体商店,该实体商店通过Entity StoreItem连接项目,因此事实上,多对多实现了从Item到StreItem的一对多,以及从Store到StoreItem的一对多。 / p>

打包bookstore.entity.store;

@Entity
@Table(name = "store")
@Data
public class Store {

    private static final String ID_STORE = "id_store";

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "store_seq")
    @SequenceGenerator(name = "store_seq", sequenceName = "store_seq", allocationSize = 1)
    private long id;

    private String name;


    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "store")
    private List<StoreItem> storeItems;

}

// StoreItem-这里我们有Store和Item作为字段。

@Entity
@Data
public class StoreItem {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="store_item_seq")
    @SequenceGenerator(name="store_item_seq", sequenceName="store_item_seq", allocationSize=1)
    private long id;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_store" /*,insertable = false, updatable = false*/)
    private Store store;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_item" /*,insertable = false, updatable = false*/)
    private Item item;

    @Column(name = "number_of_available_items_in_store")
    private int nubmerOfAvailableItemsInStore;
}

现在,在邮递员中,当我遍历所有书籍时,我还可以通过StoreItem对象获取商店。

但是,如果我想从另一面做(从获取所有商店及其书本中),我只会得到商品而不是书本。

是否有一些不错的方法来获取每个商店的归属书籍,而不仅仅是父实体对象-项目?另一方面,我希望能够获得扩展Item的实体的所有对象,而不仅仅是Books!

enter image description here

在这里,我想拥有所有附加信息而非项目的书籍 enter image description here

我将不胜感激。

0 个答案:

没有答案