不要使用cascade =" all-delete-orphan"更改对集合的引用。同时改变一个实体的properyt

时间:2014-11-10 06:24:52

标签: java hibernate

当我尝试更改实体中属性的值而不是集合时,我的异常值超出了异常。

我通过tinyUrl检索了值,即使它不是表中的主键。

以下是代码段的一部分

Sample sample = retrieveContentByUrl("https://text.com");
sample.setDescription("Sample");
getSession().saveorupdate(sample);

Sample.java

class Sample {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID")
    private int id;

    @Column(name="TINY_URL")
    private int tinyUrl;

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

    @OneToMany(mappedBy="sample",fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true)
    private Set<TextBooks> textBooks = new HashSet<TextBooks>();

    //Having getters and setters for all above variables
 }

TextBooks.java

 class TextBooks {

    @ManyToOne
    @JoinColumn(name = "ID")
    private Sample sample;

    //Having getters and setters for all above variables
 }

.hbm文件

<class name="com.sample.Sample"
        table="SAMPLE" >
        <id name="id" column="ID" type="long">
            <generator class="identity" />
        </id>
         <property column="TINY_URL" name="tinyUrl" />
        <property column="DESCRIPTION" name="description" />
        <set name="textBooks" inverse="true" cascade="all-delete-orphan">
            <key column="ID" />
            <one-to-many
            class="com.text.TextBooks" />
        </set>
</set>
</class>

0 个答案:

没有答案