HIbernate - 多对多 - 嵌套c:foreach

时间:2017-08-25 03:41:16

标签: java hibernate java-ee jstl

我正在尝试在下面运行嵌套的c:loop。在页面加载之前,我会获得itemsextrascat个列表itemsextrascatService.getExtrascatByIditems(item)。没问题,我得到了正确数量的结果。但是,嵌套循环未正确显示。不确定它是否是一个休眠问题或jstl。

JSP

<c:forEach var="itemsextrascat" items="${itemsextrascat}">
    <th><c:out value="${itemsextrascat.extrascat.name}"></c:out> </th><br>
    <table> 
        <c:forEach var="extras" items="${itemsextrascat.extrascat.extras}">
            <tr><c:out value="${extras.name}"></c:out></tr>
            </c:forEach>
    </table>
</c:forEach> 

返回

Fruit 
Overy Easy

Syrups 
Over Medium

Breakfast Additions 
Sunny Side Up

所以类别标题是正确的,但附加条件是1)项目和extrascat不正确2)它们并非全部列出该类别下的所有额外内容。很确定hibernate如何调用对象,但我不知所措。

Extrascat.java

@Entity
@Table(name = "extrascat")
public class Extrascat implements Serializable {

    private static final long serialVersionUID = 8644201177004602944L;

    @Id
    private int idextrascat;
    private String name;

    @ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "extrascat")
    private Set<Items> items = new HashSet<Items>();

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "idextras")
    private Set<Extras> extras;

    // CONSTRUCTORS
    public Extrascat() {

    }

    public Extrascat(int idextrascat, String name, Set<Items> items, Set<Extras> extras) {
        this.idextrascat = idextrascat;
        this.name = name;
        this.items = items;
        this.extras = extras;
    }

    // GETTERS AND SETTERS
    public int getIdextrascat() {
        return idextrascat;
    }

    public void setIdextrascat(int idextrascat) {
        this.idextrascat = idextrascat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Items> getItems() {
        return items;
    }

    public void setItems(Set<Items> items) {
        this.items = items;
    }

    public Set<Extras> getExtras() {
        return extras;
    }

    public void setExtras(Set<Extras> extras) {
        this.extras = extras;
    }

}

Items.java

@Entity
@Table(name = "items")
public class Items implements Serializable {
    private static final long serialVersionUID = -3607451001182083512L;

    @Id
    @GeneratedValue
    private int iditems;

    @ManyToOne
    @JoinColumn(name = "idcategories")
    private Categories categories;

    @Size(min = 1, max = 35, groups = { PersistenceValidationGroup.class,
            FormValidationGroup.class })
    private String name;

    @Size(min = 0, max = 100, groups = { PersistenceValidationGroup.class,
            FormValidationGroup.class })
    private String description;

    private double priceInCents;
    private int hidden;

    @OrderColumn
    @ManyToMany(cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinTable(name="itemsextrascat", joinColumns={@JoinColumn(name="iditems")}, 
          inverseJoinColumns={@JoinColumn(name="idextrascat")})
    private Set<Extrascat> extrascat = new HashSet<Extrascat>();

    // CONSTRUCTORS
    public Items() {

    }

    public Items(int iditems, Categories categories, String name, String description, double priceInCents, int hidden, Set<Extrascat> extrascat) {
        this.iditems = iditems;
        this.categories = categories;
        this.name = name;
        this.description = description;
        this.priceInCents = priceInCents;
        this.hidden = hidden;
        this.extrascat = extrascat;
    }

    // GETTERS AND SETTERS
    public int getIditems() {
        return iditems;
    }

    public void setIditems(int iditems) {
        this.iditems = iditems;
    }

    public Categories getCategories() {
        return categories;
    }

    public void setCategories(Categories categories) {
        this.categories = categories;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getPriceInCents() {
        return priceInCents;
    }

    public void setPriceInCents(double priceInCents) {
        this.priceInCents = priceInCents;
    }

    public int isHidden() {
        return hidden;
    }

    public void setHidden(int hidden) {
        this.hidden = hidden;
    }

    public Set<Extrascat> getExtrascat() {
        return extrascat;
    }

    public void setExtrascat(Set<Extrascat> extrascat) {
        this.extrascat = extrascat;
    }

    public int getHidden() {
        return hidden;
    }

}

Itemsextrascat.java

@Entity
@Table(name = "itemsextrascat")
public class Itemsextrascat implements Serializable {
    private static final long serialVersionUID = 7640244484584804821L;

    @Id
    @GeneratedValue
    private int iditemsextrascat;

    @ManyToOne
    @JoinColumn(name = "iditems")
    private Items items;

    @ManyToOne
    @JoinColumn(name = "idextrascat")
    private Extrascat extrascat;

    // CONSTRUCTORS
    public Itemsextrascat() {

    }

    public Itemsextrascat(int iditemsextrascat, Items items, Extrascat extrascat) {
        this.iditemsextrascat = iditemsextrascat;
        this.items = items;
        this.extrascat = extrascat;
    }

    // GETTERS AND SETTERS
    public int getIditemsextrascat() {
        return iditemsextrascat;
    }

    public void setIditemsextrascat(int iditemsextrascat) {
        this.iditemsextrascat = iditemsextrascat;
    }

    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }

    public Extrascat getExtrascat() {
        return extrascat;
    }

    public void setExtrascat(Extrascat extrascat) {
        this.extrascat = extrascat;
    }

}

Extras.java

@Entity
@Table(name = "extras")
public class Extras implements Serializable {

    private static final long serialVersionUID = 8644201177004602944L;

    @Id
    private int idextras;
    private String name;
    private double priceincents;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "idextrascat")
    private Extrascat extrascat;

    @ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "extras")
    private Set<Orderitem> orderitem = new HashSet<Orderitem>();

    // CONSTRUCTORS
    public Extras() {

    }

    public Extras(int idextras, String name, double priceincents, Extrascat extrascat) {
        this.idextras = idextras;
        this.name = name;
        this.priceincents = priceincents;
        this.extrascat = extrascat;
    }

    // GETTERS AND SETTERS
    public int getIdextras() {
        return idextras;
    }

    public void setIdextras(int idextras) {
        this.idextras = idextras;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPriceincents() {
        return priceincents;
    }

    public void setPriceincents(double priceincents) {
        this.priceincents = priceincents;
    }

    public Extrascat getExtrascat() {
        return extrascat;
    }

    public void setExtrascat(Extrascat extrascat) {
        this.extrascat = extrascat;
    }
}

1 个答案:

答案 0 :(得分:0)

Zaki建议为UI创建一个新bean是正确的。我正在查询一个表并希望从多个表中获得结果。相反,我填充了一个包含我需要的值的新对象,并在我的视图中使用该对象。