Hello,
I'm currently writing a program using JavaFX and ObjectDB and I'm encountering a problem with Entities not sending their updates to each other.
I have two Entities in question at the moment: Employee and Paygrade.
@Entity public class Employee { private String id; private String fName; private String lName; private Department dept; private Paygrade pay; ... } @Entity public class Paygrade { private int level; private int rate; ... public boolean setRate() { //Error Checking - All have been verified to be passed Main.paygradeDB.getTransaction().begin(); this.rate = rate; Main.paygradeDB.getTransaction().commit(); return true; } }
Each of these Entities are managed in Main
public class Main extends Application { private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/payrollDB.odb"); public static EntityManager employeeDB = emf.createEntityManager(); public static EntityManager paygradeDB = emf.createEntityManager(); ... }
I use a set of controller classes for each of the models. Each controller package consists of Overview, Add, Modify, Delete.
Overview uses a TabelView to display its content and due to my limited knowledge of Java and ObjectDB I have chosen to not use things such as StringProperty since I didn't know how to make it possible to persist them in ObjectDB. Due to this, I've chosen to write a function called setTable() for each of the Overview controllers which consists of content similar to this for both Employee and Paygrade...
public void setTable() { employees = FXCollections.observableArrayList(Query.employeeTable()); table.setItems(employees); id.setCellValueFactory(itemData -> new ReadOnlyStringWrapper(itemData.getValue().get[field]())); id.setCellFactory(c -> new TableCell<Employee, String>() { @Override protected void updateItem(String item, boolean empty) { if(item != null) { setText(item); setAlignment(Pos.CENTER); } } }); lName.setCellValueFactory(itemData -> new ReadOnlyStringWrapper(itemData.getValue().getLastName())); lName.setCellFactory(c -> new TableCell<Employee, String>() { @Override protected void updateItem(String item, boolean empty) { if(item != null) { setText(item); setAlignment(Pos.CENTER); } } }); fName.setCellValueFactory(itemData -> new ReadOnlyStringWrapper(itemData.getValue().getFirstName())); fName.setCellFactory(c -> new TableCell<Employee, String>() { @Override protected void updateItem(String item, boolean empty) { if(item != null) { setText(item); setAlignment(Pos.CENTER); } } }); dept.setCellValueFactory(itemData -> new ReadOnlyStringWrapper(itemData.getValue().getDepartment().getName())); dept.setCellFactory(c -> new TableCell<Employee, String>() { @Override protected void updateItem(String item, boolean empty) { if(item != null) { setText(item); setAlignment(Pos.CENTER); } } }); pay.setCellValueFactory(itemData -> new ReadOnlyObjectWrapper<Double>(itemData.getValue().getPaygrade().getRate())); pay.setCellFactory(c -> new TableCell<Employee, Double>() { @Override public void updateItem(final Double item, boolean empty) { if(item != null) { setText("$" + df.format(item)); setAlignment(Pos.CENTER); } } }); }
The Query function is as follows
public static ArrayList<Employee> employeeTable() { String str = "SELECT FROM Employee ORDER BY lastName, firstName, department.name, paygrade.rate"; return (ArrayList<Employee>) Main.employeeDB.createQuery(str, Employee.class).getResultList(); }
The problem I'm facing is when I use handleModify() from my PaygradeModify class - the change to the Paygrade is not shown when I go to view my Employee table...
public class PaygradeModify { @FXML private TextField paygrade; private PaygradesOverview controller; private Paygrade p; ... @FXML private void handleModify() { String rate = paygrade.getText(); //Error Checking - all checks get passed if(/*No Errors*/) { p.setRate(rate); controller.setTable(); } else {Main.showErrorDialog();} }
However, it is worth noting that the changes ARE made if the EmployeeOverview table has NOT been visibly loaded during Runtime once. As soon as the table is loaded: all content is displayed accurately. However, once a change has been made to paygradeDB after this visual load - the Employee table refuses to update the displayed Paygrades.
Initially I thought it was a problem in my code for how I loaded the content into the table, but then I tried checking the content from my Query.employeeTable() function. Interestingly, this function was returning the unmodified versions of the Paygrades stored in employeeDB whereas the content of paygradeDB was accurate. To me, this indicated that the two separate EntityManagers were not communicating their changes to each other. Which is where my question comes in...
How do I ensure that any changes done to paygradeDB cascade into employeeDB?