JPA-将父ID插入子表

时间:2018-07-15 11:33:00

标签: jpa

当hibernate(JPA)中的两个对象之间存在多对一关联并且我们想要在不向父表中添加新记录的情况下向子表插入父表的ID时,该如何实现?

@ManyToOne(targetEntity = RoleEntity.class,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "FK_ROLE_ID",referencedColumnName = "ID")
    private RoleEntity role;

我这样写:

UserEntity userEntity=new UserEntity();
        userEntity.setUserName(username);
        userEntity.setPassword(password);
        userEntity.setCreatedDate(new Date().toString());
RoleEntity roleEntity=new RoleEntity();
        roleEntity.setTitle("user");
        userEntity.setRole(roleEntity);

但是最后三行也会在用户表中插入一条新记录。

这完全是roleEntity:

package Entity;

import javax.persistence.*;
import java.io.Serializable;

/**
 * Created by Mohsen on 7/10/2018.
 */
@Entity(name = "role")
@Table(name = "ROLE")
public class RoleEntity implements Serializable {
    @Id
    @Column(name = "ID")
    @SequenceGenerator(name = "SEQ_ROLE", sequenceName = "SEQ_ROLE", allocationSize = 1)
    @GeneratedValue(generator = "SEQ_ROLE", strategy = GenerationType.SEQUENCE)
    private int id;

    @Basic
    @Column(name = "Title")
    private String title;

//    @OneToMany(targetEntity = UserEntity.class,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
//    @JoinColumn(name = "FK_ROLE_ID",referencedColumnName = "ID")
//    private Set<UserEntity> user;


    public RoleEntity() {
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

这完全是userEntity:

package Entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mohsen on 7/10/2018.
 */
@Entity(name = "user")
@Table(name = "USERR")
public class UserEntity implements Serializable {
    @Id
    @Column(name = "ID")
    @SequenceGenerator(name = "SEQ_USER", allocationSize = 1, sequenceName = "SEQ_USER")
    @GeneratedValue(generator = "SEQ_USER", strategy = GenerationType.SEQUENCE)
    private int id;

    @Basic
    @Column(name = "UserName", columnDefinition = "VARCHAR2(20 CHAR)")
    private String userName;

    @Basic
    @Column(name = "Password", columnDefinition = "VARCHAR2(255 CHAR)")
    private String password;

    @Basic
    @Column(name = "CreatedDate")
    private String createdDate;

    @Basic
    @Column(name = "EndedDate")
    private String endedDate;

    @OneToOne(targetEntity = PeopleEntity.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private PeopleEntity people;

    @ManyToOne(targetEntity = RoleEntity.class,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "FK_ROLE_ID",referencedColumnName = "ID")
    private RoleEntity role;

    public RoleEntity getRole() {
        return role;
    }

    public void setRole(RoleEntity role) {
        this.role = role;
    }

    public UserEntity() {
    }

    public int getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getEndedDate() {
        return endedDate;
    }

    public void setEndedDate(String endedDate) {
        this.endedDate = endedDate;
    }

    public PeopleEntity getPeople() {
        return people;
    }

    public void setPeople(PeopleEntity people) {
        this.people = people;
    }

}

1 个答案:

答案 0 :(得分:0)

我找到了解决方法

我在子对象中设置了层叠= CascadeType.REMOVE,并且可以正常工作