简体   繁体   English

一对一关系中的外键Hibernate

[英]Foreign Key in One to One RelationShip Hibernate

I have an one to one relationship between the following 2 entities:我在以下 2 个实体之间存在一对一的关系:

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

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

    @Column
    private String name;

    @Column
    private String email;

    @Column
    private String password;

    @OneToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_role_id", referencedColumnName = "id")
    private UserRole userRole;
@Entity
@Table(name = "userRole")
public class UserRole {

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

    @Column
    private String description;

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

    public UserRole() {
    }

I also use EntityManagerFactory to create the tables in my local DB.我还使用 EntityManagerFactory 在本地数据库中创建表。 I received this code and I have to follow it.我收到了这个代码,我必须遵循它。

public class UserRepo {

    private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ro.tutorial.lab.SD");

    public void insertNewUser(User user) {
        EntityManager em = entityManagerFactory.createEntityManager();
        em.getTransaction().begin();
        em.persist(user);
        em.getTransaction().commit();
        em.close();
    }

There is a similar UserRoleRepo too.也有一个类似的 UserRoleRepo。

My problem is when instantiating in main, I don't know how to get just the UserRole id for the FK in User.我的问题是在 main 中实例化时,我不知道如何仅获取 User 中 FK 的 UserRole id。 Instead, I get the whole instance of userRole and the error "Duplicate entry 'b36fcb4c-3904-4205-888b-9792f24d8b5c' for key 'userrole.PRIMARY' ".相反,我得到了 userRole 的整个实例和错误“Duplicate entry 'b36fcb4c-3904-4205-888b-9792f24d8b5c' for key 'userrole.PRIMARY' ”。

public static void main(String[] args) {
        UserRoleRepo userRoleRepo= new UserRoleRepo();

        UserRole userRole1 = new UserRole();
        userRole1.setId(UUID.randomUUID().toString());
        System.out.println(userRole1);
        userRole1.setDescription("admin");
        userRoleRepo.insertNewUser(userRole1);

        UserRole userRole2 = new UserRole();
        userRole2.setId(UUID.randomUUID().toString());
        System.out.println(userRole2);
        userRole2.setDescription("client");
        userRoleRepo.insertNewUser(userRole2);

        UserRepo userRepo= new UserRepo();

        User user = new User();
        user.setId(UUID.randomUUID().toString());
        user.setName("Todoran");
        user.setEmail("todoran@utcluj.ro");
        user.setPassword("mona");
        user.setUserRole(userRole1); //////////it breaks here :(((((
        System.out.println(user);
        userRepo.insertNewUser(user);

    }

It seemed that I got confused and the proper relationship is one to many between UserRole and User tables.似乎我感到困惑,正确的关系是 UserRole 和 User 表之间的一对多关系。 Now it works fine.现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM