JPA规范是否允许对非主键列的简单引用?
我在我的Countries表上有一个简单的替代/自然键(UNIQUE,NOT NULL)列iso_code,我想在引用中使用它,但Eclipse的Dali显示验证错误,Hibernate抛出MappingException.
是否允许这种常见情况?
我在DB中有以下架构(简化)
MainTable(
ID primary key
SOMEFIELD
CODE_FK1 -- references OtherTable1 CODE (without declared foreign key)
CODE_FK2 -- references OtherTable2 CODE (without declared foreign key)
... Other fields used
)
OtherTable1(
CODE primary key
LABEL
... other fields not used
)
OtherTable2(
CODE primary key
LABEL
... other fields not used
)
Run Code Online (Sandbox Code Playgroud)
我问是否有任何方法可以为主表定义我的实体,以便直接使用其他表中的标签,即不为这些其他表定义实体.
我无法更改数据库架构,这非常糟糕(在多个表中定义了标签/代码耦合,在多个表中定义).如果有可能,这个解决方案将允许我的代码简单,因为我真的不需要这些其他实体.
我猜它会产生类似的结果:
@Entity
public class MainEntity{
@Id
private Integer ID;
@Column(name="SOMEFIELD")
private String SomeField;
@SomeAnnotation to Join CODE_FK_1 with OtherTable1.CODE
@SomeAnnotation like @Column(name="LABEL", table="OtherTable1")
private String Label1;
@SomeAnnotation …Run Code Online (Sandbox Code Playgroud) 我有两个实体,我想使用他们共同的字段连接在一起,称为shared_id.该字段不是任一实体的主键.shared_id是唯一的 - 每个Hipster都有一个唯一的shared_id.
表格如下:
Hipster Fixie
========= ========
id id
shared_id shared_id
Run Code Online (Sandbox Code Playgroud)
Hipsters和他们的Fixies之间存在OneToMany关系.我尝试过这样的事情:
@Entity
public class Hipster {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "shared_id")
private Integer sharedId;
@OneToMany(mappedBy = "hipster")
private List<Fixie> fixies;
}
@Entity
public class Fixie {
@Id
@Column(name = "id")
private Integer id;
@ManyToOne
@JoinColumn(name = "shared_id", referencedColumnName = "shared_id")
private Hipster hipster;
}
@Repository
public class HipsterDAO {
@PersistenceContext
private EntityManager entityManager;
public Hipster getHipsterBySharedId(Integer sharedId) {
String queryString = "SELECT h …Run Code Online (Sandbox Code Playgroud)