简体   繁体   中英

JPA insertion in the database failed

i have a problem when i try to insert data into my database. I am doing it with jpa (persist). Here is my code where i call the method insert

private static ServiceTicketRemote serviceTicket;
......
......

public void insertDevices(ActionEvent acteven) {

    java.sql.Date sqlDate = new java.sql.Date(dateDev.getTime());

    if(modele.equals("")|| serialNumber.equals("") || sqlDate.equals("") || categorie.equals("Select device")) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "Champ(s) vide(s)", "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    else {
        Devices device = new Devices();

        Personnel as = getBeanLogin().getAgentSupport();
        Agentinfo agent = getBeanAgentinfo().getSelectedAgentInfo();

        device.setAgentinfo(agent);
        device.setPersonnel(as);
        device.setCategorie(categorie);
        device.setModele(modele);
        device.setNumeroSerie(serialNumber);
        device.setDateEnvoi(sqlDate);

        try {
                serviceTicket.insertDevices(device);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage
                    (FacesMessage.SEVERITY_INFO,"Device added", ""));  
            }
            catch(Exception e) {
                System.out.println("##########ERROR########createPers()########LoginBean#######" + e.getMessage());
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"echec de l'ajout : error", e.getMessage());
                    FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        }
}

and in my class serviceTicketRemote i have the method insertDevices (Devices device) that i call like this:

@PersistenceContext(unitName = "jpa")
private EntityManager em;

@Override
public Devices insertDevices(Devices device) {
    try {
        em.persist(device); 
    }
    catch (Exception e) {
        System.out.println("##ERROR# insertNewTicketting " + e.getMessage());}
    return device;
}

and this is my entity bean Devices:

@Entity
public class Devices implements Serializable {

    private static final long serialVersionUID = 1L;

            @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id_device")
    private int idDevice;

    //bi-directional many-to-one association to Agentinfo
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="id_agentinfo")
    private Agentinfo agentinfo; 

    //bi-directional many-to-one association to Personnel
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="id_per")
    private Personnel personnel;

        @Column(name="categorie")
    private String categorie;

    @Column(name="modele")
    private String modele;

    @Column(name="numero_serie")
    private String numeroSerie;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="date_envoi")
    private Date dateEnvoi;

    public double getIdDevice() {
    return idDevice;
    }

public void setIdDevice(int idDevice) {
    this.idDevice = idDevice;
}

public Agentinfo getAgentinfo() {
    return agentinfo;
}

public void setAgentinfo(Agentinfo agentinfo) {
    this.agentinfo = agentinfo;
}

public Personnel getPersonnel() {
    return personnel;
}

public void setPersonnel(Personnel personnel) {
    this.personnel = personnel;
}

public String getCategorie() {
    return categorie;
}

public void setCategorie(String categorie) {
    this.categorie = categorie;
}

public String getModele() {
    return modele;
}

public void setModele(String modele) {
    this.modele = modele;
}

public String getNumeroSerie() {
    return numeroSerie;
}

public void setNumeroSerie(String numeroSerie) {
    this.numeroSerie = numeroSerie;
}

public Date getDateEnvoi() {
    return dateEnvoi;
}

public void setDateEnvoi(Date dateEnvoi) {
    this.dateEnvoi = dateEnvoi;
}
}

When i run my application, it shows "Device added", i don't have any errors, everything seems working fine but i have nothing in my database, no insertion.

Can anyone help me to solve this problem???

Just a wild guess: EJB references should not be declared as static.

private static ServiceTicketRemote serviceTicket;

Try changing it to:

private ServiceTicketRemote serviceTicket;

and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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