I'm trying to save - using JPA - the entity called "Proposta", and this entity has a composition with the entity "Motivo" (below my mapping)
@Entity
@Table(
schema = "CADMAT",
name = "TB_PROP",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"TIPO", "COD"})
}
)
public class Proposta implements Serializable, Identifiable {
...
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, optional = true)
@JoinColumn(
name = "COD_MOTIV",
columnDefinition = "NUMERIC",
referencedColumnName = "COD",
insertable = false,
updatable = false,
nullable = true)
private Motivo motivo;
...
}
But when I will create a new registry this error is reported:
javax.persistence.RollbackException: java.lang.IllegalStateException:
During synchronization a new object was found through a relationship that was not marked cascade PERSIST:
br.gov.sp.sefaz.siafem.cm.db.model.Motivo@4ecd00b5.
I can't to put "CascadeType.PERSIST" because following my use case it's possible to save the entity "Proposta" without the entity "Motivo"
I already put the property "optional=true" and "nullable=true" in the annotations, but the error continues.
CodePudding user response:
You got the mistery Chris!!! Actually I have a constructor method in "Proposta" and it was setting properties without verifying if "Motivo" was null or not. I fixed the constructor method and it works.