Persistence with Vaadin
I'm trying to learn vaadin. I'm really confused how to do any kind of application development with persistence because if I have a reference to my service layer objects, they get stored in session and I really don't want that. Persistence stuff just isn't serializable.
My case is that I have an already made application that uses Spring and I'm creating a new UI for it with Vaadin. I can inject the spring stuff to my Vaadin application but that gets stored in the session. Any tips?
By the way how do you post in the Va开发者_JAVA技巧adin forums? I can't find a post button or way to register :)
My suggestions:
- Define your containers as only containing presentation data. Don't keep any direct references from your presentation objects to your persistent objects.
- Define a "load" operation that, within a transaction, loads the persistent data objects and converts them into the corresponding presentation objects that back your container.
- Make all your containers read-only (following the model-view-controller pattern: the container is the model)
- Set up notifications/triggers as necessary to reload your containers when the underlying persistent data changes.
- For scalability, design or use containers that know how to lazy load, etc., so you don't populate the entire container at once (note: difficult with
TreeTable
- see issue #9516)
Have you tried this http://vaadin.com/directory#addon/enterprise-app. This add-on use spring and hibernate.
Seems like the Vaadin book covers this issue. The solution they recommend is to use ThreadLocal to store the Service objects. The ThreadLocal is initialised and destroyed in the transactionStart and transactionEnd of the Application class.
More detailed instructions in the Vaadin book: http://vaadin.com/book/-/page/advanced.global.html
I use hibernate with Vaadin. My main application class has a session factory method:
public Session getSession() {
Session currentSession = HibernateUtil.getSessionFactory()
.getCurrentSession();
if(!currentSession.getTransaction().isActive()) {
currentSession.beginTransaction();
}
return currentSession;
}
Every class that needs to persist anything will use a reference to the main app then grab a session.
Then I have a service layer that uses HQL named queries then returns that data as Lists.
public List<DataClass> getEntriesforDate(Date date, Integer key) {
return mainApp.getSession().getNamedQuery(DataClass.NAMEDQUERY)
.setParameter("key", key).list();
}
For saving you just have a ORM class mapped to a table, you populate an instance of that class then use merge to persist the object:
getSession().merge(o);
Here is a detailed explanation from the Vaadin Wiki:
Hibernate with Vaadin
In Vaadin persistence data is not stored in your session, so you do not need to worry about this.
精彩评论