I´m new using Hibernate and Vaadin and need some help:
My application reads from a database cars(car_id, manufacturer, type, miles) and uses a BeanItemContainer to set it as datasource for a table.
Deleting and addind Cars to the table and the datasource of my application works fine. But how can i save the changes to the Database?
I thought table.setWriteThrough(true); and table.setImmediate(true); set changes automatically to the datasource of the application and the database? only my datasource is updated…
How can i write the content of the datasource to my database? do i need something like an valuechangelistener on my datasource to write every change into the database?
I dont know how to synchronize the datasource in the app with my database through hibernate
If you do use a database aware container, such as JPAContainer or HbnContainer (or SQLContainer), the item class used by the container could handle writes to the database. With in-memory containers, you need to call persist() etc. yourself after committing changes to the (in-memory) item.
If you do use an in-memory container, I would recommend BeanContainer (Vaadin 6.5) rather than BeanItemContainer for entities, as it permits using e.g. the database id of your entities as the item id. This can also cause some complications (need for conversions) depending on where you are using the container, but would eliminate the requirement for id-based equals() and hashCode() methods in your entities.
public class CustomerForm extends Form implements ClickListener
private BeanItem beanItem;
@Autowired // Spring
private ICustomerService customerService;
@Override
public void setItemDataSource(Item newDataSource) {
if (newDataSource != null) {
// Get the Selected Item and Cast to Bean
beanItem = (BeanItem<Item>) newDataSource;
List<Object> orderedProperties = Arrays.asList(KeyMatchingMainPanel.NATURAL_COL_ORDER);
super.setItemDataSource(newDataSource, orderedProperties);
super.setReadOnly(true);
getFooter().setVisible(true);
} else {
super.setItemDataSource(null);
getFooter().setVisible(false);
}
}
@Override
public void buttonClick(ClickEvent event) {
Button source = event.getButton();
Customer customer = null;
if (source == save) {
/* If the given input is not valid there is no point in continuing */
if (!isValid()) {
return;
}
commit();
// Get the Customer Object
customer = (Customer) beanItem.getBean();
// Call Service
customerService.saveCustomer(customer);
setReadOnly(true);
} else if (source == cancel) {
discard();
setReadOnly(true);
} else if (source == edit) {
setReadOnly(false);
}
}
even i am facing the same problem i am using hibernate and BeanItemcontainer to et data from database my table container is BeanItemContainer and i had wriiten a method in my hibernate to get data from database and called that method in my BeanItemContainer i can able to edit and delete the row of a table in the Frontend table but that changes are not applying to the database for that i had created Two buttons for editing and deleting the user.If i again done event on the getting data from databaseinto my vaadin table i can able to retrieve the old database table data.How i can achieve this for editing,deleting ,adding new row and able to persist to the database
Hi!! I have a Table component with Beanitemcontainer (entities list) associated. I set this table on Editable Mode, so…i can change all values. How can I save all changes that I made with just one SAVE button? Thankssss a lot.