2012-08-28 172 views
0

我有兩個映射的類與Hibernate:休眠不插入父

SETTLEMENT_MODEL_GROUP(父):

@Entity 
@Table(name = "MODEL_GROUP") 
public class SettlementModelGroup 
{ 

//omitted some not fields 

@OneToMany(fetch = FetchType.LAZY, mappedBy = "modelGroup") 
private List<SettlementModel> settlementModels; 

} 

SETTLEMENT_MODEL孩子的:

@Entity 
@Table(name = "MODEL") 
public class SettlementModel 
{ 

//omitted some not fields 

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "MODEL_GROUP_ID", insertable = false, updatable = false, nullable = false) 
private SettlementModelGroup modelGroup; 

} 

現在我做簡單的數據庫插入:

SettlementModel model = new SettlementModel(); 
    SettlementModelGroup settlementModelGroup = modelManager.getModelGroup(modelGroupId); 

    //setting other fields for model 
    model.setModelGroup(settlementModelGroup); 

    sessionFactory.getCurrentSession().save(model); 

給我:

SQL Error: 1400, SQLState: 23000 
ORA-01400: cannot insetr NULL into ("MYSCHEMA"."MODEL"."MODEL_GROUP_ID") 

爲什麼冬眠不插入父ID,但它的存在,並創建?即使給它在標準輸出返回正確的ID ...

而且我無法從SettlementModel刪除insertable = false, updatable = false因爲它給了我的部署

+0

我認爲實際問題我爲什麼你不能刪除'insertable = false,updatable = false'。你會得到什麼錯誤? – axtavt

+0

我猜是因爲我沒有在'@ OneToMany'註釋中添加'cascade = CascadeType.ALL' – kamil

回答

2

冬眠錯誤增加到@OneToManycascade = CascadeType.ALL
從母體取出nullable = false

+0

是的。然後我刪除了可插入的,可更新= false,它的工作,謝謝! – kamil

1

嘗試使用cascade = CascadeType.ALL進入hbm文件,其中您定義了oneToMany關係

+0

是的,就是這樣。然後我刪除了'insertable,updateable = false',它工作,謝謝! – kamil