This is not a duplicated issue, the others don't have the same scenario
My problem is similar to this comment: nullable = false not only create database constraint, but his solution disable all check nullability from hibernate, which I don't want, I want it specifically in this @ManyToOne relationship.
I have a Class RegraValidacao.java that can ocasionally have only one of its @ManyToOne fields setted, the others null. Those @ManyToOne relationships are made with a @JoinTable. What I want is: when generating my DDL via spring.jpa.properties.hibernate.hbm2ddl.auto, I want the @JoinColumns present inside the @JoinTable annotation to generate database constraint NOT NULL on my db2 database ONLY.
So, RegraValidacao can have @ManyToOne nullable relationships with the entities ContaLimite.java and Documento.java. It shall be OK to persist a RegraValidacao that only have a ContaLimite.java, like:
RegraValidacao rgr = new RegraValidacao();
rgr.setContaLimite(new ContaLimite());
rgr.setDocumento(null);
hibernate.save(rgr) // SHOULD WORK
But when I set nullable = false on my JoinColumn, it won't let the field Documento to be null.
@Entity
public class RegraValidacao {
@ManyToOne
@JoinTable(name = "CDR_CONTA_LIMITE_REGRA",
joinColumns = {@JoinColumn(name = "RGR_ID", nullable = false)},
inverseJoinColumns = {@JoinColumn(name = "CDL_ID", nullable = false)})
private ContaLimite contaLimite;
@ManyToOne
@JoinTable(name = "DOR_DOCUMENTO_REGRA",
joinColumns = {@JoinColumn(name = "RGR_ID", nullable = false)},
inverseJoinColumns = {@JoinColumn(name = "DOM_ID", nullable = false)})
private Documento documento;
}
EDIT: For simplification, I've just put those two attributes, but actually I have 8 pairs of Attributtes/JoinTables in this class. And why? Because this relationship is mutually excluded. If I have a common table relationship @ManyToOne without JoinTables, I would have 7 null fields on database and 1 set. With JoinTables I will not have any null fields, and if there's an id set in a JoinTable, certainly there's an entry on the tables related.
In other words, the relationship with ContaLimite.java and Documento.java should be: optional = true, but their columns on the @JoinTable should be nullable = false.
What I expect: Tables generated via hbm2ddl.auto:
CDR_CONTA_LIMITE_REGRA --> RGR_ID NOT NULL, CDL_ID NOT NULL //OK
DOR_DOCUMENTO_REGRA --> RGR_ID NOT NULL, CDL_ID NOT NULL //OK
Persisting through Hibernate:
RegraValidacao rgr = new RegraValidacao();
rgr.setContaLimite(new ContaLimite());
rgr.setDocumento(null);
hibernate.save(rgr) // SHOULD BE OK!
What I get: Tables generated via hbm2ddl.auto:
CDR_CONTA_LIMITE_REGRA --> RGR_ID NOT NULL, CDL_ID NOT NULL //OK
DOR_DOCUMENTO_REGRA --> RGR_ID NOT NULL, CDL_ID NOT NULL //OK
Persisting through Hibernate:
RegraValidacao rgr = new RegraValidacao();
rgr.setContaLimite(new ContaLimite());
rgr.setDocumento(null);
hibernate.save(rgr) --> Error, cannot persist null
ERROR: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : br.system.regravalidacao.RegraValidacao.documento; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : br.system.regravalidacao.RegraValidacao.documento
This way I would expect that the columns of the JoinTable would have the NOT NULL constraint, but I can still persist a RegraValidacao with a null Documento.