質問

I'm using GWT RequestFactory to communicate with server. In the server i using hibernate and implement one-session-per-request pattern to control sessions.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        sf.getCurrentSession().beginTransaction();

        chain.doFilter(request, response);

        sf.getCurrentSession().getTransaction().commit();
    } catch (Throwable ex) {
        ex.printStackTrace();
        sf.getCurrentSession().getTransaction().rollback();
    }
}

In the model, i put some validations with annotations. The problem is when i try to persist this entity with invalid informations, the validation occur normally returning to view the messages through onViolation method, but that information is saved in the database anyway.

What i discovery is that hibernate always save the model, even if a don't call save method.

From the request factory it don't even touch in persist method. It stops in the findXxxx method.

Any hint of what can i do to solve this? My object is not allow that a entity been persisted with invalid information.

I don't know if this is important for the case, but i using MultiTenancy of hibernate.

役に立ちましたか?

解決

This is the expected behavior.

RequestFactory mandates one-session-per-request, but that doesn't mean one-transaction-per-request! You should open/close transactions in each and every service method (e.g. your persist() method).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top