0

I create entity class from database with foreing key, the attribute set like this:

@Entity
@Table(name = "PERSON")

//....

@JoinColumn(name = "ID_DEPARTMENT", referencedColumnName = "ID_DEPARTMENT")
@ManyToOne
private Department idDepartment;

// On the other entity class the configuration set like this

@Entity
@Table(name = "DEPARTMENT")

// ...

@OneToMany(mappedBy = "idDepartment")
private Collection<Person> personCollection;

The value of idDepartment in table Person can be null, because at the beginning you don't have that information

When I try save without this value gives the following error:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance.

but I dont need save nothing in Department... I try with cascade all, but save a blank record in table department ( this is not what I want).

How can I save the record in table person without value idDepartment?

  • Can you please check whether `idDepartment` field is set to `null` before saving `Person` – vishnu Jun 04 '20 at 05:04
  • Does this answer your question? [Can a @ManyToOne JPA relation be null?](https://stackoverflow.com/questions/25718229/can-a-manytoone-jpa-relation-be-null) – perissf Jun 04 '20 at 09:03
  • Thanks for the help, If I have verified that the idDepartment value is null and I also tried all the options described in the previous case and none worked. when i put Lazy it gave this error : javax.el.ELException: /Person.xhtml @215,103 value="#{tablePerson.idDepartment.department}": org.hibernate.LazyInitializationException: could not initialize proxy [model.Department#1] - no Session – rramirezech Jun 04 '20 at 21:30

1 Answers1

0

Vishnu thank you very much, that is precisely, my error is that it verified idDepartment was null and if it was, but in the structure the reference was idDepartment for the entity and idDepartment for the primary key, that if it was null, but the entity (object) was not, when I nullified the entity everything worked perfectly.