复合主键选择-适用于Hibernate的方法不适用于EclipseLink.为什么? [英] Composite Primary Key Selection- What works with Hibernate doesnt work with EclipseLink. Why?
问题描述
我有两个桌子.
CREATE TABLE profile_table
(
profile_id NUMBER(10,0) PRIMARY KEY,
creator_manager_id NUMBER(10,0),
lastupdate DATE,
description VARCHAR2(255 BYTE),
profile_name VARCHAR2(255 BYTE),
creatorname VARCHAR2(255 BYTE)
);
CREATE TABLE profile_basket_table
(
profile_id NUMBER(10,0),
from_id NUMBER(10,0),
action NUMBER(10,0),
constraint pbt_pk_constraint PRIMARY KEY(profile_id, from_id),
constraint pbt_fk_constraint FOREIGN KEY(profile_id) REFERENCES profile_table(profile_id)
);
我有1个配置文件和* ProfileItem.
I have 1 Profile and * ProfileItem.
1个配置文件--- * ProfileItems(一个配置文件可以具有多个ProfileItems).
1 Profile --- * ProfileItems ( One profile can have Many ProfileItems).
注意:1.从配置文件到profileItem的一对多关系. 2.它是单向关系.
Note : 1. Its a one-to-many relationship from profile to profileItem. 2. Its a unidirectional relationship.
个人资料的PK是profile_id.
PK for Profile is profile_id.
ProfileItem的PK是复合主键(profile_id,from_id)
PK for ProfileItem is a composite primary key ( profile_id, from_id )
首先,我想抱怨EclipseLink不能像Hibernate一样工作:
Firstly, I would I like to complain that EclipseLink doesnt work like Hibernate :
EclipseLink的第一个问题: 只是因为我在子实体中有一个复合主键,称为ProfileItem,它迫使我使用JoinColumns.我不能使用@JoinColumns,因为其他列from_id不是任何其他表的外键. 最初,我使用与hibernate完美配合的相同批注,如下所示:
1st Problem with EclipseLink: Just becasue I have a composite primary key in child entity called ProfileItem, its forcing me to use JoinColumns. I cant use @JoinColumns as other column from_id is not a foreign key to any other table. I initially used same annotation which worked perfectly with hibernate as follows :
@Entity
@Table(name="profile_table")
class Profile {
@Id
@Column(name="profile_id")
private Integer profileId ;
...
/**
* WORKS WITH HIBERNATE AS PERSISTENCE PROVIDER, BUT FAILS WITH ECLIPSELINK
*/
@OneToMany( cascade = CascadeType.ALL )
@JoinColumn( name= "profile_id", referencedColumnName="profile_id" )
private Set<XVMUpdateProfileItem> profileItems = null;
...
}
上面的代码使用eclipselink引发以下错误:
Above code throws following error with eclipselink :
The @JoinColumns on the annotated element [field profileItems] from the entity class [class arun.ucerelay.datastructures.XVMUpdateProfile] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
通过@JoinColumn批注的几个排列组合,我设法使它能够用于加载配置文件和相关的配置文件项.
Some how with several permutations and combinations of @JoinColumn annotation, I managed to get this working for loading profiles and related profile items.
/**
* WORKS WITH ECLIPSELINK AS PERSISTENCE PROVIDER
*/
@OneToMany( cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn( name= "profile_id", referencedColumnName="profile_id" , insertable = true, updatable = true),
@JoinColumn( name= "profile_id", referencedColumnName="profile_id" , nullable = false)
})
private Set<XVMUpdateProfileItem> profileItems = null;
再次声明@JoinColumn是没有意义的……但是很奇怪,它与eclipselink一起使用.这是一个错误.是吗?
This doesnt make sense to declare @JoinColumn again...But weirdly it works with eclipselink. This is a bug. Isnt it?
有人可以告诉我我是否想念任何东西吗?还有其他实现单向关系的正确方法,请告诉我.
Can anyone tell me if I am missing anything ? Is there any other right way of implementing unidirectional relationship, please tell me.
这是有关@JoinColumns的错误吗?我应该登录吗?
This is a bug about @JoinColumns isnt it ? Should I log it ?
这是从一个持久性提供者到另一种持久性提供者的可移植性"吗? 如果无法解决可移植性,那么使用JPA规范有什么用.
Is this called "portability" from one persistence provider to another ??????? What is the use of using JPA specification if it doesnt solve portability.
保存父项和级联的子项存在第二个问题,该问题在休眠状态下有效,而在eclipselink中无效.我会把它作为一个单独的问题发布,以保持其简短性和准确性.
There is a second problem with saving of parent and cascading child items which works with hibernate and doesnt work with eclipselink. Ill post it as a separate question to keep this short and precise.
谢谢. 阿伦·坎德鲁古拉(Arun Kandregula)
Thanks. Arun Kandregula
推荐答案
这似乎是注释处理中的错误.如果此错误仍在最新版本中出现,请在EclipseLink上进行记录.
This seems to be a bug in the annotation processing. Please log this bug on EclipseLink if it still occurs on the latest version.
请注意,@ JoinColumn通常用于@ManyToOne或@OneToOne,而不是@OneToMany.通常,对于@OneToMany,您应该有一个反@@ ManyToOne并使用mappingBy.在@OneToMany上使用@JoinColumn是@OneToMany的一种特殊类型,它自己维护目标表的外键.由于profile_id是主键的一部分,为了正确起见,您确实应该将@ManyToOne放回去,然后可以删除profileId并仅将@Id添加到@ManyToOne中.
Note that @JoinColumn is normally used for @ManyToOne or @OneToOne, not @OneToMany. Normally for a @OneToMany you should have an inverse @ManyToOne and use the mappedBy. Using a @JoinColumn on a @OneToMany is a special type of @OneToMany that maintains the target table's foreign key itself. Since the profile_id is part of your primary key, to be correct you really should have the @ManyToOne back, you could then remove the profileId and just add @Id to the @ManyToOne.
这篇关于复合主键选择-适用于Hibernate的方法不适用于EclipseLink.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!