I am trying to retrieve all my Things from the database using Hibernate, but I get this error:
Table 'thingsdb.Things' doesn't exist org.hibernate.exception.SQLGrammarException: could not execute query
The table in the database is actually named things, not Things.
The query I am executing is the following:
List<Thing> listofthings = session.createQuery(" from Thing thing").list();
And the hibernate xml:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/mythings</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<mapping class="mythings.Thing" />
</session-factory>
</hibernate-configuration>
I tried to search for a solution but I didn't find it. I am new to Hibernate so I don't really know what to look for or where.
You will need to set the name of the table, otherwise it tries to build the query with the default name which is the class name:
#Entity
#Table(name="thing")
public class Thing {
...
}
Related
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
I am just concerned, why while using Hibernate criteria from http://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm
Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.eq("salary", 2000));
List results = cr.list();
If in my database Employee class does not have salary column, or even worse - Employee is not mapped, results will be an empty list and hibernate will not throw errors.
I thought it is the problem of implicit-polymorphism, but firstly Im not extenting anything, secondly if I'll use session.delete, session.save, session.update everything goes correctly and I get all those exceptions like NoSuchFieldException, UnknownEntity and so on.
But while getting objects using criteria - just empty list.
Is it as it supposed to be? Can I somehow check if class/field I am searching for exists in DB?
All the questions about it on StackOverflow concerns the problem, when criteria returns empty list, while they have got objects in database.
I want to know why my criteria returns empty list, while it should throw error...
Mine hibernate config file:
`
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">*******</property>
<property name="hibernate.connection.username">******</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
`
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
I am using Hibernate as an ORM for a server daemon.
This code returns no items if the table associated is empty at run time but has an item added outside of hibernate.
public static List<QueueItem> queue()
{
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.flush();
session.clear();
#SuppressWarnings("unchecked")
List<QueueItem> topList = session.createQuery("FROM QueueItem i ORDER BY i.id asc").setMaxResults(3).list();
session.close();
return topList;
}
Here is the hibernate config
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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://10.10.1.4/lanjukebox_test</property>
<property name="hibernate.connection.username">ussqldev</property>
<property name="hibernate.connection.password">ussqldev</property>
<property name="hibernate.jdbc.batch_size">25</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">validate</property>
<!-- List of XML mapping files -->
<mapping resource="usagisoft/lan/jukebox/domain/entities/Song.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/Id3.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/QueueItem.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/Vote.hbm.xml"/>
<mapping resource="usagisoft/lan/jukebox/domain/entities/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If I create an object and persist it from within the application it works fine, If there is already data in the table and I insert the data it finds the other objects, the issue arises as soon as the table is empty during run time.
This is a big issue because another application handles the inserting of data for these objects.
I am using hibernate 4.2.7.Final with Java 1.7(jdk1.7.0_45) in eclipse.
You can resolve this problem since
hibernate.cfg.xml
adding this tag:
<property name="hibernate.connection.autocommit">true</property>
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