- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这可能是一个常见问题,但到目前为止我找不到任何解决方案。
我有以下 2 个实体:使命 和代表。两个实体分别组成了主键:
表示和任务之间存在一对多关系,这意味着每个任务都有一个表示,并且每个表示可以在不同的任务中使用。
我使用 JPA 存储库将实体保存在数据库中:
public interface RepresentationDao extends JpaRepository <Representation, RepresentationId>
public interface MissionDao extends JpaRepository <Mission, MissionId>
我需要首先保存一组表示,然后保存一组任务。这些表示工作正常,但是当我尝试保存任务时,表示的外键仍然为空。
这是我的代码:
// here representations are already persisted (in a separate transaction)..
RepresentationId representationId = new RepresentationId(idImpXml, codeRepresentation);
Representation representation = representationDao.findOne(representationId);
// representation is retrieved correctly
MissionId missionId = new MissionId(idImpXml, code, categorie);
Mission mission = new Mission(missionId, representation, label);
// I try to save the mission
missionDao.saveAndFlush(mission);
// THE FOREIGN KEY THAT SHOULD REFERENCE THE REPRESENTATION (CODE_REPRESENTATION) REMAINS NULL
使命
@Entity
@Table(name = "MISSION", schema = "dbo", catalog ="TEST")
public class Mission implements java.io.Serializable {
private MissionId id;
private Representation representation;
private String label;
public Mission() {
}
public Mission(MissionId id, Representation representation, String label) {
this.id = id;
this.representation = representation;
this.label = label;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "idImpXml", column = @Column(name = "ID_IMP_XML", nullable = false, precision = 38, scale = 0)),
@AttributeOverride(name = "code", column = @Column(name = "CODE", nullable = false)),
@AttributeOverride(name = "categorie", column = @Column(name = "CATEGORIE", nullable = false)) })
public MissionId getId() {
return this.id;
}
public void setId(MissionId id) {
this.id = id;
}
@MapsId("ID_IMP_XML")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns(
value={ @JoinColumn(name = "ID_IMP_XML", referencedColumnName = "ID_IMP_XML", updatable=false),
@JoinColumn(name = "CODE_REPRESENTATION", referencedColumnName = "CODE_REPRESENTATION", insertable=true, updatable=true)
})
public Representation getRepresentation() {
return this.representation;
}
public void setRepresentation(Representation representation) {
this.representation = representation;
}
@Column(name = "LABEL", nullable = false)
public String getLabel() {
return this.label;
}
public void setLabel(String label) {
this.label = label;
}
}
任务ID
@Embeddable
public class MissionId implements java.io.Serializable {
private Long idImpXml;
private String code;
private int categorie;
public MissionId() {
}
public MissionId(Long idImpXml, String code, int categorie) {
this.idImpXml = idImpXml;
this.code = code;
this.categorie = categorie;
}
@Column(name = "ID_IMP_XML", nullable = false, precision = 38, scale = 0)
public Long getIdImpXml() {
return this.idImpXml;
}
public void setIdImpXml(Long idImpXml) {
this.idImpXml = idImpXml;
}
@Column(name = "CODE", nullable = false)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name = "CATEGORIE", nullable = false)
public int getCategorie() {
return this.categorie;
}
public void setCategorie(int categorie) {
this.categorie = categorie;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof MissionId))
return false;
MissionId castOther = (MissionId) other;
return ((this.getIdImpXml() == castOther.getIdImpXml()) || (this.getIdImpXml() != null
&& castOther.getIdImpXml() != null && this.getIdImpXml().equals(castOther.getIdImpXml())))
&& ((this.getCode() == castOther.getCode()) || (this.getCode() != null && castOther.getCode() != null
&& this.getCode().equals(castOther.getCode())))
&& (this.getCategorie() == castOther.getCategorie());
}
public int hashCode() {
int result = 17;
result = 37 * result + (getIdImpXml() == null ? 0 : this.getIdImpXml().hashCode());
result = 37 * result + (getCode() == null ? 0 : this.getCode().hashCode());
result = 37 * result + this.getCategorie();
return result;
}
}
代表
@Entity
@Table(name = "REPRESENTATION", schema = "dbo", catalog ="TEST")
public class Representation implements Serializable {
private RepresentationId id;
private Long colorPattern;
private String typePattern;
private Integer thickness;
public Representation() {
}
public Representation(RepresentationId id) {
this.id = id;
}
public Representation(RepresentationId id, Long colorPattern, String typePattern,
Integer thickness, Long codeCouleurPattern2, String typePattern2, Integer epaisseurPattern2) {
this.id = id;
this.colorPattern = colorPattern;
this.typePattern = typePattern;
this.thickness = thickness;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "idImpXml", column = @Column(name = "ID_IMP_XML", nullable = false, precision = 38, scale = 0)),
@AttributeOverride(name = "codeRepresentation", column = @Column(name = "CODE_REPRESENTATION", nullable = false)) })
public RepresentationId getId() {
return this.id;
}
public void setId(RepresentationId id) {
this.id = id;
}
@Column(name = "COLOR_PATTERN")
public Long getColorPattern() {
return this.colorPattern;
}
public void setColorPattern(Long colorPattern) {
this.colorPattern = colorPattern;
}
@Column(name = "TYPE_PATTERN")
public String getTypePattern() {
return this.typePattern;
}
public void setTypePattern(String typePattern) {
this.typePattern = typePattern;
}
@Column(name = "THICKNESS")
public Integer getThickness() {
return this.thickness;
}
public void setThickness(Integer thickness) {
this.thickness = thickness;
}
}
RepresentationId
@Embeddable
public class RepresentationId implements java.io.Serializable {
private Long idImpXml;
private int codeRepresentation;
public RepresentationId() {
}
public RepresentationId(Long idImpXml, int codeRepresentation) {
this.idImpXml = idImpXml;
this.codeRepresentation = codeRepresentation;
}
@Column(name = "ID_IMP_XML", nullable = false, precision = 38, scale = 0)
public Long getIdImpXml() {
return this.idImpXml;
}
public void setIdImpXml(Long idImpXml) {
this.idImpXml = idImpXml;
}
@Column(name = "CODE_REPRESENTATION", nullable = false)
public int getCodeRepresentation() {
return this.codeRepresentation;
}
public void setCodeRepresentation(int codeRepresentation) {
this.codeRepresentation = codeRepresentation;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof RepresentationId))
return false;
RepresentationId castOther = (RepresentationId) other;
return ((this.getIdImpXml() == castOther.getIdImpXml()) || (this.getIdImpXml() != null
&& castOther.getIdImpXml() != null && this.getIdImpXml().equals(castOther.getIdImpXml())))
&& (this.getCodeRepresentation() == castOther.getCodeRepresentation());
}
public int hashCode() {
int result = 17;
result = 37 * result + (getIdImpXml() == null ? 0 : this.getIdImpXml().hashCode());
result = 37 * result + this.getCodeRepresentation();
return result;
}
}
最佳答案
尝试通过在对象中添加另一个字段来覆盖列定义,并在 @JoinColum 中将 insertable 和 updateable 属性设置为 false,如下所示:
@MapsId("ID_IMP_XML")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns(
value={ @JoinColumn(name = "ID_IMP_XML", referencedColumnName = "ID_IMP_XML", insertable=false, updatable=false),
@JoinColumn(name = "CODE_REPRESENTATION", referencedColumnName = "CODE_REPRESENTATION", insertable=false, updatable=false)
})
public Representation getRepresentation() {
return this.representation;
}
private Integer representationCode;
@Column(name = "CODE_REPRESENTATION")
public Integer getRepresentationCode() {
return this.representationCode;
}
关于java - hibernate -JPA : foreign key not set in ManyToOne relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44496291/
仍在习惯正则表达式中的环视,但似乎无法理解 \b(?>! key)foreign\b 要成为正确的解决方案?我试图在某些文本中找到所有外来词的实例,但不是外键或外键。 最佳答案 前瞻需要在 forei
我有一个外国表,例如: CREATE FOREIGN TABLE film ( id varchar(40) NOT NULL, title varcha
我的意思是例如我可以创建表 create table XTable ( idt int not null primary key, value nvarchar(50), idq int,
我有以下表格: 财务: PK_FinancialID FK_SchoolID 学校: PK_SchoolID 类: PK_ClassID FK_SchoolID 类(class)名 Class 和 F
我有一个旧数据库,我不能用每个实体中具有外键属性的两个实体更改它: EntityOne ForeignKey1 (pointing to EntityTwo.ForeignKey2) Enti
当我尝试运行代码时,出现此错误 Cannot add or update a child row: a foreign key constraint fails (hotel_info.results
好的,我尝试了这种从外国翻译过来的方法,它确实有效我在我的库中的 structs.lisp 文件中定义了这些,它在我所有其他依赖项之前首先加载 (cffi:defcstruct (cv-size :c
我有一个 Django 程序员的问题应该很简单,但目前我不知道如何解决。 我有这三个模型(我尽量简化): class Nations(models.Model): label = models
真的很讨厌占用别人的时间,但问题似乎并没有消失。 我考虑了 http://verysimple.com/2006/10/22/mysql-error-number-1005-cant-create-t
我需要一双新的眼睛来看看我到底做错了什么。 CREATE TABLE IF NOT EXISTS `spring_normalize`.`users` ( `username` VARCHAR(6
我正在尝试在表中添加外键约束。我的表结构是:表请购单 我想在下一张表中添加申请表的外键 申请批准 当我尝试使用以下 SQL 查询添加外键约束时: ALTER TABLE `requisition_ap
每当我尝试将数据插入“学生”表时,都会收到此错误。下面是两个表,我使用的是MySql: 学生表: | Field | Type | Null | Key | Defaul
我收到错误代码 1215:无法为子表添加外键约束。父表具有复合主键。我想使用该复合主键作为子表中的外键。请指导我。 父表 CREATE TABLE health.procedures( Spe
var state = require('./state') module.exports = function (sequelize, DataTypes) { var city = seq
我可以通过 this case 部分解决这个问题 不幸的是,Preload() 函数似乎无法在相关对象集中进行更深入的研究。 澄清一下,我有以下模型: type Room struct {
我正在使用 Laravel 6。我创建了一些迁移,但我无法让它们成功运行。这些是我的迁移。 public function up() { Schema::create('nationa
这是我的总输出: Executing SQL script in server ERROR: Error 1215: Cannot add foreign key constraint CREATE
我有 postgres 数据库,我的应用程序是使用 django 构建的,我使用 south migration 来维护数据库模式。我有以下场景: user_table与userclickstream
简单的问题。只是想知道是否可以在不需要我在 Java 代码中手动强制执行此约束的情况下完成此操作。这两个外键(一起在同一个表中)都引用另一个表,但对于每一行,它们不得指向同一个外项。 link tex
我正在为我的模型关联使用 Sails 和 Waterline,但我不确定要做什么才能修复我在尝试更新 PageChild 对象时收到的错误。 module.exports = { tableN
我是一名优秀的程序员,十分优秀!