I have here a Entity Class
@Entity
@Table(name = "student", catalog = "studdb", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"),
@NamedQuery(name = "Student.findById", query = "SELECT s FROM Student s WHERE s.id = :id"),
@NamedQuery(name = "Student.findByName", query = "SELECT s FROM Student s WHERE s.name = :name"),
@NamedQuery(name = "Student.findBySurname", query = "SELECT s FROM Student s WHERE s.surname = :surname")})
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@Column(name = "Name")
private String name;
@Basic(optional = false)
@Column(name = "Surname")
private String surname;
public Student() {
}
public Student(Integer id) {
this.id = id;
}
public Student(Integer id, String name, String surname) {
this.id = id;
this.name = name;
this.surname = surname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
And Here is my JPA Controller:
public class StudentJpaController implements Serializable {
public StudentJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Student student) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(student);
em.getTransaction().commit();
} catch (Exception ex) {
if (findStudent(student.getId()) != null) {
throw new PreexistingEntityException("Student " + student + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Student student) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
student = em.merge(student);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = student.getId();
if (findStudent(id) == null) {
throw new NonexistentEntityException("The student with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Student student;
try {
student = em.getReference(Student.class, id);
student.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The student with id " + id + " no longer exists.", enfe);
}
em.remove(student);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Student> findStudentEntities() {
return findStudentEntities(true, -1, -1);
}
public List<Student> findStudentEntities(int maxResults, int firstResult) {
return findStudentEntities(false, maxResults, firstResult);
}
private List<Student> findStudentEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Student.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Student findStudent(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Student.class, id);
} finally {
em.close();
}
}
Based on the TableView API for javaFX 8.0,
we can create a TableView<Student>
, and create some columns, depended on the attributes of the class Studdent,
but the problem is, my Student class doesen't have
ex. SimpleStringProperty
for a String object
or SimpleIntegerProperty
for a Integer object or a PDT (int)
So here is my FXMLController for my TableView Example which gets the Data from dhe Database Table using the EntityClasses and the JPA,
public class TableViewExController implements Initializable {
@FXML
private TableView<Student> tableView;
@FXML
private TableColumn<Student, String> nameCol;
@FXML
private TableColumn<Student, String> SurCol;
public ObservableList<Student> getStudentList() {
ObservableList<Student> data = FXCollections.observableArrayList();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication1PU");
StudentJpaController st = new StudentJpaController(emf);
List<Student> list = st.findStudentEntities();
for (Student stu : list) {
data.add(stu);
}
return data;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
tableView = new TableView<>();
nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory<>("nameProperty"));
SurCol = new TableColumn<>("Surname");
SurCol.setCellValueFactory(new PropertyValueFactory<>("surnameProperty"));
tableView.setItems(getStudentList());
for (Student s : getStudentList()) {
System.out.println(s.getName() + " : " + s.getSurname());
}
tableView.getColumns().addAll(nameCol, SurCol);
}
}
and when a launch the args, in the main class which is this..
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TableViewEx.fxml"));
Pane pane = (Pane)loader.load();
Scene sc = new Scene(pane);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String [] args){
launch(args);
}
The result is a table, with no data.. HOW CAN I ADD ITEMS, IN THE TABLE, WITHOUT THE PROPERTY ATTRIBUTES IN THE STUDENT CLASS?