在实体之间有主键关系时,必须在调用save()之前手动分配此类的id [英] ids for this class must be manually assigned before calling save() when entities have relation between their primary keys

查看:803
本文介绍了在实体之间有主键关系时,必须在调用save()之前手动分配此类的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,我想创建一个关系,以便共享它们的主键。当我们提交一个实体时,另一个实体也应该使用为第一个实体生成的相同主键提交。



我的第一个实体是User

  @Entity 
@Table(name =ENDUSER)
public class UserId 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;

我的第二个实体是UserLoginrecord



<$ p $ ($ name =id)
public class UserLoginRecord {
@Id
@Column(name =id){code> @Entity
@Table(name =ENDUSER_TEMP)
私人长ID;

public Long getId(){
return id;
}

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

@Column(name =name)
私有字符串名称;

我希望当我坚持用户时,UserLoginRecord的新行也应该创建为相同的主



但在尝试持续时,我在下面看到这个错误。

  


解决此类的code> ids必须手动赋值save():
方案


  1. 你会得到这个错误,因为除非指定一个标识符生成器,​​否则将会假定分配生成器。分配的标识符期望您手动设置ID,因此我认为您有兴趣自动生成ID。



    尝试将其更改为:

      @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private长ID;


  2. 用户应该是user / userLoginRecord关联的所有者,所以:



    $ $ p $ $ $ $ c $ @Entity
    @Table(name =ENDUSER)
    public class UserId extends LongIdBase implements IActivatable,IUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name =id)
    private长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;




    $ li $我认为用户可以有更多的用户登录记录,意味着用户将拥有与UserLoginRecord的一对多关联,并且UserLoginRecord将与用户建立多对一关联。

  3. 假设你在User和UserLoginRecord之间有一对一的关系



    UserLoginRecord看起来像:

    <$ p $ ($ 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
    私人用户用户;


  4. 对于双向一对一关联,用户也可以包含:

      @OneToOne(mappedBy =user)
    private UserLoginRecord userLoginRecord;

    如果您要进行双向关联,请不要忘记在保存前设置双方:

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

    即使userLoginRecord.user一方是此关联的所有者,并且user.userLoginRecord是反的一面。



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

    @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

   @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. 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.

    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:

    @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.

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

    The UserLoginRecord looks like:

    @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.

这篇关于在实体之间有主键关系时,必须在调用save()之前手动分配此类的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆