【问题标题】:Foreign key is showing null while inserting into Child Table, error Cannot insert the value NULL into column插入子表时外键显示为空,错误无法将值 NULL 插入列
【发布时间】:2017-11-01 08:11:55
【问题描述】:

原因:com.microsoft.sqlserver.jdbc.SQLServerException: 不能 将值 NULL 插入列 'WHITE_LIST_UID',表 'ECM3.dbo.WHITE_LIST_MATCHESII';列不允许空值。插入 失败。

我已经对我的问题进行了多次搜索,并且尝试了所有可能的解决方案,但它不起作用。我正在尝试使用具有关系oneToMany 的父表(WhiteList)插入子表(WhiteListMatches),但它显示了先前的错误!

WhiteList.java(父级)

 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 String 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, String entityName) {
            this.whiteListUid = whiteListUid;
            this.entityName = entityName;
        }

        public Integer getWhiteListUid() {
            return whiteListUid;
        }

        public void setWhiteListUid(Integer whiteListUid) {
            this.whiteListUid = whiteListUid;
        }

        public String getEntityName() {
            return entityName;
        }

        public void setEntityName(String 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 String toString() {
            return "com.model.datagear.entity.WhiteList[ whiteListUid=" + whiteListUid + " ]";
        }

    }

WhiteListMatches.java(子)

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 String matchEntityName;

    @Column(name = "MATCH_ENTITY_ADDRESS")
    private String matchEntityAddress;

    @Column(name = "MATCH_ENTITY_YEAR_OF_BIRTH")
    private String matchEntityYearOfBirth;

    @Column(name = "MATCH_ENTITY_DATE_OF_BIRTH")
    private String matchEntityDateOfBirth;

    @Column(name = "MATCH_ENTITY_PLACE_OF_BIRTH")
    private String 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 String getMatchEntityName() {
        return matchEntityName;
    }

    public void setMatchEntityName(String matchEntityName) {
        this.matchEntityName = matchEntityName;
    }

    public String getMatchEntityAddress() {
        return matchEntityAddress;
    }

    public void setMatchEntityAddress(String matchEntityAddress) {
        this.matchEntityAddress = matchEntityAddress;
    }

    public String getMatchEntityYearOfBirth() {
        return matchEntityYearOfBirth;
    }

    public void setMatchEntityYearOfBirth(String matchEntityYearOfBirth) {
        this.matchEntityYearOfBirth = matchEntityYearOfBirth;
    }

    public String getMatchEntityDateOfBirth() {
        return matchEntityDateOfBirth;
    }

    public void setMatchEntityDateOfBirth(String matchEntityDateOfBirth) {
        this.matchEntityDateOfBirth = matchEntityDateOfBirth;
    }

    public String getMatchEntityPlaceOfBirth() {
        return matchEntityPlaceOfBirth;
    }

    public void setMatchEntityPlaceOfBirth(String 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 String toString() {
        return "com.model.datagear.entity.WhiteListMatches[ id=" + id + " ]";
    }

}

插入方法

public void inertIntoWhiteList(String 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();
        }
    }

WhitListMatches 正在从 Set&lt;Party&gt;createWhitListMatch 辅助方法初始化,如果有人想知道列表是如何从其中获取数据的。我所有的 PK 也是数据库中的身份。

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().toString());
        }
        whiteListMatch.setMatchEntityName(party.getENTITY_NAME());
        whiteListMatch.setMatchEntityPlaceOfBirth(party.getPLACE_OF_BIRTH());
        whiteListMatch.setMatchEntityYearOfBirth("" + party.getX_YEAR_OF_BIRTH());
        return whiteListMatch;
    }

【问题讨论】:

  • 我已经尝试过您的解决方案,但仍然无法解决同样的问题!
  • 我认为你的意思是 WHITE_LIST_UID 而不是 whiteListUid 之前尝试过这个但仍然是同样的错误

标签: java sql-server hibernate one-to-many


【解决方案1】:

Hibernate 不会为您做任何额外的工作。所以如果你有 WhiteListMatcheswhiteList == null Hibernate 将 null 保存到数据库中。

你可以这样做:

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

WhiteList 中使用这种方法也是常见的做法

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

【讨论】:

  • 是的,就是这样,您的解决方案与我经过深入调查后发现的结果一致,现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多