一对一休眠,不生成外键

sd2nnvve  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(275)

这里有4个表:register、user、user roles和user profile image
寄存器和用户被Map成一对一的关系,寄存器的引用在users表中生成。。。。。。这很好。。
现在讨论用户和角色表之间的一对多关系,它还可以通过在角色表中生成用户表引用来完美地工作。。
但问题是在用户和配置文件图像之间进行一对一操作时。配置文件映像未生成用户的引用….为什么在配置文件映像表中未生成用户引用
登记

@Entity
@Getter
@Setter
public class Register {

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

    @OneToOne(cascade = CascadeType.ALL,orphanRemoval = true,mappedBy = "register")
    private User user;

}

用户

@Entity
@Getter
@Setter
public class User {

    @Id
    @Column(name = "User_Id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private UserProfileImage userProfileImage;

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private List<UserRoles> userRoles;

}

用户配置文件图像

@Entity
@Getter
@Setter
public class UserProfileImage {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "type")
    private String type;
    @Column(name = "picByte", length = 100000)
    private byte[] picByte;

    public UserProfileImage() {
        super();
    }

    public UserProfileImage(String name, String type, byte[] picByte) {
        this.name = name;
        this.type = type;
        this.picByte = picByte;
    }
}
qgelzfjb

qgelzfjb1#

用户类中的配置文件Map不正确,并且在您的配置文件类中没有用户字段,因此它不会在配置文件类中生成用户引用。
另外,用户到角色的Map也不正确,您的用户类不会用Map填充角色。
试试这个:

public class User {

    ...
    @OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name = "PROFILE_IMAGE_ID")   // foreign key column in User table
    private UserProfileImage userProfileImage;

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,mappedBy="user")
    private List<UserRoles> userRoles;

}

public class UserProfileImage {
    ...

    @OneToOne(mappedBy="userProfileImage")
    private User user;

    ...
}

public class UserRole {
    ...

    @ManyToOne
    @JoinColumn(name="USER_ID")  // foreign key column in User Role table
    private User user;

    ...
}

相关问题