我是Java新手,我尝试使用primefaces数据表来保存数据库中的数据。我可以在datatable中编辑一些值,但刷新后没有任何改变。这是我的代码。
User.java
@Entity
@Table(name="users")
public class User {
private int id;
public String name = null;
public String surname = null;
public String username = null;
public String description = null;
public String email = null;
public String phone = null;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
@Column(name = "Name")
public String getName(){
return name;
}
public void setName(String Name){
this.name = Name;
}
@Column(name = "Surname")
public String getSurname(){
return surname;
LogonTest.java
@ViewScoped
@SessionScoped
@javax.faces.bean.ManagedBean(name = "logonTest")
public class LogonTest implements Serializable{
@PersistenceUnit(unitName="Webbeans_RESOURCE_LOCAL")
private EntityManagerFactory emf;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public List<User> userList = new ArrayList();
@PostConstruct
public void init(){
EntityManager em = emf.createEntityManager();
// Read the existing entries and write to console
Query q = em.createQuery("SELECT u FROM User u");
userList = q.getResultList();
System.out.println("Size: " + userList.size());
}
public LogonTest() {
}
}
TableBean.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.ManagedBean;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RowEditEvent;
@ViewScoped
@javax.faces.bean.ManagedBean(name = "tableBean")
public class TableBean implements Serializable {
private List<User> carsSmall;
public TableBean() {
carsSmall = new ArrayList<User>();
populateRandomCars(carsSmall, 9);
}
private void populateRandomCars(List<User> list, int size) {
for(int i = 0 ; i < size ; i++)
list.add(new User());
}
public List<User> getCarsSmall() {
return carsSmall;
}
public void onEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage(" Edited", ((User) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage(" Cancelled", ((User) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}