0

我有两个休眠的映射类:

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 ...

此外,我无法insertable = false, updatable = falseSettlementModel中删除,因为它在部署期间给了我休眠错误

4

2 回答 2

2

添加以从父级中@OneToMany cascade = CascadeType.ALL
删除nullable = false

于 2012-08-28T13:12:45.230 回答
1

尝试使用cascade = CascadeType.ALL到 hbm 文件中,您在其中定义了 oneToMany 关系

于 2012-08-28T13:16:12.200 回答