我有兩個映射的類與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
因爲它給了我的部署
我認爲實際問題我爲什麼你不能刪除'insertable = false,updatable = false'。你會得到什麼錯誤? – axtavt
我猜是因爲我沒有在'@ OneToMany'註釋中添加'cascade = CascadeType.ALL' – kamil