Spring JPA日期getReference(getOne)按唯一字段

时间:2018-09-20 16:07:29

标签: jpa spring-data-jpa

我有一个与其他实体有关系的实体,而没有级联这样的东西:

@Entity
public class User {

    //id
    @Id
    private Long id;

    //fields
    @Column(unique = true, nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    @OneToMany(mappedBy = "owner")
    private List<Address> addressList;

}

和这个实体:

@Entity
public class Address{

    //id
    @Id
    private Long id;

    //fields
    private String title;

    @Column(nullable = false)
    private String location;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "owner_id")
    private User owner;

}

在此示例中,我想创建一个地址,而无需从db重新获取用户,并使用jpa getReference,但我没有用户ID,只是用户名如何通过诸如用户名之类的唯一字段调用SpringJpaData getOne()? 有没有更好的选择来创建新地址而无需从数据库获取用户?

0 个答案:

没有答案