do not want to save data to the second table

I have two Entity one User(Owing entity - consists of field roleId as mapping between user and role) and other is Role. Roles are already created in the database. I have screen where in the User selects particular User Role on User creation. When i save the data i only want to save user details along with the role Id in the User table. I do not want to save the role details as it is already present.

I am using Hibernate for persistence. Using @OneToOne mapping.

    @Entity   
    public class User{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false)
        private String firstName;


        @Column(nullable = false)
        private String lastName;

        @OneToOne(fetch = FetchType.EAGER)
            @JoinColumn(name = "role_id")
            private Role role;

    }


@Entity
public class Role {

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

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

}

Currently it is trying to save both User plus Role details. Which is not required. Please help on how to solve the issue.


我建议你限制级联删除:

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JoinColumn(name = "role_id")
private Role role;
链接地址: http://www.djcxy.com/p/63686.html

上一篇: Hibernate在Java类之间的ORM映射

下一篇: 不想将数据保存到第二个表中