Inject SessionFactory Hibernate to ActionHandler

62 views
Skip to first unread message

Łukasz Bączek

unread,
Nov 29, 2016, 9:05:35 PM11/29/16
to GWTP
Hello,

Do you know how to inject SessionFactory to ActionHandler?

For example:

public class ConfigGridSQLHandler extends AbstractActionHandler<ConfigGridSQL, ConfigGridSQLResult> {

@Inject
public ConfigGridSQLHandler() {
super(ConfigGridSQL.class);
}

public ConfigGridSQLResult execute(ConfigGridSQL arg0, ExecutionContext arg1) throws ActionException {
String primaryKeyName = null;
List<MetaDataFieldConfigSQL> list = null;

try {
String sql = "SELECT k.COLUMN_NAME FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' AND t.table_schema=DATABASE() AND t.table_name=:TABLE_NAME";
SQLQuery query = {INJECT SESSIONFACTORY HIBERNATE}.openSession().createSQLQuery(sql);
query.setParameter("TABLE_NAME", SQLToolsImpl.getFirstTableFromSQL(arg0.getSql()).toUpperCase().trim());
System.out.println(SQLToolsImpl.getFirstTableFromSQL(arg0.getSql()).toUpperCase().trim());
List<Object[]> resultKey = query.list();
System.out.println(resultKey.size());
if (resultKey.size() != 0) {
primaryKeyName = String.valueOf(resultKey.get(0));
} else {
throw new PrimaryKeyNotFound();
}
list = SQLToolsImpl.getColumnFromSQL(getSessionFactory(), arg0.getSql());

} catch (HibernateException | JSQLParserException e) {
throw new OtherPrimaryKeyException(e.getMessage());
}
return new ConfigGridSQLResult(primaryKeyName, list);

@Override
public void undo(ConfigGridSQL action, ConfigGridSQLResult result, ExecutionContext context)
throws ActionException {
// TODO Auto-generated method stub

}


I would like to inject SessionFactory (Hibernate) without rigid in its definition of ActionHandler. Is it possible?

Regards

Asier

unread,
Dec 23, 2016, 12:33:01 PM12/23/16
to GWTP
Hi

Have you considered using something like onami-persist?

If you can't include that package then I suppose it's easy to get with a @Provides method in a guice module.

Regards

Łukasz Bączek

unread,
Dec 23, 2016, 8:17:19 PM12/23/16
to gwt-pl...@googlegroups.com
Hi,

Yes, I did it using Guice.

My test code:

package pl.cba.lukaszbaczek.ApplicationTestSQLGrid.server;

import org.hibernate.SessionFactory;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import pl.cba.lukaszbaczek.webMuseumDatabase.server.HibernateUtils;

public class SessionFactoryModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(SessionFactory.class).annotatedWith(Names.named("test")).toInstance(HibernateUtils.getSessionFactory());
    }   
}


package pl.cba.lukaszbaczek.gxtSQLGrid.server.data;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.gwtplatform.dispatch.rpc.server.ExecutionContext;
import com.gwtplatform.dispatch.rpc.server.actionhandler.AbstractActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;

import net.sf.jsqlparser.JSQLParserException;
import pl.cba.lukaszbaczek.gxtSQLGrid.client.data.ConfigGridSQL;
import pl.cba.lukaszbaczek.gxtSQLGrid.client.data.ConfigGridSQLResult;
import pl.cba.lukaszbaczek.gxtSQLGrid.client.grid.OtherPrimaryKeyException;
import pl.cba.lukaszbaczek.gxtSQLGrid.client.grid.PrimaryKeyNotFound;
import pl.cba.lukaszbaczek.gxtSQLGrid.client.sqlTools.MetaDataFieldConfigSQL;
import pl.cba.lukaszbaczek.gxtSQLGrid.server.parserSQL.SQLToolsImpl;


public class ConfigGridSQLHandler extends AbstractActionHandler<ConfigGridSQL, ConfigGridSQLResult> {

    private SessionFactory sessionFactory;

    @Inject
    public ConfigGridSQLHandler() {
        super(ConfigGridSQL.class);
    }

    @Inject
    public void setSessionFactory(@Named("test") SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;

    }

    public ConfigGridSQLResult execute(ConfigGridSQL arg0, ExecutionContext arg1) throws ActionException {
        String primaryKeyName = null;
        List<MetaDataFieldConfigSQL> list = null;
        Session session = null;


        String sql = "SELECT k.COLUMN_NAME FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' AND t.table_schema=DATABASE() AND t.table_name=:TABLE_NAME";
        try {
            session = sessionFactory.openSession();
            SQLQuery query = session.createSQLQuery(sql);
            query.setParameter("TABLE_NAME", SQLToolsImpl.getFirstTableFromSQL(arg0.getSql()).toUpperCase().trim());

            List<Object[]> resultKey = query.list();
            if (resultKey.size() != 0) {
                primaryKeyName = String.valueOf(resultKey.get(0));
            } else {
                throw new PrimaryKeyNotFound();
            }
            list = SQLToolsImpl.getColumnFromSQL(session, arg0.getSql());


        } catch (HibernateException | JSQLParserException e) {
            throw new OtherPrimaryKeyException(e.getMessage());
        } finally {
            session.close();

        }
        return new ConfigGridSQLResult(primaryKeyName, list);
    }

    @Override
    public void undo(ConfigGridSQL action, ConfigGridSQLResult result, ExecutionContext context)
            throws ActionException {
    }
}


Thank you for your interest.


W dniu 23.12.2016 o 18:33, Asier
 pisze:
--
You received this message because you are subscribed to a topic in the Google Groups "GWTP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gwt-platform/-TVLttt40Qs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gwt-platform...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages