jpa onetoone_JPA - 非主键字段的@OneToOne关系不起作用

I have a Spring Data JPA backend using Hibernate as the ORM implementation.

This is the model:

__________ _________________________

|Person | |MailConfig |

|________| |_______________________|

| id PK | | uid PK-FK(Person.uid) |

| uid | | ... |

| ... | | |

|________| |_______________________|

@Entity

@Table(name="Person")

public class PersonEntity{

@Id

private String id;

private String uid;

@OneToOne(mappedBy="id", fetch=FetchType.EAGER)

private MailConfigEntity mailConfigNotes;

...

}

@Entity

@Table(name="MailConfig")

public class MailConfigEntity implements Serializable{

@Id

@OneToOne

@JoinColumn(name="uid", table="Person", referencedColumnName="uid", insertable = false, updatable = false)

private PersonEntity id;

...

}

Person table is joined with MailConfig table through a field that is not Person's primary key. When I load an entity using personDAO.findOne(id) I can see the join in the query is performed against person.id instead of person.uid (on personent0_.id=mailconfig2_.uid). Any idea why this isn't working?

Query log:

select

personent0_.id as id8_2_,

personent0_.uid as uid8_2_,

mailconfig2_.uid as uid5_1_

from

Person personent0_

left outer join

mailconfig mailconfig2_

on personent0_.id=mailconfig2_.uid

where

personent0_.id=?

解决方案

As per the documentation, check if this is a foreign key

There are three cases for one-to-one associations: either the

associated entities share the same primary keys values, a foreign key

is held by one of the entities (note that this FK column in the

database should be constrained unique to simulate one-to-one

multiplicity), or a association table is used to store the link

between the 2 entities (a unique constraint has to be defined on each

fk to ensure the one to one multiplicity).

Also the JoinColumn should be on the owner side of the relationship

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值