Hibernate Criteria.setMaxResults() fails on Oracle 11g - java

When using hibernate to retrieve data from Oracle 11g DB using either org.hibernate.dialect.Oracle10gDialect or org.hibernate.dialect.OracleDialect
I get the following:
org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
Looking in the log we can see the query:
select top ? this_.LI_ILN as LI1_8_0_, this_.COUNTRY_CODE ...
Obviously, the DB doesn't recognize the keyword and this is where the problem lies, because in Oracle pagination can only be done by using ROWNUM, a thing which Hibernate should know.
The hibernate configuration looks as follows:
<hibernate-configuration>
<session-factory name="HibernateSessionFactory">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">...</property>
<property name="hibernate.connection.url">...</property>
<property name="hibernate.connection.username">...</property>
<property name="hibernate.default_schema">...</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<mapping resource="HB_Mappings/Supplier.hbm.xml" />
</session-factory>
The query is done like so:
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Supplier.class);
crit.setFirstResult(50 * pageIndex);
crit.setMaxResults(50);
List<Supplier> list = crit.list();
Any help is appreciated.
Solved:
Forgot to mention that I am using spring in which the applicationContext.xml looks like:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:HB_Mappings/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
It overwrote the hibernate.cfg.xml's property...
Note to self: go easy on copy-paste

OracleDialect is deprecated, see here:
http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/dialect/OracleDialect.html
Use the Oracle10gDialect instead: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/dialect/Oracle10gDialect.html
Also Check this out, and make sure that you are using the newest Oracle JDBC driver.
https://web.archive.org/web/20130204044852/https://community.jboss.org/wiki/SupportedDatabases2

Related

I need to connect hibernate with mongoDB. Please provide the hibernate.cfg for mongoDB. I have one but its not working

hibernate.cfg
<session-factory>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.ogm.datastore.provider">MONGODB</property>
<property name="hibernate.ogm.mongodb.database">rcfdb</property>
<property name="hibernate.ogm.mongodb.host">127.0.0.1</property>
<property name="hibernate.ogm.mongodb.port">27017</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">./Indexes</property>
<property name="hibernate.search.default.locking_strategy">single</property>
<mapping resource="beanDao.hbm.xml"/>
</session-factory>
Getting error with the above hibernate.cfg. Unable to connect to mongoDB.
As pointed out in the comments, you need to set the credentials and the authentication database; these are the properties:
hibernate.ogm.datastore.username
hibernate.ogm.datastore.password
hibernate.ogm.mongodb.authentication_database
You can find the list of properties for MongoDB in the official Hibernate OGM documentation: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_configuring_mongodb

ArrayIndexOutOfBounds - Hibernate

I am using hibernate with a database wich is in a WAS (Websphere Application Server [IBM]) and I am getting this error sometimes:
java.lang.ArrayIndexOutOfBoundsException
at com.ibm.ws.rsadapter.spi.CacheMap.removeLRU(CacheMap.java:378)
at com.ibm.ws.rsadapter.spi.CacheMap.add(CacheMap.java:180)
at com.ibm.ws.rsadapter.spi.WSRdbManagedConnectionImpl.cacheStatement(WSRdbManagedConnectionImpl.java:3150)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.closeWrapper(WSJdbcPreparedStatement.java:513)
at com.ibm.ws.rsadapter.jdbc.WSJccPreparedStatement.closeWrapper(WSJccPreparedStatement.java:290)
at com.ibm.ws.rsadapter.jdbc.WSJdbcObject.close(WSJdbcObject.java:241)
at com.ibm.ws.rsadapter.jdbc.WSJdbcObject.close(WSJdbcObject.java:194)
at org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:563)
at org.hibernate.jdbc.AbstractBatcher.closeStatement(AbstractBatcher.java:291)
at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:214)
at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1300)
at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:365)
Is a multithread application, so may be some stuff related with the cache of hibernate when multiple access are performed, but I am not sure about it. The error is thrown when the hibernate query sentence is executed like
query.list();
or
query.executeUpdate();
I have tried to set the cacheable to false and the CacheMode to IGNORE:
Session session = getSession();
session.setCacheMode(CacheMode.IGNORE);
session.createSQLQuery('query').setCacheable(false);
for disabling the cache, but the exceptions keep appearing, as I said, sometimes.
My hibernate-config.xml is this one:
<hibernate-configuration>
<session-factory name="HibernateSessionFactory">
<property name="hibernate.dialect">${hibernate.dialect}</property>
<property name="hibernate.show_sql">${hibernate.show_sql}</property>
<property name="hibernate.format_sql">${hibernate.format_sql}</property>
<property name="hibernate.use_sql_comments">${hibernate.use_sql_comments}</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.aggressive_release">false</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
</session-factory>
</hibernate-configuration>
Here you can see as I keep trying to totally disable the cache, but It still not work properly.
Any help or suggestion would be appreciated.
Thank you very much in advance! :D

Spring+hibernate vs hibernate configuration. Cause of UnsupportedOperationException: Not supported by BasicDataSource

Initially I used only hibernate
And I had following hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="connection.url">jdbc:mysql://localhost:3306/...</property>
<property name="connection.username">root</property>
<property name="connection.password">XXX</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
//mapping
...
</session-factory>
</hibernate-configuration>
And It works good:
after I include Spring and then configuration look so:
...
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/..." />
<property name="username" value="root" />
<property name="password" value=XXX />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
...
after it I see in console:
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1432)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
After I tried to remove
<property name="connection.username">root</property>
<property name="connection.password">XXX</property>
from hibernate.cfg.xml and I don't see exceptions.
Can you explain what is the cause of this problems?
Initially I thought that problem that I shouldn't duplicate information in different configurations but now I see that for example url define inside dataSource and inside hibernate.cfg.xml
Please clarify this Spring + Hibernate magic.
Be aware that with:
org.springframework.jdbc.datasource.DriverManagerDataSource
You're not provided with connection pooling!
I would recommend to move to Tomcat JDBC Connection Pool.
That is by now IMHO the most efficient one.
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
It requires the dependency
org.apache.tomcat tomcat-jdbc
Check documentation:
https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
I had the same problem. The BasicDataSource class will connect using its own url, username and password parameters. Session factory will use already configured data source. Therefore as a result the url, username and password parameters are overridden. Just ditch those connection parameters in hibernate.cfg.xml because they are managed by the dataSource bean.
I had this same issue and after above solution I got a destroy method 'close' not found error or something along those lines.
However, it does appear that getConnection(user, password) is the underlying issue here. When I commented out the connection.username and connection.password in the hibernate.cfg.xml everything works fine.
Correction: I should clarify that I moved the user name and password to the appContext like so
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://127.0.0.1:3306/my_schema"
p:username="root"
p:password="mypassword">
</bean>
In latest version getConnection(User,Password) method is not supported.
It will helps you :
Replace
org.apache.commons.dbcp2.BasicDataSource
with
org.springframework.jdbc.datasource.DriverManagerDataSource

Prefix for hibernate connection configurations?

Does anybody know about the hibernate configurations file (hibernate.cfg.xml), what are the "connection" properties - without the "hibernate" prefix - used for?
I mean, why do those properties (such as connection.url, connection.usermame...) exist?
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
...
<property name="connection.url">SOME_URL</property>
<property name="hibernate.connection.url">SOME_URL</property>
<property name="connection.username">SOME_USER</property>
<property name="hibernate.connection.username">SOME_USER</property>
...
Why can I use the both connection.url and hibernate.connection.url ?
ANSWER:
Ok, I believe those properties (without the hibernate. prefix) exist just for backward compatibility with configuration files of older hibernate versions.
Thanks.
The hibernate.connection.url is use to connect the database url following is the configuration setting for hibernate . Hope It will help you.
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/youdb</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="emp/dto/Employee.hbm.xml"/>

How to set up default schema name in JPA configuration?

I found that in hibernate config file we could set up parameter hibernate.default_schema:
<hibernate-configuration>
<session-factory>
...
<property name="hibernate.default_schema">myschema</property>
...
</session-factory>
</hibernate-configuration>
Now I'm using JPA and I want to do the same. Otherwise I have to add parameter schema to each #Table annotation like:
#Entity
#Table (name = "projectcategory", schema = "SCHEMANAME")
public class Category implements Serializable { ... }
As I understand this parameter should be somewhere in this part of configuration:
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JiraManager"/>
<property name="dataSource" ref="domainDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false"/>
<property name="showSql" value="false"/>
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
</property>
</bean>
<bean id="domainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${datasource.url}" />
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="15"/>
<property name="checkoutTimeout" value="10000"/>
<property name="maxStatements" value="150"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="idleConnectionTestPeriod" value="50"/>
</bean>
... but I can't find its name in google. Any ideas?
Don't know of JPA property for this either. But you could just add the Hibernate property (assuming you use Hibernate as provider) as
...
<property name="hibernate.default_schema" value="myschema"/>
...
Hibernate should pick that up
Just to save time of people who come to the post (like me, who looking for Spring config type and want you schema name be set by an external source (property file)). The configuration will work for you is
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JiraManager"/>
<property name="dataSource" ref="domainDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false"/>
<property name="showSql" value="false"/>
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.default_schema">${yourSchema}</prop>
</props>
</property>
</bean>
Ps :
For the hibernate.hdm2ddl.auto, you could look in the post Hibernate hbm2ddl.auto possible values and what they do?
I have used to set create-update,because it is convenient. However, in production, I think it is better to take control of the ddl, so I take whatever ddl generate first time, save it, rather than let it automatically create and update.
For others who use spring-boot, java based configuration,
I set the schema value in application.properties
spring.jpa.properties.hibernate.dialect=...
spring.jpa.properties.hibernate.default_schema=...
In order to avoid hardcoding schema in JPA Entity Java Classes we used orm.xml mapping file in Java EE application deployed in OracleApplicationServer10 (OC4J,Orion).
It lays in model.jar/META-INF/ as well as persistence.xml. Mapping file orm.xml is referenced from peresistence.xml with tag
...
<persistence-unit name="MySchemaPU" transaction-type="JTA">
<provider>...</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
...
File orm.xml content is cited below:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>myschema</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
For those who uses last versions of spring boot will help this:
.properties:
spring.jpa.properties.hibernate.default_schema=<name of your schema>
.yml:
spring:
jpa:
properties:
hibernate:
default_schema: <name of your schema>
I had to set the value in '' and ""
spring:
jpa:
properties:
hibernate:
default_schema: '"schema"'
Use this
#Table (name = "Test", schema = "\"schema\"")
insteade of #Table (name = "Test", schema = "schema")
If you are on postgresql the request is :
SELECT * FROM "schema".test
not :
SELECT * FROM schema.test
PS: Test is a table
If you are using (org.springframework.jdbc.datasource.DriverManagerDataSource) in ApplicationContext.xml to specify Database details then use below simple property to specify the schema.
<property name="schema" value="schemaName" />

Categories

Resources