0

I'm working in a project and I'm looking to add data to the database, to two join tables.

My parent:

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;
    }

}

My child:

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;
    }

}

My client:

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);

And I create the FACADE for each one.

Now I can add data but i need also the program add the FOREIGN KEY.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nas Sim
  • 59
  • 1
  • 10

1 Answers1

0

You don't need to get the foreign key, JPA is do every thing, so you should just make it in the right way, so you entities should look like this:

Artiste Entity

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

Seat Entity

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

Your code should look like this:

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);

So when you persist the Seat the list of articles will persist automaticlly.

This will help you.

You can learn more here : JPA @ManyToOne with CascadeType.ALL

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • I tried it but it doesn't work, i think the problem is in my client but idon't know the right code to add in both tables and also for the foreign key. – Nas Sim Jan 14 '17 at 14:26
  • yes i edit my answer check now, how this can help you @NasSim – Youcef LAIDANI Jan 14 '17 at 14:35
  • i got this error: 2017-01-14T15:46:13.586+0100|Avvertenza: StandardWrapperValve[servlet.ArtisteServlet]: Servlet.service() for servlet servlet.ArtisteServlet threw exception java.lang.NullPointerException at servlet.ArtisteServlet.doPost(ArtisteServlet.java:99) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) – Nas Sim Jan 14 '17 at 14:46
  • Ye i think you need to init your List `@OneToMany(mappedBy="seat", cascade = CascadeType.ALL) private List artistes = new ArrayList<>();` – Youcef LAIDANI Jan 14 '17 at 14:50
  • Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'id_seat' cannot be null Error Code: 1048 Call: INSERT INTO ARTISTE (a_category, a_name, DATE, id_seat) VALUES (?, ?, ?, ?) bind => [4 parameters bound] – Nas Sim Jan 14 '17 at 15:07
  • Are you sure you put `@OneToMany(mappedBy="seat", cascade = CascadeType.ALL)` in `private List artistes = new ArrayList<>();` – Youcef LAIDANI Jan 14 '17 at 15:10
  • Yes i did that, in fact when i set the foreign key id_seat in the table as Not NULL i got this error and when i cancel that property as Default NULL null, the add is working but the culmn of the id_seat as foreing key is empty what i don't want. – Nas Sim Jan 14 '17 at 15:17
  • can you try with `a.setSeat(b);` and tell me result, just add it before the `seatFacade.create(b);` – Youcef LAIDANI Jan 14 '17 at 15:19
  • Thnak you so much it's working: a.setSeat(b); seatFacade.create(b); artisteFacade.create(a); – Nas Sim Jan 14 '17 at 15:37