我正在努力实现许多关系。当将数据从数据库获取到实体中时,它可以正常工作,但在尝试将新值插入联接表时失败。我有三张桌子:job、job_prozedur和prozedur。联接表包含父表(job和prozedur)的外键和很少的额外列。
实体类是:
@Entity
@Table(name = "DOK_JOB")
Public class Job implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private List<JobProzedur> jobProzedurList = new ArrayList<JobProzedur>();
public Job() {}
@Id
@SequenceGenerator(name="seqJob", sequenceName="DOK_SEQ_1", allocationSize=1)
@GeneratedValue(generator="seqJob")
@Column(name = "JOB_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "JOB_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "job", cascade = CascadeType.ALL, orphanRemoval=true)
@OrderBy(value = "sortLnr")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public List<JobProzedur> getJobProzedurList() {
return jobProzedurList;
}
public void setJobProzedurList(List<JobProzedur> jobProzedurList) {
this.jobProzedurList = jobProzedurList;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Job other = (Job) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
-
@Entity
@Table(name = "DOK_JOB_PROZEDUR")
public class JobProzedur implements Serializable {
private static final long serialVersionUID = 7971463524201735449L;
private JobProzedurId id = new JobProzedurId();
private Integer sortLnr;
private Job job;
private Prozedur proz;
public JobProzedur() {}
@EmbeddedId
public JobProzedurId getId() {
return id;
}
public void setId(JobProzedurId id) {
this.id = id;
}
@Column(name = "SORT_LNR")
public Integer getSortLnr() {
return sortLnr;
}
public void setSortLnr(Integer sortLnr) {
this.sortLnr = sortLnr;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "JOB_ID", referencedColumnName = "JOB_ID", insertable=false, updatable=false, nullable=false)
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "PROZ_ID", referencedColumnName = "PROZ_ID", insertable=false, updatable=false)
public Prozedur getProz() {
return proz;
}
public void setProz(Prozedur proz) {
this.proz = proz;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JobProzedur other = (JobProzedur) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
-
@Embeddable
public class JobProzedurId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer jobId;
private Integer prozId;
public JobProzedurId() {}
public JobProzedurId(Integer jobId, Integer prozId) {
super();
this.jobId = jobId;
this.prozId = prozId;
}
@Column(name = "JOB_ID", nullable=false)
public Integer getJobId() {
return jobId;
}
public void setJobId(Integer jobId) {
this.jobId = jobId;
}
@Column(name = "PROZ_ID")
public Integer getProzId() {
return prozId;
}
public void setProzId(Integer prozId) {
this.prozId = prozId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((jobId == null) ? 0 : jobId.hashCode());
result = prime * result + ((prozId == null) ? 0 : prozId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JobProzedurId other = (JobProzedurId) obj;
if (jobId == null) {
if (other.jobId != null)
return false;
} else if (!jobId.equals(other.jobId))
return false;
if (prozId == null) {
if (other.prozId != null)
return false;
} else if (!prozId.equals(other.prozId))
return false;
return true;
}
@Override
public String toString() {
return "JobProzedurId [jobId=" + jobId + ", prozId=" + prozId + "]";
}
}
-
@Entity
@Table(name = "DOK_PROZEDUR")
public class Prozedur implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String dbName;
private String schemaName;
private String packageName;
private String name;
public Prozedur() {}
@Id
@SequenceGenerator(name="seqProz", sequenceName="DOK_SEQ_1", allocationSize=1)
@GeneratedValue(generator="seqProz")
@Column(name = "PROZ_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "DB_NAME")
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
@Column(name = "SCHEMA_NAME")
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@Column(name = "PACKAGE_NAME")
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
@Column(name = "PROZ_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Prozedur other = (Prozedur) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
我分配prozedur到作业并持久的代码部分是:
// ...
// ...
JobProzedur jobProz = new JobProzedur();
jobProz.setJob(job);
jobProz.setProz(proz);
job.getJobProzedurList().add(jobProz);
entityManager.persist(job);
entityManager.flush();
当坚持我得到异常:
org.hibernate.exception.ConstraintViolationException: ORA-01400: cannot insert NULL into ("DOK"."DOK_JOB_PROZEDUR"."JOB_ID")
我不知道为什么在持久化期间,Hibernate没有给连接表中的外键列赋值。更新之所以有效,是因为我手动分配了这些值,但这不是正确的方法。任何帮助都很感激。我已经为此工作了几天,但我看不到解决方案。请帮帮忙!
您忘记在JobProzedur上添加
和@MapsId
批注。getProz()JobProzedut。getJob()
编辑:正如@PepperBob所说的:这些关联不应该被标记为insertable=false和updateable=false,因为您确实想要插入它们。
但是,通过使用单列、自动生成的主键而不是复合主键,我可以让我的生活更轻松,应用程序也更快。使用唯一约束以确保具有相同作业ID和相同prozedur ID的两行不存在。
我有两个表,当我试图保存通常的实体,扔 组织。springframework。刀。DataIntegrityViolationException:无法添加或更新子行:外键约束失败(,约束外键(,)引用(,));SQL[n/a];约束[null];嵌套的异常是org。冬眠例外ConstraintViolationException:无法添加或更新子行:外键约束失败(,constraint外键(,)参
问题内容: 当我不想相互连接两个表时,我得到以下消息:#1452-无法添加或更新子行:外键约束失败....我想从facebook_users表格中连接oauth_uid2(主键)外键为oauth_uid2的错误列表。但是我总是得到这个信息。我已经清除了数据,但没有任何效果。在表users_facebook中,我还有1条记录。 1452-无法添加或更新子行:外键约束失败(。,在更新级联上删除级联时使
我知道在这个问题上有很多问题,但我一直未能解决。我正在做一个图形界面来添加/删除员工和管理缺勤。 我一直在阅读和测试那些建议stackOverflow1,但我不能更改数据库,因为它已经由我们的教授给出了。 来自外键的值已经在表“service”中,我在代码中称之为“department”。 我有以下数据库(对于我得到问题的表): 在应用程序中,您可以看到这里和下面的图片,我必须修改员工数据,并将其
当我不想将两个表相互连接时,我收到消息:#1452 - 无法添加或更新子行:外键约束失败....我想使用外键oauth_uid2将oauth_uid2(主键)从facebook_users选项卡连接到错误选项卡。但我总是收到这个信息。我已经清除了我的数据,但没有任何效果。同样在表中users_facebook我有 1 条记录。 1452-无法添加或更新子行:外键约束失败(., CONSTRAINT
如果我插入: 我用visual studio代码和mysql控制台的源代码编写代码 第一表: 第二表: 第三表: 我收到消息: 错误1452(23000):无法添加或更新子行:外键约束失败(。,约束外键()在更新级联的删除级联上引用() 请帮帮我:(,,thx:)
错误代码:1452。无法添加或更新子行:外键约束失败(.,约束外键()引用()) 我看过其他关于这个话题的帖子,但没有运气。我是不是在监督什么或者知道该怎么做?