具有LAZY关系的Spring数据JPA克隆实体

时间:2019-02-13 14:42:56

标签: java spring spring-data-jpa entity

我有实体:

@Data
@Entity
@Table(name = "USERS")
public class User{

    @Id
    @Column(name = "GUID", nullable = false)
    private String guid;

    @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = LAZY)
    private List<Role> roles;

    @OneToMany(mappedBy = "user", fetch = LAZY, cascade = {CascadeType.ALL})
    private List<Person> persons;

我需要克隆此实体。我是这样的:

usersRepository.detach(user);

但是由于roles,我无法获得personsLAZY。我使用hack:

    user.getRoles().size();
    user.getPersons().size();
    usersRepository.detach(user);

但是我不喜欢它。我可以简化它吗?

0 个答案:

没有答案