开发者

JPA (hibernate) Mapping OneToMany incorrect?

Hi I'm new to JPA and I think I have an issue with my 开发者_StackOverflowmapping annotations I'm using hibernate 3.6.1 final with JPA 2

Here are my class :

    public class Resident {
    ...
        @OneToMany(orphanRemoval = true, mappedBy = "resident")
        @Cascade({org.hibernate.annotations.CascadeType.ALL})
        public List<ResidentInfo> infos;
    }

    public class ResidentInfo {
    ...
        @ManyToOne(optional = false)
        public Resident resident;
    }

When I try to save data for the first time, all is working perfectly.

However when I try to update a record by using save() method, the parameter orphanRemoval seems to don't be applied.

For instance if infos was a list of 4 records, and I update it by removing 2 records, the 2 records removed are not deleted from the database

I also have another issue, when I try to add an element to my list infos, then I don't have error, but the elements added are not recorded in the database.

In order to record datas I simply use resident.save(), maybe I'm wrong ?


  1. Don't you forget to add

    @Column(name = "resident")
    

    to the ManyToOne relationship ?

  2. Are you using something like

    session.startTransaction()` 
    

    and

    session.getTransaction().commit()` ?
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜