【问题标题】:How to add child entity without parent in OneToOne bidirectional relationship如何在 OneToOne 双向关系中添加没有父级的子实体
【发布时间】:2019-01-19 18:55:26
【问题描述】:

我有 2 个实体:组和用户。 两者都是 OneToOne 关系,User 是 parent,Group 是 child。

问题是我将组实体保存到数据库时出错: org.hibernate.id.IdentifierGenerationException: 试图从空的一对一属性 [com.robert.app.ws.io.entity.GroupEntity.user] 分配 id

集团实体:

    public class GroupEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private String name;

@OneToMany(mappedBy = "group")
private List<ChildEntity> children;

//FK z tablicy users
@OneToOne
@MapsId
UserEntity user;

//getters and setters

用户实体:

    public class UserEntity implements Serializable {

private static final long serialVersionUID = 8748598732973872010L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(nullable = false)
private String userId;

@Column(nullable = false, length = 50)
private String firstName;

@Column(nullable = false, length = 50)
private String lastName;

@Column(nullable = false, length = 15)
@Enumerated(EnumType.STRING)
private Gender gender;

@Column(nullable = false, length = 20)
private String pesel;

@Column(nullable = false, length = 20)
@Enumerated(EnumType.STRING)
private AccountType accountType;

@Column(nullable = false, length = 60)
private String address;

@Column(nullable = false, unique = true)
private String email;

private String encryptedPassword;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", 
orphanRemoval = true)
private List<ChildEntity> children = new ArrayList<>();

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private GroupEntity group;

GroupServiceImlp 类:

    @Override
    public GroupDto createGroup(GroupDto group) {

    GroupEntity groupEntity = new GroupEntity();
    BeanUtils.copyProperties(group, groupEntity);

    GroupEntity storedGroup = groupRepository.save(groupEntity);

    GroupDto returnValue = new GroupDto();
    BeanUtils.copyProperties(storedGroup, returnValue);

    return returnValue;
}

我想创建组并稍后将用户分配到该组(希望在 android 应用程序中进行)。我有一个按钮,我只是将新组添加到数据库。在另一个视图中,我想将已创建的特定组与用户匹配。

我确信我不了解与 JPA 中的关系相关的内容,但我是 Spring 的新手,所以我正在寻求帮助。

【问题讨论】:

    标签: hibernate spring-boot spring-data-jpa


    【解决方案1】:

    删除GroupEntity中的@MapsId

    默认情况下,实体按 ID 映射。你的GroupEntity 应该是这样的。

    public class GroupEntity {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @Column(nullable = false, unique = true)
        private String name;
    
        @OneToMany(mappedBy = "group")
        private List<ChildEntity> children;
    
        //FK z tablicy users
        @OneToOne
        UserEntity user;
    
        .
        .
        .
    }
    

    【讨论】:

    • 它有帮助!我必须删除表格才能使其工作,但它工作:) 在这种情况下,当我想将组与用户匹配时 - 我应该怎么做?如果我的关系是单向和双向的,你能举个例子吗?
    • 我不确定您想要实现什么。您的关系是双向关系。 user 指的是group,group 指的是user。如果您想要单向关系 - 删除其中一个引用。
    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 2019-03-14
    • 1970-01-01
    • 2016-05-17
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多