JPA更新实体,包括实体列表

时间:2020-03-21 16:10:30

标签: java spring hibernate jpa

通常,我的目标是将xml文件解析为数据库表,因此文件是这样的

<?xml version="1.0" encoding="UTF-8"?>
<Storage>
    <Box id="1">
        <Item id="1"/>
        <Item color="red" id="2"/>
        <Box id="3">
            <Item id="3" color="red" />
            <Item id="4" color="black" />
        </Box>
        <Box id="6"/>
        <Item id="5"/>
    </Box>
    <Item id="6"/>
</Storage>

我可以用umarshaller解析它,并且它可以很好地创建Storage对象,但是当尝试更新db中的框和项目时会出现问题。我使用的实体看起来像这样

package com.demo.alpha.domen;

import lombok.Data;
import org.hibernate.annotations.Cascade;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.List;

@Data
@Entity
@Table(name = "BOX")
@XmlAccessorType(XmlAccessType.FIELD)
public class Box {
    @Id
    @Column(name = "ID")
    @XmlAttribute(name = "id")
    private int id;

    @Column(name = "CONTAINED IN")
    private int containedIn;

    @OneToMany(mappedBy = "box", cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @XmlElement(name = "Item")
    private List<Item> items;


    @ManyToOne(fetch = FetchType.LAZY)
    private Box parentBox;

    @OneToMany(mappedBy = "parentBox", cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @XmlElement(name = "Box")
    private List<Box> boxes;
}
package com.demo.alpha.domen;

import lombok.Data;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@Data
@Entity
@Table(name = "ITEM")
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {

    @Id
    @Column(name = "ID")
    @XmlAttribute(name = "id")
    private int id;

    @Column(name = "COLOR")
    @XmlAttribute(name = "color")
    private String color;

    @Column(name = "CONTAINED IN")
    private int containedIn;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH},
            fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTAINED_IN")
    private Box box;
}

我不确定如何映射包含其他框的框,因为它是同一张表,并且我需要CONTAINED_IN作为父框的ID。尝试使用jpa存储库更新它时,在此查询中获取org.h2.jdbc.JdbcSQLSyntaxErrorException

   select
        box0_.id as id1_0_1_,
        box0_.contained in as containe2_0_1_,
        box0_.parent_box_id as parent_b3_0_1_,
        boxes1_.parent_box_id as parent_b3_0_3_,
        boxes1_.id as id1_0_3_,
        boxes1_.id as id1_0_0_,
        boxes1_.contained in as containe2_0_0_,
        boxes1_.parent_box_id as parent_b3_0_0_ 
    from
        box box0_ 
    left outer join
        box boxes1_ 
            on box0_.id=boxes1_.parent_box_id 
    where
        box0_.id=?

starting log

0 个答案:

没有答案