hibernar no insertando padre

I have two mapped class with hibernate:

SETTLEMENT_MODEL_GROUP (el padre):

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

//omitted some not fields

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

}

SETTLEMENT_MODEL childs:

@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;

}

Now I'm doing simple insert into database:

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

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

  sessionFactory.getCurrentSession().save( model );

Me da:

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

Why is hibernate not inserting parent ID, although it exists and is created? Even giving it on standard output returns proper ID...

Moreover I cannot delete insertable = false, updatable = false de SettlementModel cause it gives me Hibernate Error during deployment

preguntado el 28 de agosto de 12 a las 14:08

I think the actual question is why you cannot delete insertable = false, updatable = false. What error do you get? -

I guess because I did not add cascade = CascadeType.ALL a @OneToMany anotación -

2 Respuestas

añadir @OneToMany cascade = CascadeType.ALL
remove nullable = false de los padres

Respondido 28 ago 12, 14:08

Yea that was it. Then I deleted insertable,updateable = false and It worked, thanks! - kamil

Intenta usar cascade = CascadeType.ALL into hbm file, where you defined the oneToMany relationship

Respondido 28 ago 12, 14:08

Yea that was it. Then I deleted insertable,updateable = false and It worked, thanks! - kamil

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.