2017-10-15 42 views
1

我試着用休眠,JPA和JSON來休眠。 我不得不實體象下面這樣:JPA和Hibernate dosnt更新一到多關係在第一次嘗試中使用JSON的春天休息

University.java

@Entity() 
@Table(name = "university") 
public class University extends BaseEntity { 
    private String uniName; 

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) 
    @JoinColumn(name = "university_id", foreignKey = @ForeignKey(name = "university_id")) 
    private Collection<Student> students; 

    //setters and getters 
} 

Student.java

@Entity 
@Table(name = "student") 
public class Student extends BaseEntity { 
    private String stuName; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JsonIgnoreProperties("students") 
    private University university; 

    //setters and getters 
} 

我在我的分貝這dumy值。

[ 
    { 
    "id": 1, 
    "uniName": "uni1", 
    "students": [ 
     { 
     "id": 1, 
     "stutName": "st1", 
     "university": { 
      "id": 1, 
      "uniName": "uni1" 
     } 
     }, 
     { 
     "id": 2, 
     "stutName": "st2", 
     "university": { 
      "id": 1, 
      "uniName": "uni1" 
     } 
     } 
    ] 
    } 
] 

第一次嘗試:當我嘗試更新它的學生的大學,沒有成功 ,這是我的休眠日誌

Hibernate: 
    select 
     university0_.id as id1_5_1_, 
     university0_.uniName as uniName2_5_1_, 
     students1_.university_id as universi3_4_3_, 
     students1_.id as id1_4_3_, 
     students1_.id as id1_4_0_, 
     students1_.stuName as stuName2_4_0_, 
     students1_.university_id as universi3_4_0_ 
    from 
     university university0_ 
    left outer join 
     student students1_ 
      on university0_.id=students1_.university_id 
    where 
     university0_.id=? 
Hibernate: 
    update 
     student 
    set 
     stuName=?, 
     university_id=? 
    where 
     id=? 
Hibernate: 
    update 
     university 
    set 
     uniName=? 
    where 
     id=? 
Hibernate: 
    update 
     student 
    set 
     stuName=?, 
     university_id=? 
    where 
     id=? 

第二次嘗試:但是,當我發表相同的數據第二次它是成功和休眠日誌是

Hibernate: 
    select 
     university0_.id as id1_5_1_, 
     university0_.uniName as uniName2_5_1_, 
     students1_.university_id as universi3_4_3_, 
     students1_.id as id1_4_3_, 
     students1_.id as id1_4_0_, 
     students1_.stuName as stuName2_4_0_, 
     students1_.university_id as universi3_4_0_ 
    from 
     university university0_ 
    left outer join 
     student students1_ 
      on university0_.id=students1_.university_id 
    where 
     university0_.id=? 
Hibernate: 
    select 
     student0_.id as id1_4_0_, 
     student0_.stuName as stuName2_4_0_, 
     student0_.university_id as universi3_4_0_ 
    from 
     student student0_ 
    where 
     student0_.id=? 
Hibernate: 
    select 
     student0_.id as id1_4_0_, 
     student0_.stuName as stuName2_4_0_, 
     student0_.university_id as universi3_4_0_ 
    from 
     student student0_ 
    where 
     student0_.id=? 
Hibernate: 
    update 
     student 
    set 
     university_id=? 
    where 
     id=? 
Hibernate: 
    update 
     student 
    set 
     university_id=? 
    where 
     id=? 

與第一個不同!

我在做什麼我的hibernate註釋錯誤,或者如果我在我的JSON註釋中缺少某些東西,爲什麼第二次嘗試工作。

更新:這是我的編輯服務

@Override 
@Transactional/*(propagation = Propagation.NESTED)*/ 
public T edit(T entity) throws Exception { 
    return entityManager.merge(entity); 
} 

任何幫助和建議表示讚賞。

+1

發佈持久數據的代碼。 –

+0

@ Abdullah Khan我更新了我的問題 – Generic

+1

你的映射是錯誤的。閱讀Hibernate手冊,瞭解如何正確映射雙向OneToMany關聯。 –

回答

1

University.java

@Entity() 
@Table(name = "university") 
public class University extends BaseEntity { 
    private String uniName; 

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) 
    @JoinColumn(name = "university_id") 
    private Collection<Student> students; 

    //setters and getters 
} 

Student.java

@Entity 
    @Table(name = "student") 
    public class Student extends BaseEntity { 
     private String stuName; 

     @ManyToOne(fetch = FetchType.EAGER) 
     @JoinColumn(name = "university_id",updatable = false,insertable = false) 
     @JsonIgnoreProperties(value = "students", allowSetters = true) 
     private University university; 

     //setters and getters 
    } 

我希望這種幫助別人有同樣的問題。

1

試着改變我的實體,如低於它的工作完美后更新您的實體,像這樣

@Entity 
@Table(name = "university") 
public class University extends BaseEntity { 

    private String uniName; 

    @OneToMany(mappedBy = "university", ...) // Check the mappedBy property 
    private Collection<Student> students; 

    //setters and getters 

} 

@Entity 
@Table(name = "student") 
public class Student extends BaseEntity { 

    private String stuName; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JsonIgnoreProperties("students") 
    private University university; // university is bidirectionally mapped to Student 

    //setters and getters 
} 
+0

我需要雙方急切和JSON序列化它有循環,如果我添加@JsonIdentity信息JSON序列化是不符合我的要求 – Generic

+0

對於這個問題使用不同的DTO類將您的JSON映射到方法參數。 –

+0

請更具體如何將它 – Generic