英文:
referencedColumnNames not mapped to a single property error during mapping to a non primary key Spring JPA
问题
这是您提供的内容的翻译:
我正试图在两个表之间建立一对多的关系,其中父表中有一个非主键 id,但我得到了以下错误信息:
> referencedColumnNames 未映射到单个属性。
以下是具有一对多关系的数据库表结构。父表具有(Email, Emp_SSN) 的复合主键,但不参与任何关系形成。但父表 EMP_DETAILS 中的 eid,它具有 唯一约束 和 非空约束,被映射到子表 MODEL_DETAILS 作为外键。
[![在这里输入图片描述][1]][1]
以下是为子表和父表创建的实体。
EmpDetails.java
@Table(EMP_DETAILS)
@Entity
public class EmpDetails{
@EmbeddedId
private EmpDetailsIdentity empIdentity;
@Id
@Column(name="eid", nullable=false)
private Integer eid;
@OneToMany(cascade=CascadeType.ALL, mappedBy="empDetails")
private Set<ModelDetails> modelDetailsSet;
//其他列映射
}
ModelDetails.java
@Table(MODEL_DETAILS)
@Entity
public class ModelDetails{
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName="eid", nullable=false)
private EmpDetails empDetails;
//其他列映射和getter、setter
}
EmpDetailsIdentity.java
@Embeddable
public class EmpDetailsIdentity{
@Column(name="email")
private String email;
@Column(name="emp_ssn")
private String empSsn;
//getter和setter
}
我不确定我在哪里出了问题!有人可以帮忙吗?
[1]: https://i.stack.imgur.com/xzAIV.png
英文:
I am trying to form a one-to-many relation between two tables with a non primary key id from the parent table but I am getting
> referencedColumnNames not mapped to a single property.
Here is the DB table structure with one-to-many relation. The parent table has a compoiste key of(Email, Emp_SSN) which is not participating in any relation formation. But the eid of parent table EMP_DETAILS which has Unique constraint and Not null is mapped to child table MODEL_DETAILS as foreign key.
[![enter image description here][1]][1]
Below are the entities create for both child and parent tables.
EmpDetails.java
@Table(EMP_DETAILS)
@Entity
public class EmpDetails{
@EmbeddedId
private EmpDetailsIdentity empIdentity;
@Id
@Column(name="eid", nullable=false)
private Integer eid;
@OneToMany(cascade=CascadeType.ALL, mappedBy="empDetails"
private Set<ModelDetails> modelDetailsSet;
//other column mappings
}
ModelDetails.java
@Table(MODEL_DETAILS)
@Entity
public class ModelDetails{
@Id
@Column(name="id)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName="eid", nullable=false)
private EmpDetails empDetails;
//Other column mappings and getters setters
}
EmpDetailsIdentity.java
@Embeddable
public class EmpDetailsIdentity{
@Column(name="email")
private String email;
@Column(name="emp_ssn")
private String empSsn;
//getters and setters
}
I am not sure what and where am I missing! Can someone please help.
[1]: https://i.stack.imgur.com/xzAIV.png
答案1
得分: 0
根据 @Kawu 的建议,我移除了 @EmbeddedId 并删除了 EmpDetailsIdentity。去除 embeddedId 是有意义的,因为我们没有在任何关系映射中使用 Embeddable。将 EmpDetailsIdentity 中的列映射转移到 EmpDetails 中。更新后的 EmpDetails 如下:
@Table(EMP_DETAILS)
@Entity
public class EmpDetails{
@Id
@Column(name="eid", nullable=false)
private Integer eid;
@OneToMany(cascade=CascadeType.ALL, mappedBy="empDetails")
private Set<ModelDetails> modelDetailsSet;
@Column(name="email")
private String email;
@Column(name="emp_ssn")
private String empSsn;
}
这对我起作用!感谢 @Kawu。
英文:
As suggested by @Kawu, I removed @EmbeddedId and deleted EmpDetailsIdentity. It does makes sense to remove embeddedId as when we are not using Embeddable for any relationship mapping. Transferred column mappings from EmpDetailsIdentity into EmpDetails. The working EmpDetails looks like below,
@Table(EMP_DETAILS)
@Entity
public class EmpDetails{
@Id
@Column(name="eid", nullable=false)
private Integer eid;
@OneToMany(cascade=CascadeType.ALL, mappedBy="empDetails"
private Set<ModelDetails> modelDetailsSet;
@Column(name="email")
private String email;
@Column(name="emp_ssn")
private String empSsn;
}
This worked for me! Thanks @Kawu.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论