在hibernate中插入父子模型

时间:2016-06-01 09:32:16

标签: java hibernate jpa

我正在尝试将带子项的Parent插入数据库。在子表中,父ID不应为null。 我发现很少有类似的问题:

Get cart item name, quantity all details woocommerce - 没有回答

hibernate cascade to save parent and child - 这个问题有答案,但我的堆栈溢出了它的实现

这是我的模特:

@Entity
@Table(name = "report_children")
public class ReportChildren implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = -2093943145066737073L;

    @Id @GeneratedValue
    @Column(name="report_children_id")  
    private int reportChildrenId;   

    @ManyToOne(targetEntity = ReportParent.class)
    @JoinColumn(name="parent", nullable = false)
    private ReportParent parent;

    /**
     * @return the reportChildrenId
     */
    public int getReportChildrenId()
    {
        return reportChildrenId;
    }

    /**
     * @param reportChildrenId the reportChildrenId to set
     */
    public void setReportChildrenId(int reportChildrenId)
    {
        this.reportChildrenId = reportChildrenId;
    }

    /**
     * @return the parent
     */
    public ReportParent getParent()
    {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(ReportParent parent)
    {
        this.parent = parent;
    }

    //Default constructor, hashcode, equals, toString

}

子:

@Test
public void testLoad() throws TaxReportException
{
    ReportChildren modelChildren = new ReportChildren();        
    ReportParent model = new ReportParent();
    model.addToChildrens(modelChildren);        
    Integer id = ReportFactory.getReportDataService().load(model);
}

所以我在单元测试中得到了堆栈溢出

public void addToChildrens(ReportChildren child) {
        child.setParent(this);
        this.childrens.add(child);
    }

我明白这里

java.lang.StackOverflowError
    at java.util.ArrayList$Itr.<init>(ArrayList.java:820)
    at java.util.ArrayList$Itr.<init>(ArrayList.java:820)
    at java.util.ArrayList.iterator(ArrayList.java:814) 
    at java.util.AbstractCollection.toString(AbstractCollection.java:450)

    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportParent.toString(ReportParent.java:366)
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportChildren.toString(ReportChildren.java:316) 
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at java.util.AbstractCollection.toString(AbstractCollection.java:458)

    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportParent.toString(ReportParent.java:366)
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportChildren.toString(ReportChildren.java:316)
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at java.util.AbstractCollection.toString(AbstractCollection.java:458)

    ..............


    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportParent.toString(ReportParent.java:366)
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at ua.lz.tax.model.report.data.ReportChildren.toString(ReportChildren.java:316)
    at java.lang.String.valueOf(String.java:2847)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at java.util.AbstractCollection.toString(AbstractCollection.java:458)

可能是recurtion 但是我不明白这假设在addToChildren方法中如何工作Save child objects automatically using JPA Hibernate

那么我的实现有什么问题,我得到堆栈溢出或者还有其他好的解决方案吗?但如果可能的话,我真的不想分别写父母和孩子。

stackoverflow的一部分

leave_request.php

<?php
if ($status=='Pending'){
   echo '<a href="leave_request.php?leave_id='.$id1.'"><input type="button" name="submit" class="btn btn-success" value="Approve" "></button></a>';    
 }

    if(isset($_GET['leave_id'])){
       $id2=$_GET['leave_id'];
        $result= "UPDATE emp_leaves  SET approval_status='Accepted' WHERE leave_id='$id2'";
        $query=mysql_query($result);
    }

0 个答案:

没有答案