这是正确使用" mappedBy"在@OneToOne关系中?

时间:2015-06-20 16:24:56

标签: java hibernate

我有一个人类,其基本字段如姓名,年龄等......我想为每个人的汽车细节创建一个单独的表格,

所以我以这种方式实现了@OneToOne关系:

@Entity
@Table(name = "PERSON")
public class person {

@OneToOne(mappedBy="person", cascade= CascadeType.ALL)
 private CarDetails carDetails ;

//more fields and getters and setters and of course @Id.....
}

和另一个实体:

@Entity
@Table(name="CAR_DETAILS")
public class CarDetails {

 @Id
 @Column(name= "READER_ID")
 @GeneratedValue(generator="gen")  
 @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="Person"))  
 private long readerId;

 @OneToOne  
 @PrimaryKeyJoinColumn  
 private Person person;

//more fields and getters and setters
}

一切正常但我不确定这是" mappedBy"的正确用法。 我稍微阅读了一下,我遇到了我的实现,但也有其他人,所以我有点困惑。

有人可以详细说明这是否正确使用" mappedBy"?

1 个答案:

答案 0 :(得分:0)

我建议你使用旧时尚模式

@OneToOne(orphanRemoval = true, optional = true, cascade = {CascadeType.ALL}, mappedBy = "person")
@Getter
@Setter
private CarDetails carDetails; 


@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id")//You will need to create foreign key
@Getter
@Setter
private Person person;