我有我的應用程序的麻煩folloing:休眠@OneToMany合併錯誤
我有一個關係@OneToMany如上圖:
酒吧類:
@Entity
public class Bar {
@Id
private Long id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinTable(name = "bar_foo_relation",
joinColumns = { @JoinColumn(name = "id", referencedColumnName = "bar_id") },
inverseJoinColumns = { @JoinColumn(name = "id", referencedColumnName = "foo_id") })
private List<Foo> fooList;
// Getters & Setters
}
Foo類:
@Entity
public class Foo {
@Id
private Long id;
private String fooValue;
// Getters & Setters
}
當我創建欄的新實例,與fooList稀少,像這樣:
{
"id": null,
"fooList": [
{
"id": null,
"fooValue": "foo"
},
{
"id": null,
"fooValue": "foo"
}
]
}
我打電話給manager.persist(bar)
,它工作得很好。
DB表吧:
row | id ----+---- 01 | 01
DB bar_foo_relation表:
row |bar_id|foo_id ----+------+------ 01 | 01| 01 01 | 01| 02
DB富表:
row | id|foovalue ----+----+-------- 01 | 01|foo 02 | 02|foo
但是,當我把新的Foo實例是這樣的:
{
"id": 1,
"fooList": [
{
"id": 1,
"fooValue": "foo"
},
{
"id": 2,
"fooValue": "foo"
},
// New instance of Foo to persist
{
"id": null,
"fooValue": "foo" // fooValue is setted
},
// New instance of Foo to persist
{
"id": null,
"fooValue": "foo" // fooValue is setted
}
]
}
種
,並呼籲manager.merge(bar)
美孚的新情況下,這發生在DB: DB表吧:
row | id ----+---- 01 | 01
DB bar_foo_relation表:
row |bar_id|foo_id ----+------+------ 01 | 01| 01 01 | 01| 02 01 | 01| 03 // New instance of Foo got persisted and is referenced here 01 | 01| 04 // New instance of Foo got persisted and is referenced here
DB富表:
row | id|foovalue ----+----+-------- 01 | 01|foo 02 | 02|foo 02 | 03|null // New instance of Foo without foovalue 02 | 04|null // New instance of Foo without foovalue
新Foos的列fooValeu設置爲null
。
我不知道它爲什麼會發生。有人能澄清我嗎?
我的環境是: 的Java 1.7 的PostgreSQL 9.3 休眠4.3.6.Final
如果您在其中添加CascadeType.MERGE會發生什麼? (cascade = {CascadeType.REFRESH,CascadeType.MERGE})事實上,你試圖通過調用Bar上的merge來保存Foo的新實例似乎不正確。 – zmf 2015-02-10 17:11:38
謝謝。我添加了** CascadeType.MERGE **,這是工作。回答問題。我會投票並標記解決。 – brbrbr 2015-02-10 17:41:37