La key étrangère affiche null lors de l'insertion dans Child Table, erreur Impossible d'insert la valeur NULL dans la colonne

Causée par: com.microsoft.sqlserver.jdbc.SQLServerException: Impossible d'insert la valeur NULL dans la colonne 'WHITE_LIST_UID', table 'ECM3.dbo.WHITE_LIST_MATCHESII'; La colonne n'autorise pas les valeurs NULL. INSERT échoue.

J'ai fait beaucoup de searchs pour mon problème et j'ai essayé toutes les solutions possibles, mais ça ne marche pas. J'essaie d'insert dans la table enfant (WhiteListMatches) en utilisant la table parent (WhiteList) avec la relation oneToMany , mais il montre l'erreur précédente!

WhiteList.java (parent)

  import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "WHITE_LISTII") public class WhiteList implements Serializable { @Id @Column(name = "WHITE_LIST_UID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer whiteListUid; @Basic(optional = false) @Column(name = "ENTITY_NAME") private Ssortingng entityName; @OneToMany(cascade = CascadeType.ALL,mappedBy = "whiteList") private List<WhiteListMatches> matches = new ArrayList<>(); public WhiteList() { } public WhiteList(Integer whiteListUid) { this.whiteListUid = whiteListUid; } public WhiteList(Integer whiteListUid, Ssortingng entityName) { this.whiteListUid = whiteListUid; this.entityName = entityName; } public Integer getWhiteListUid() { return whiteListUid; } public void setWhiteListUid(Integer whiteListUid) { this.whiteListUid = whiteListUid; } public Ssortingng getEntityName() { return entityName; } public void setEntityName(Ssortingng entityName) { this.entityName = entityName; } public List<WhiteListMatches> getMatches() { return matches; } public void setMatches(List<WhiteListMatches> matches) { this.matches = matches; } @Override public int hashCode() { int hash = 0; hash += (whiteListUid != null ? whiteListUid.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof WhiteList)) { return false; } WhiteList other = (WhiteList) object; if ((this.whiteListUid == null && other.whiteListUid != null) || (this.whiteListUid != null && !this.whiteListUid.equals(other.whiteListUid))) { return false; } return true; } @Override public Ssortingng toSsortingng() { return "com.model.datagear.entity.WhiteList[ whiteListUid=" + whiteListUid + " ]"; } } 

WhiteListMatches.java (enfant)

 import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "WHITE_LIST_MATCHESII") public class WhiteListMatches implements Serializable { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "MATCH_ENTITY_NAME") private Ssortingng matchEntityName; @Column(name = "MATCH_ENTITY_ADDRESS") private Ssortingng matchEntityAddress; @Column(name = "MATCH_ENTITY_YEAR_OF_BIRTH") private Ssortingng matchEntityYearOfBirth; @Column(name = "MATCH_ENTITY_DATE_OF_BIRTH") private Ssortingng matchEntityDateOfBirth; @Column(name = "MATCH_ENTITY_PLACE_OF_BIRTH") private Ssortingng matchEntityPlaceOfBirth; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "WHITE_LIST_UID", nullable = false) private WhiteList whiteList; public WhiteListMatches() { } public WhiteListMatches(Integer id) { this.id = id; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Ssortingng getMatchEntityName() { return matchEntityName; } public void setMatchEntityName(Ssortingng matchEntityName) { this.matchEntityName = matchEntityName; } public Ssortingng getMatchEntityAddress() { return matchEntityAddress; } public void setMatchEntityAddress(Ssortingng matchEntityAddress) { this.matchEntityAddress = matchEntityAddress; } public Ssortingng getMatchEntityYearOfBirth() { return matchEntityYearOfBirth; } public void setMatchEntityYearOfBirth(Ssortingng matchEntityYearOfBirth) { this.matchEntityYearOfBirth = matchEntityYearOfBirth; } public Ssortingng getMatchEntityDateOfBirth() { return matchEntityDateOfBirth; } public void setMatchEntityDateOfBirth(Ssortingng matchEntityDateOfBirth) { this.matchEntityDateOfBirth = matchEntityDateOfBirth; } public Ssortingng getMatchEntityPlaceOfBirth() { return matchEntityPlaceOfBirth; } public void setMatchEntityPlaceOfBirth(Ssortingng matchEntityPlaceOfBirth) { this.matchEntityPlaceOfBirth = matchEntityPlaceOfBirth; } public WhiteList getWhiteList() { return whiteList; } public void setWhiteList(WhiteList whiteList) { this.whiteList = whiteList; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof WhiteListMatches)) { return false; } WhiteListMatches other = (WhiteListMatches) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public Ssortingng toSsortingng() { return "com.model.datagear.entity.WhiteListMatches[ id=" + id + " ]"; } } 

insert la méthode

 public void inertIntoWhiteList(Ssortingng entityName, Set<Party> parties) { Transaction tx = null; List<WhiteListMatches> matches = new ArrayList<>(); Iterator it = parties.iterator(); while (it.hasNext()) { Party p = (Party) it.next(); WhiteListMatches w = createWhiteListMatch(p); matches.add(w); } try { hibernateSession = getSession(); tx = hibernateSession.beginTransaction(); WhiteList whiteListEntity = new WhiteList(); whiteListEntity.setEntityName(entityName); for (WhiteListMatches wl : matches) { whiteListEntity.getMatches().add(wl); } hibernateSession.save(whiteListEntity); } catch (Exception ex) { ex.printStackTrace(); } finally { tx.commit(); hibernateSession.close(); } } 

Le WhitListMatche s est initialisé à partir de Set<Party> via la méthode helper createWhitListMatch si quelqu'un se request comment la list a obtenu ses données. Tous mes PK sont aussi des identités dans la database.

 private WhiteListMatches createWhiteListMatch(Party party) { WhiteListMatches whiteListMatch = new WhiteListMatches(); whiteListMatch.setMatchEntityAddress(party.getADDRESS()); if (party.getX_BIRTH_DT() != null) { whiteListMatch.setMatchEntityDateOfBirth(party.getX_BIRTH_DT().toSsortingng()); } whiteListMatch.setMatchEntityName(party.getENTITY_NAME()); whiteListMatch.setMatchEntityPlaceOfBirth(party.getPLACE_OF_BIRTH()); whiteListMatch.setMatchEntityYearOfBirth("" + party.getX_YEAR_OF_BIRTH()); return whiteListMatch; } 

Hibernate ne fait aucun travail supplémentaire pour vous. Donc, si vous avez WhiteListMatches avec whiteList == null Hibernate enregistre cette valeur null dans la database.

Vous pouvez faire quelque chose comme ça:

 for (WhiteListMatches wl : matches) { wl.setWhiteList(whiteListEntity); whiteListEntity.getMatches().add(wl); } 

Il est également courant d'avoir une telle méthode dans la list WhiteList

 public void addMatches(List<WhiteListMatches> matches) { for (WhiteListMatches wl : matches) { wl.setWhiteList(this); this.matches.add(wl); } }