将数据添加到数据库添加数据以连接表

时间:2017-01-14 13:45:06

标签: jpa many-to-one

我正在一个项目中工作,我正在寻找将数据添加到数据库,以及两个连接表。

我的父母:

package entity;

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


/**
 * The persistent class for the artiste database table.
 * 
 */
@Entity
@NamedQuery(name="Artiste.findAll", query="SELECT a FROM Artiste a")
public class Artiste implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @Column(name="a_category")
    private String aCategory;

    @Column(name="a_name")
    private String aName;

    private String date;

    //bi-directional many-to-one association to Seat
    @ManyToOne
    @JoinColumn(name="id_seat")
    private Seat seat;

    public Artiste() {
    }

    public int getIdArtiste() {
        return this.idArtiste;
    }

    public void setIdArtiste(int idArtiste) {
        this.idArtiste = idArtiste;
    }

    public String getACategory() {
        return this.aCategory;
    }

    public void setACategory(String aCategory) {
        this.aCategory = aCategory;
    }

    public String getAName() {
        return this.aName;
    }

    public void setAName(String aName) {
        this.aName = aName;
    }

    public String getDate() {
        return this.date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public Seat getSeat() {
        return this.seat;
    }

    public void setSeat(Seat seat) {
        this.seat = seat;
    }

}

我的孩子:

package entity;

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


/**
 * The persistent class for the seat database table.
 * 
 */
@Entity
@NamedQuery(name="Seat.findAll", query="SELECT s FROM Seat s")
public class Seat implements Serializable {
    private static final long serialVersionUID = 1L;

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

    private String seat_CA;

    private String seat_CB;

    private String seat_CC;

    private String seat_CD;

    //bi-directional many-to-one association to Artiste
    @OneToMany(mappedBy="seat")
    private List<Artiste> artistes;

    public Seat() {
    }

    public int getIdSeat() {
        return this.idSeat;
    }

    public void setIdSeat(int idSeat) {
        this.idSeat = idSeat;
    }

    public String getSeat_CA() {
        return this.seat_CA;
    }

    public void setSeat_CA(String seat_CA) {
        this.seat_CA = seat_CA;
    }

    public String getSeat_CB() {
        return this.seat_CB;
    }

    public void setSeat_CB(String seat_CB) {
        this.seat_CB = seat_CB;
    }

    public String getSeat_CC() {
        return this.seat_CC;
    }

    public void setSeat_CC(String seat_CC) {
        this.seat_CC = seat_CC;
    }

    public String getSeat_CD() {
        return this.seat_CD;
    }

    public void setSeat_CD(String seat_CD) {
        this.seat_CD = seat_CD;
    }

    public List<Artiste> getArtistes() {
        return this.artistes;
    }

    public void setArtistes(List<Artiste> artistes) {
        this.artistes = artistes;
    }

    public Artiste addArtiste(Artiste artiste) {
        getArtistes().add(artiste);
        artiste.setSeat(this);

        return artiste;
    }

    public Artiste removeArtiste(Artiste artiste) {
        getArtistes().remove(artiste);
        artiste.setSeat(null);

        return artiste;
    }

}

我的客户:

Artiste a= new Artiste();
Seat b = new Seat();
b.setSeat_CA(request.getParameter("w"));
b.setSeat_CB(request.getParameter("x"));
b.setSeat_CD(request.getParameter("y"));
b.setSeat_CC(request.getParameter("z"));
a.setIdArtiste(b.getIdSeat());
seatFacade.create(b);
a.setAName(request.getParameter("a_name"));
a.setACategory(request.getParameter("a_category"));
a.setDate(request.getParameter("date"));
artisteFacade.create(a);

我为每个人创建了FACADE。

现在我可以添加数据,但我还需要程序添加FOREIGN KEY

1 个答案:

答案 0 :(得分:0)

您不需要获取外键,JPA会做所有事情,所以您应该以正确的方式进行,所以您的实体应该如下所示:

艺人实体

@ManyToOne
@JoinColumn(name="id_seat")
private Seat seat;

席位实体

@OneToMany(mappedBy="seat", cascade = CascadeType.ALL)
private List<Artiste> artistes = new ArrayList<>();

您的代码应如下所示:

Artiste a= new Artiste();
Seat b = new Seat();
b.setSeat_CA(request.getParameter("w"));
b.setSeat_CB(request.getParameter("x"));
b.setSeat_CD(request.getParameter("y"));
b.setSeat_CC(request.getParameter("z"));

a.setAName(request.getParameter("a_name"));
a.setACategory(request.getParameter("a_category"));
a.setDate(request.getParameter("date"));

//add this the Article to the list of Seat like this.
b.getArtistes().add(a);

//a.setIdArtiste(b.getIdSeat()); you don't need this
//artisteFacade.create(a); you dont need this also

//set the Seal to your article
a.setSeat(b);
seatFacade.create(b);

因此,当您坚持Seat时,articles列表将自动保留。

这会对你有帮助。

您可以在此处了解详情:JPA @ManyToOne with CascadeType.ALL