List of usage examples for org.hibernate HibernateException HibernateException
public HibernateException(Throwable cause)
From source file:com.infinities.keystone4j.jpa.provider.BoneCPConnectionProvider.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override// w w w . j a v a2s. co m public void configure(Map props) { try { Properties properties = new Properties(); properties.putAll(props); this.config = new BoneCPConfig(properties); // old hibernate config String url = (String) props.get(CONFIG_CONNECTION_URL); String username = (String) props.get(CONFIG_CONNECTION_USERNAME); String password = (String) props.get(CONFIG_CONNECTION_PASSWORD); String driver = (String) props.get(CONFIG_CONNECTION_DRIVER_CLASS); if (url == null) { url = (String) props.get(CONFIG_CONNECTION_URL_ALTERNATE); } if (username == null) { username = (String) props.get(CONFIG_CONNECTION_USERNAME_ALTERNATE); } if (password == null) { password = (String) props.get(CONFIG_CONNECTION_PASSWORD_ALTERNATE); } if (driver == null) { driver = (String) props.get(CONFIG_CONNECTION_DRIVER_CLASS_ALTERNATE); } if (url != null) { this.config.setJdbcUrl(url); } if (username != null) { this.config.setUsername(username); } if (password != null) { this.config.setPassword(password); } // Remember Isolation level String isolationLevel = (String) props.get(Environment.ISOLATION); if ((isolationLevel != null) && (isolationLevel.trim().length() > 0)) { this.isolation = Integer.parseInt(isolationLevel); } String commitMode = (String) props.get(Environment.AUTOCOMMIT); if ((commitMode != null) && (commitMode.trim().length() > 0)) { this.autocommit = Boolean.parseBoolean(commitMode); } logger.debug(this.config.toString()); if (!Strings.isNullOrEmpty(driver)) { loadClass(driver); } if (this.config.getConnectionHookClassName() != null) { Object hookClass = loadClass(this.config.getConnectionHookClassName()).newInstance(); this.config.setConnectionHook((ConnectionHook) hookClass); } // create the connection pool this.pool = createPool(this.config); logStatistics(); } catch (Exception e) { throw new HibernateException(e); } }
From source file:com.jolbox.bonecp.provider.BoneCPConnectionProvider.java
License:Apache License
/** * Pool configuration.//from w ww.jav a2 s . c om * @param props * @throws HibernateException */ public void configure(Properties props) throws HibernateException { try { this.config = new BoneCPConfig(props); // old hibernate config String url = props.getProperty(CONFIG_CONNECTION_URL); String username = props.getProperty(CONFIG_CONNECTION_USERNAME); String password = props.getProperty(CONFIG_CONNECTION_PASSWORD); String driver = props.getProperty(CONFIG_CONNECTION_DRIVER_CLASS); if (url == null) { url = props.getProperty(CONFIG_CONNECTION_URL_ALTERNATE); } if (username == null) { username = props.getProperty(CONFIG_CONNECTION_USERNAME_ALTERNATE); } if (password == null) { password = props.getProperty(CONFIG_CONNECTION_PASSWORD_ALTERNATE); } if (driver == null) { driver = props.getProperty(CONFIG_CONNECTION_DRIVER_CLASS_ALTERNATE); } if (url != null) { this.config.setJdbcUrl(url); } if (username != null) { this.config.setUsername(username); } if (password != null) { this.config.setPassword(password); } // Remember Isolation level this.isolation = ConfigurationHelper.getInteger(AvailableSettings.ISOLATION, props); this.autocommit = ConfigurationHelper.getBoolean(AvailableSettings.AUTOCOMMIT, props); logger.debug(this.config.toString()); if (driver != null && !driver.trim().equals("")) { loadClass(driver); } if (this.config.getConnectionHookClassName() != null) { Object hookClass = loadClass(this.config.getConnectionHookClassName()).newInstance(); this.config.setConnectionHook((ConnectionHook) hookClass); } // create the connection pool this.pool = createPool(this.config); } catch (Exception e) { throw new HibernateException(e); } }
From source file:com.klistret.cmdb.utility.hibernate.XPathAggregation.java
License:Open Source License
/** * Copied from AggregateProjection//w w w . j ava2s . c o m */ protected SQLFunction getFunction(String functionName, CriteriaQuery criteriaQuery) { SQLFunction function = criteriaQuery.getFactory().getSqlFunctionRegistry().findSQLFunction(functionName); if (function == null) { throw new HibernateException("Unable to locate mapping for function named [" + functionName + "]"); } return function; }
From source file:com.klistret.cmdb.utility.hibernate.XPathAggregation.java
License:Open Source License
private List<String> buildFunctionParameterList(Criteria criteria, CriteriaQuery criteriaQuery) { Dialect dialect = criteriaQuery.getFactory().getDialect(); String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); if (columns.length != 1) { logger.error("XMLQUERY may only be used with single-column properties [property: {}]", propertyName); throw new HibernateException("XMLQUERY may only be used with single-column properties"); }//from w w w .j a v a 2 s.co m BaseExpression be = step.getRelativePath().getBaseExpression(); /** * Property type is generalized to wild-card "*" leaving only the * predicate */ String axis = String.format("%s:%s", step.getQName().getPrefix(), step.getQName().getLocalPart()); /** * Passing clause */ String passingClause = String.format("PASSING %s AS \"%s\"", columns[0], variableReference); /** * Setup XPath first with the variable reference (acts a root), the axis * is replaced and then XPath is built up again either from generated * string or the raw XPath for each step (depending on if it is a * readable step or irresolute). */ String xpath = String.format("$%s", variableReference); for (Expr expr : step.getRelativePath().getSteps()) { if (expr instanceof Step) { if (((Step) expr).getDepth() >= step.getDepth()) { if (expr instanceof StepExpr) { xpath = String.format("%s/%s", xpath, expr.getXPath(true)); } if (expr instanceof IrresoluteExpr) { xpath = String.format("%s/%s", xpath, step.getRelativePath().getRawXPath(((Step) expr).getDepth())); } } } } xpath = xpath.replaceFirst(axis, "*"); logger.debug("XPath [{}] prior prefixing default function declaration and namespace declarations", xpath); /** * Concatenate namespace declarations */ for (String namespace : be.getNamespaces()) xpath = namespace.concat(xpath); /** * Concatenate default element namespace declaration */ if (be.getDefaultElementNamespace() != null) xpath = be.getDefaultElementNamespace().concat(xpath); /** * Dialect controlls */ if (dialect instanceof DB2Dialect) { /** * DB2 only allows SQL with double quotes (or at least that is the * extend of my knowledge) */ Matcher sq = singleQuotes.matcher(xpath); if (sq.find()) throw new ApplicationException(String .format("XPath [%s] contains surrounding single quotes which DB2 does not allow", xpath), new UnsupportedOperationException()); } /** * Return the XMLQuery predicate */ String[] results = { String.format("XMLCAST(XMLQUERY(\'%s\' %s) AS VARCHAR(%d))", xpath, passingClause, varcharLimit) }; return Arrays.asList(results); }
From source file:com.klistret.cmdb.utility.hibernate.XPathRestriction.java
License:Open Source License
@Override public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { /**//from ww w. j a va2s . co m * Establish dialect, property information about this column */ Dialect dialect = criteriaQuery.getFactory().getDialect(); String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); if (columns.length != 1) { logger.error("XMLEXISTS may only be used with single-column properties [property: {}]", propertyName); throw new HibernateException("XMLEXISTS may only be used with single-column properties"); } /** * Path Expression */ BaseExpression be = step.getRelativePath().getBaseExpression(); /** * Property type is generalized to wild-card "*" leaving only the * predicate */ String axis = String.format("%s:%s", step.getQName().getPrefix(), step.getQName().getLocalPart()); /** * Passing clause */ String passingClause = String.format("PASSING %s AS \"%s\"", columns[0], variableReference); /** * Setup XPath first with the variable reference (acts a root), the axis * is replaced and then XPath is built up again either from generated * string or the raw XPath for each step (depending on if it is a * readable step or irresolute). */ String xpath = String.format("$%s", variableReference); String sqlMask = baseMask; for (Expr expr : step.getRelativePath().getSteps()) { if (expr instanceof Step) { if (((Step) expr).getDepth() >= step.getDepth()) { if (expr instanceof StepExpr) { xpath = String.format("%s/%s", xpath, expr.getXPath(true)); for (Value value : ((StepExpr) expr).getValues()) { if (value.getText().length() > varcharLimit) throw new ApplicationException( String.format("Literal value [%s] is larger than VARCHAR limiation [%d]", value.getText(), varcharLimit)); String xpathMask = value.getMask(); passingClause = String.format("%s, CAST (? AS VARCHAR(%d)) AS \"%s\"", passingClause, varcharLimit, xpathMask); typedValues.add(new TypedValue(stringType, value.getText(), EntityMode.POJO)); logger.debug("Adding StringType [value: {}] to restriction with variable [{}]", value.getText(), xpathMask); /** * Use a common mask to reduce the variation in * generated SQL */ xpath = xpath.replaceAll(xpathMask, sqlMask); passingClause = passingClause.replaceAll(xpathMask, sqlMask); logger.debug("Replaced XPath mask {} with a common SQL mask {}", xpathMask, sqlMask); sqlMask = incrementMask(sqlMask); } } if (expr instanceof IrresoluteExpr) { xpath = String.format("%s/%s", xpath, step.getRelativePath().getRawXPath(((Step) expr).getDepth())); } } } } xpath = xpath.replaceFirst(axis, "*"); logger.debug("XPath [{}] prior prefixing default function declaration and namespace declarations", xpath); /** * Concatenate namespace declarations */ for (String namespace : be.getNamespaces()) xpath = namespace.concat(xpath); /** * Concatenate default element namespace declaration */ if (be.getDefaultElementNamespace() != null) xpath = be.getDefaultElementNamespace().concat(xpath); if (dialect instanceof DB2Dialect) { /** * DB2 only allows SQL with double quotes (or at least that is the * extend of my knowledge) */ Matcher sq = singleQuotes.matcher(xpath); if (sq.find()) throw new ApplicationException(String .format("XPath [%s] contains surrounding single quotes which DB2 does not allow", xpath), new UnsupportedOperationException()); } /** * Return the XMLEXISTS predicate */ return String.format("XMLEXISTS(\'%s\' %s)", xpath, passingClause); }
From source file:com.knowbout.hibernate.EnumType.java
License:Apache License
/** * Gets the ordinal value from the result set and converts it to the enum instance * @param rs the result set/*w w w .j a v a 2 s.c o m*/ * @param names the names of the columns * @param owner the owning object * @return the enum instance */ public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { //lookup the ordinal int ordinal = rs.getInt(names[0]); if (rs.wasNull()) { return null; } else { Object[] values = enumClass.getEnumConstants(); for (Object value : values) { if (((Enum) value).ordinal() == ordinal) { return value; } } throw new HibernateException("Unknown enum constant: " + ordinal + " for class " + enumClass.getName()); } }
From source file:com.knowbout.hibernate.HibernateUtil.java
License:Apache License
public static Session openSession() { Session s = session.get();/*from w w w . j a v a 2s.c o m*/ if (s != null) { throw new HibernateException("A session is already open."); } s = getSessionFactory().openSession(); session.set(s); return s; }
From source file:com.knowbout.hibernate.HibernateUtil.java
License:Apache License
public static Session currentSession() { Session s = session.get();//from w ww . j av a 2 s. co m if (s == null) { throw new HibernateException("No session is currently open."); } return s; }
From source file:com.knowbout.hibernate.TransactionManager.java
License:Apache License
/** * @return//from w w w . j a v a 2s. c o m * @throws HibernateException */ public static Transaction beginTransaction() throws HibernateException { Session session = HibernateUtil.currentSession(); if (session == null) { throw new HibernateException("No session found."); } if (currentTransaction() != null) { throw new HibernateException("Transaction already started."); } Transaction t = session.beginTransaction(); transaction.set(t); if (log.isDebugEnabled()) { log.debug("vvvvvvvvvvvvvvvvvvvvv BEGIN TRANSACTION: Thread=" + Thread.currentThread().getName() + " vvvvvvvvvvvvvvvvvvvvvvvvvvv"); } return t; }
From source file:com.knowbout.hibernate.TransactionManager.java
License:Apache License
/** * //from w ww .j av a 2s .c om * @throws HibernateException */ public static void commitTransaction() throws HibernateException { if (isRollbackOnly()) { throw new HibernateException("The transaction cannot be committed because it is set to rollback only."); } Transaction t = currentTransaction(); if (t != null) { t.commit(); if (log.isDebugEnabled()) { log.debug("^^^^^^^^^^^^^^^^^^^^^ COMMIT TRANSACTION: Thread=" + Thread.currentThread().getName() + " ^^^^^^^^^^^^^^^^^^^^^^^^^^"); } } transaction.set(null); rollbackOnly.set(null); }