带有主键的Hibernate类也是外键 [英] Hibernate class with primary key that is also a foreign key
问题描述
@Entity
@Table(name =USER_DATA)
public class UserData {
实体实体;
@OneToOne(fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name =PK_FK_ENTITY)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public Entity getEntity(){
return entity;
}
public void setEntity(Entity entity){
this.entity = entity;
给出的错误是没有为实体指定标识符。我如何指定实体字段既是主键又是外键?请注意,这里没有'UserData'的类层次结构;它只是一个班级。对于每个'UserData'来说,只会有一个'Entity',因此我们希望将它同时作为主键和外键。
解决方案对于一对一的双向映射,只需在子实体中定义@MapsId注释。
@Entity
@Table(name =USER_DATA)
public class UserData {
@OneToOne(cascade = CascadeType.ALL,mappedBy =userData,orphanRemoval = true)
私人实体实体;
public void setEntity(Entity entity){
this.entity = entity;
if(null!= entity&&& entity.getUserData()!= this){
entity.setUserData(this);
$ b @Entity
@Table(name =ENTITY)
公共类实体{
@Id
私人长ID;
@MapsId
@OneToOne
@JoinColumn(name =user_data_id)
private UserData userData;
public void setUserData(UserData userData){
this.userData = userData;
if(null!= userData&&&& userData.getEntity()!= this){
userData.setEntity(this);
}
}
}
到许多单向映射,您必须使用@ElementalCollection和@CollectionTable,并使用@Embeddable注释来注释Entity.class
@Entity
@Table(name =USER_DATA)
public class UserData {
@ElementCollection
@CollectionTable(name =entity,
joinColumns = @ JoinColumn(name =user_data_id),
uniqueConstraints = {@UniqueConstraint(columnNames
= {user_data_id,name})})
private final Set< Entity> entities = new LinkedHashSet<>();
public void setEntities(Set< Entity> entities){
this.entities.clear();
if(null!= entities){
this.entities.addAll(entities);
可嵌入
公共类实体{
@Column
@Access( AccessType.FIELD)
私有字符串名称;
}
请参阅以下文章以加深理解: >
1.使用@PrimaryKeyJoinColumn共享主键的@OneToOne http://vard-lokkur.blogspot.my/2011/05/onetoone-with-shared-primary-key.html 。
- @OneToOne使用@MapsId共享主键 http://vard-lokkur.blogspot.my/2014/05/onetoone-with-shared-primary-key.html
@Entity
@Table(name = "USER_DATA")
public class UserData {
Entity entity;
@OneToOne(fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name="PK_FK_ENTITY")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
}
Error given is "No identifier specified for entity". How can I specify that the entity field is both a primary and a foreign key? Note that there is no class hierarchy for 'UserData' here; it is just a single class. It just so happens that for every 'UserData' there will only be one 'Entity', hence we want to make it both a primary and a foreign key.
解决方案 For one to one bidirectional mapping, just define the @MapsId annotation at the child entity.
@Entity
@Table(name = "USER_DATA")
public class UserData {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "userData", orphanRemoval = true)
private Entity entity;
public void setEntity(Entity entity) {
this.entity = entity;
if (null != entity && entity.getUserData() != this) {
entity.setUserData(this);
}
}
}
@Entity
@Table(name = "ENTITY")
public class Entity {
@Id
private Long id;
@MapsId
@OneToOne
@JoinColumn(name = "user_data_id")
private UserData userData;
public void setUserData(UserData userData) {
this.userData = userData;
if (null != userData && userData.getEntity() != this) {
userData.setEntity(this);
}
}
}
For one to many unidirectional mapping, You have to use @ElementalCollection and @CollectionTable and annotate Entity.class with @Embeddable annotation
@Entity
@Table(name = "USER_DATA")
public class UserData {
@ElementCollection
@CollectionTable(name = "entity",
joinColumns = @JoinColumn(name = "user_data_id"),
uniqueConstraints = { @UniqueConstraint(columnNames
= { "user_data_id", "name" }) })
private final Set<Entity> entities = new LinkedHashSet<>();
public void setEntities(Set<Entity> entities) {
this.entities.clear();
if (null != entities) {
this.entities.addAll(entities);
}
}
}
@Embeddable
public class Entity {
@Column
@Access(AccessType.FIELD)
private String name;
}
Kindly refers to the following articles for better understanding:
1. @OneToOne with shared primary key using @PrimaryKeyJoinColumn http://vard-lokkur.blogspot.my/2011/05/onetoone-with-shared-primary-key.html.
- @OneToOne with shared primary key using @MapsId http://vard-lokkur.blogspot.my/2014/05/onetoone-with-shared-primary-key.html
这篇关于带有主键的Hibernate类也是外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!