原因:com。微软sqlserver。jdbc。SQLServerException:无法将值NULL插入表ECM3的“白色列表”列中。dbo。白名单;列不允许空值。插入失败。
我已经为我的问题做了很多搜索,我尝试了所有可能的解决方案,但都不起作用。我试图使用关系为oneToMany
的父表(白名单)插入子表(白名单匹配),但它显示了前面的错误!
白名单。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();
}
}
正在从集合初始化
WhitlistMatch
s
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;
}
HiberNate不会为您做任何额外的工作。所以如果您有WhiteListMatches
withWhiteList==null
HiberNate将null
保存到数据库中。
你可以这样做:
for (WhiteListMatches wl : matches) {
wl.setWhiteList(whiteListEntity);
whiteListEntity.getMatches().add(wl);
}
在白名单
public void addMatches(List<WhiteListMatches> matches) {
for (WhiteListMatches wl : matches) {
wl.setWhiteList(this);
this.matches.add(wl);
}
}
Hy, 我为数据库创建了以下3个表: ); 我有以下存储过程 C#代码: 但在我将数据插入使用上述代码的表单后,我得到了以下错误: 无法将值NULL插入列User_Id...... PLATFORM. MDF. dbo. Buyer;列不允许空值。INSERT失败。 无法将值NULL插入列Type_Id,表... PLATFORM. MDF. dbo. User;列不允许空值。INSERT失败。
这是我的插入代码,后面是创建表的代码 我不想插入任何东西到列占用,我不知道为什么这是发生的或如何修复它。我只想在tblGrave(GraveName)中插入值。任何帮助都会很好。
使用flyway-core:4.1.2进行数据库迁移。 添加了一个新的DDL文件供flyway执行。Flyway会正确执行DDL并对表和列进行相应的更改。(我们在新的DDL中添加了一个表并更改了以前的一些列)。 但是,flyway无法将此尝试注册到schema_version表:我得到以下错误: 架构[dbo]的当前版本:2.1 无法在元数据表[dbo]中插入版本“3.0”的行。[schema_v
问题内容: 我正在尝试像这样从C#向我的数据库中插入一个空值: DBNull.Value是日期可以在的地方,但我希望它等于null,但它似乎将默认日期设置为1900 … 问题答案: 改成: 返回空字符串,但您希望改为null。 但是,这种构建查询的方式可能会导致问题。例如,如果您的字符串之一包含引号’,则结果查询将引发错误。更好的方法是使用参数并在SqlCommand对象上进行设置:
我有一个表,列:,,。我试图实现简单的插入功能,但当我提交表单时,我无法将NULL值插入“name”列、table“database_name”中。dbo。用户的;列不允许空值。插入失败。 我正在学习这个教程。 地点主人 请帮忙,提前谢谢!
我有以下代码: 其中,是一个标识列,其中不允许有空值。在我的Excel文件中,该列是空的(除了标题) 我得到以下错误消息: 无法将值NULL插入列AmountId中,表AnalyticsV2P7. CapModel. LedgerAmountByECMAcCountByRptLOB;列不允许空值。INSERT失败。 我知道不允许有空值,因为我是这样设置的,但它不应该填充标识列,这样就没有空值了吗?