当实体的主键之间存在关系时,必须在调用save()之前手动分配该类的id

时间:2021-11-21 12:57:25

I have 2 entities between which i want to create a relation such that both share their primary keys. And when we commit one entity the other entity should also be committed with the same primary key which was generated for 1st entity.

我有两个实体,我想在它们之间建立一个关系,使它们共享它们的主键。当我们提交一个实体时,另一个实体也应该使用为第一个实体生成的主键来提交。

My 1st entity is User

我的第一个实体是User

    @Entity
@Table(name = "ENDUSER")
public class User extends LongIdBase implements IActivatable, IUser {
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
     @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, targetEntity = UserLoginRecord.class)
    @PrimaryKeyJoinColumn(name = "id")
    private UserLoginRecord userLoginRecord;

My second entity is UserLoginrecord

我的第二个实体是UserLoginrecord

   @Entity
@Table(name = "ENDUSER_TEMP")
public class UserLoginRecord {
    @Id
    @Column(name = "id")
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name")
    private String name;

I want that when i persist user , a new row for UserLoginRecord should also be created with same primary as of User.

我希望当我持久化用户时,也应该创建与用户相同的主要用户名的新行。

But while trying to persist, I am getting this error below.

但是当我试图坚持的时候,我得到了下面的错误。

ids for this class must be manually assigned before calling save():

1 个解决方案

#1


3  

  1. You get that error because whenever unless specifying an identifier generator, the "assigned" generator will be assumed. The assigned identifier expects you to set the ids manually, so I think you are interested in having the ids generated automatically.

    您会得到这个错误,因为除非指定标识符生成器,否则将假定“已分配”生成器。分配的标识符要求您手动设置id,因此我认为您希望自动生成id。

    Try changing it to:

    试着改变它:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
  2. The User should be the owner of the user/userLoginRecord association so:

    用户应该是用户/userLoginRecord关联的所有者,因此:

    @Entity
    @Table(name = "ENDUSER")
    public class User extends LongIdBase implements IActivatable, IUser {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        @Column(name = "first_name")
        private String firstName;
    
        @Column(name = "last_name")
        private String lastName;
    
    }
    
  3. I think a user can have more UserLoginRecords which means a user will have a one-to-many association to UserLoginRecord and the UserLoginRecord will have a many-to-one association to a User.

    我认为用户可以有更多的UserLoginRecords,这意味着用户将与UserLoginRecord有一对多的关联,UserLoginRecord将与用户有多对一的关联。

  4. Assuming you have a one-to-one relationship between a User and a UserLoginRecord

    假设用户和UserLoginRecord之间存在一对一的关系

    The UserLoginRecord looks like:

    UserLoginRecord看起来像:

    @Entity
    @Table(name = "ENDUSER_TEMP")
    public class UserLoginRecord {
    
        @Id 
        @Column(name="userId", unique=true, nullable=false)
        private Long userId;
    
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name="userId")
        @MapsId
        private User user;
    
  5. For a bidirectional one-to-one association the User may also contain:

    对于双向一对一的关联,用户还可以包含:

        @OneToOne(mappedBy = "user")
        private UserLoginRecord userLoginRecord;
    

    If you go for a bidirectional association don't forget to set both sides prior to saving:

    如果你选择双向关联,不要忘记在保存之前设置两边:

        user.setUserLoginRecord(userLoginRecord);
        userLoginRecord.setUser(user);
    

    Even if the userLoginRecord.user side is the owner of this association and the user.userLoginRecord is the "inverse" side.

    即使userLoginRecord。用户端是这个关联的所有者和用户。userLoginRecord是“逆”端。

#1


3  

  1. You get that error because whenever unless specifying an identifier generator, the "assigned" generator will be assumed. The assigned identifier expects you to set the ids manually, so I think you are interested in having the ids generated automatically.

    您会得到这个错误,因为除非指定标识符生成器,否则将假定“已分配”生成器。分配的标识符要求您手动设置id,因此我认为您希望自动生成id。

    Try changing it to:

    试着改变它:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
  2. The User should be the owner of the user/userLoginRecord association so:

    用户应该是用户/userLoginRecord关联的所有者,因此:

    @Entity
    @Table(name = "ENDUSER")
    public class User extends LongIdBase implements IActivatable, IUser {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        @Column(name = "first_name")
        private String firstName;
    
        @Column(name = "last_name")
        private String lastName;
    
    }
    
  3. I think a user can have more UserLoginRecords which means a user will have a one-to-many association to UserLoginRecord and the UserLoginRecord will have a many-to-one association to a User.

    我认为用户可以有更多的UserLoginRecords,这意味着用户将与UserLoginRecord有一对多的关联,UserLoginRecord将与用户有多对一的关联。

  4. Assuming you have a one-to-one relationship between a User and a UserLoginRecord

    假设用户和UserLoginRecord之间存在一对一的关系

    The UserLoginRecord looks like:

    UserLoginRecord看起来像:

    @Entity
    @Table(name = "ENDUSER_TEMP")
    public class UserLoginRecord {
    
        @Id 
        @Column(name="userId", unique=true, nullable=false)
        private Long userId;
    
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name="userId")
        @MapsId
        private User user;
    
  5. For a bidirectional one-to-one association the User may also contain:

    对于双向一对一的关联,用户还可以包含:

        @OneToOne(mappedBy = "user")
        private UserLoginRecord userLoginRecord;
    

    If you go for a bidirectional association don't forget to set both sides prior to saving:

    如果你选择双向关联,不要忘记在保存之前设置两边:

        user.setUserLoginRecord(userLoginRecord);
        userLoginRecord.setUser(user);
    

    Even if the userLoginRecord.user side is the owner of this association and the user.userLoginRecord is the "inverse" side.

    即使userLoginRecord。用户端是这个关联的所有者和用户。userLoginRecord是“逆”端。