Example usage for org.hibernate.type WrapperBinaryType WrapperBinaryType

List of usage examples for org.hibernate.type WrapperBinaryType WrapperBinaryType

Introduction

In this page you can find the example usage for org.hibernate.type WrapperBinaryType WrapperBinaryType.

Prototype

public WrapperBinaryType() 

Source Link

Usage

From source file:lt.emasina.resthub.server.factory.DataFactory.java

License:Open Source License

public CcLob getLob(final Session session, final LobHandler handler) throws Exception {
    final Query q = handler.getQuery();
    final SQLQuery query = getPagedSQLQuery(session, handler);

    final MdColumn c = handler.getMdColumn();
    switch (c.getType()) {
    case BLOB:/*  ww  w .  j a  v  a  2 s .co m*/
        query.addScalar(c.getName(), new WrapperBinaryType());
        break;
    case CLOB:
        query.addScalar(c.getName(), new TextType());
        break;
    default:
        throw new ClientErrorException(Status.CLIENT_ERROR_BAD_REQUEST,
                "Column %d (%s) expected to be LOB found %s", handler.getColumn(), c.getName(),
                c.getType().name());
    }

    if (log.isDebugEnabled()) {
        log.debug(query.getQueryString());
    }

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<CcLob> fetchData = executor.submit(new Callable<CcLob>() {

        @Override
        @SuppressWarnings("unchecked")
        public CcLob call() throws Exception {
            CcLob cc = new CcLob();
            Object o = query.uniqueResult();
            if (o != null) {
                switch (c.getType()) {
                case CLOB:
                    cc.setValue((String) o);
                    break;
                case BLOB:
                    cc.setValue((Byte[]) o);
                    break;
                }
            }
            return cc;
        };
    });

    try {

        return fetchData.get(q.getTimeOut(), TimeUnit.SECONDS);

    } catch (ExecutionException | InterruptedException ex) {
        throw ex;
    } catch (TimeoutException ex) {
        throw new ServerErrorException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, ex);
    }

}