我尝试将数据插入数据库时出现问题。我是用jpa(坚持)来做的。 这是我的代码,我调用方法插入
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);
}
}
}
在我的班级serviceTicketRemote中我有方法insertDevices(Devices device),我这样称呼:
@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;
}
这是我的实体bean设备:
@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;
}
}
当我运行我的应用程序时,它显示"设备已添加",我没有任何错误,一切似乎都正常,但我的数据库中没有任何内容,没有插入。
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
只是猜测:不应将EJB引用声明为静态。
private static ServiceTicketRemote serviceTicket;
尝试将其更改为:
private ServiceTicketRemote serviceTicket;
看看会发生什么。