我有一个类(车辆),我实现了CRUD操作。删除作品,更新作品,只是创建不起作用。我可以放什么来代替这个创建代码?附言:我使用Eclipse这是FormMasini.java我从Vehicule.java日期中获取,它将以JSF形式显示。错误出现在这一行:this.vehicul = new Vehicul();
package vehiculeFeaa;
import java.util.List;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name = "VEHICUL")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "VEHICUL_TYPE")
public abstract class Vehicul {
@TableGenerator(name = "VEHICUL_GEN", table = "ID_GEN", pkColumnName = "GEN_NUME", valueColumnName = "GEN_VALOARE", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "VEHICUL_GEN")
private int idVehicul;
private String producator;
public int getIdVehicul() {
return idVehicul;
}
public void setIdVehicul(int idVehicul) {
this.idVehicul = idVehicul;
}
public String getProducator() {
return producator;
}
public void setProducator(String producator) {
this.producator = producator;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public List<Vehicul> get(int i) {
// TODO Auto-generated method stub
return null;
}
}
package masinifeaa;
import java.util.ArrayList;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import vehiculeFeaa.Vehicul;
public class FormMasini {
private Vehicul vehicul;
private List<Vehicul>vehicule=new ArrayList<Vehicul>();
public Vehicul getVehicul() {
return vehicul;
}
public void setVehicul(Vehicul vehicul) {
this.vehicul = vehicul;
}
public List<Vehicul> getVehicule() {
return vehicule;
}
public void setVehicule(List<Vehicul> vehicule) {
this.vehicule = vehicule;
}
private EntityManager em;
@SuppressWarnings("unchecked")
public FormMasini() {
EntityManagerFactory emf=
Persistence.createEntityManagerFactory("FirmeJPA");
em=emf.createEntityManager();
this.vehicule= em.createQuery("SELECT v FROM Vehicul v")
.getResultList();
if(!this.vehicule.isEmpty())
vehicul=this.vehicule.get(0);
}
public void backVehicul(ActionEvent evt) {
Integer idxCurent = this.vehicule.indexOf(vehicul);
if(idxCurent > 0)
this.vehicul=this.vehicule.get(idxCurent-1);
}
public void nextVehicul(ActionEvent evt) {
Integer idxCurent = this.vehicule.indexOf(vehicul);
if((idxCurent + 1) < this.vehicule.size())
this.vehicul=this.vehicule.get(idxCurent + 1);
}
//Implementez actiunile CRUD
public void deleteVehicul(ActionEvent evt) {
this.vehicule.remove(this.vehicul);
if(this.em.contains(this.vehicul)) {
this.em.getTransaction().begin();
this.em.remove(this.vehicul);
this.em.getTransaction().commit();
}
if(!this.vehicule.isEmpty())
this.vehicul=this.vehicule.get(0);
else
this.vehicul=null;
}
public void saveVehicul(ActionEvent evt) {
this.em.getTransaction().begin();
this.em.persist(this.vehicul);
this.em.getTransaction().commit();
}
public void abandonVehicul(ActionEvent evt) {
if(this.em.contains(this.vehicul))
this.em.refresh(this.vehicul);
}
public void createVehicul(ActionEvent evt) {
this.vehicul = new Vehicul();
this.vehicul.setidVehicul(999);
this.vehicul.setNume("New car");
this.vehicule.add(this.vehicul);
}
与 JSF 或其他内容无关。你只是不能实例化一个抽象类,这是基本的Java。