Hibernate - org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped - java

I'm new to hibernate and I'm trying to solve some problems i cannot understand too well...
For example, I have this query
Query query = session.createQuery("from Client where clientId = "+clientId+")");
List result = query.list();
My Client.hbm.xml is
<class name="it.besmart.models.Client" table="client" catalog="SMARTPARK">
<id name="idClient" type="int">
<column name="id_client" />
<generator class="identity" />
</id>
<property name="nomeClient" type="string">
<column name="nome_client" length="65535" not-null="true" />
</property>
<property name="numPosti" type="int">
<column name="num_posti" not-null="true" />
</property>
<property name="numLuci" type="int">
<column name="num_luci" not-null="true" />
</property>
<property name="inizioPosti" type="int">
<column name="inizio_posti" not-null="true" />
</property>
<property name="inizioLuci" type="int">
<column name="inizio_luci" not-null="true" />
</property>
</class>
</hibernate-mapping>
The table on the DB is called client and the column is client_id
The mapping in hibernate.cfg.xml is
<hibernate-configuration>
<session-factory name="parkserver">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">passwprd</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.3.67:3306/SMARTPARK</property>
<property name="hibernate.connection.username">parkuser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="it.besmart.models.Client" resource="Client.hbm.xml"/>
<mapping class="it.besmart.models.Illuminazione" resource="Illuminazione.hbm.xml"/>
<mapping class="it.besmart.models.MappaPosti" resource="MappaPosti.hbm.xml"/>
<mapping class="it.besmart.models.Occupazione" resource="Occupazione.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I'm having no problems in another query in the application, but when i try to executer the SELECT query i got this exception
org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped [from Client where clientId = 1)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at it.besmart.parkserver.SocketClientHandler.run(SocketClientHandler.java:83)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:76)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
I don't know why Client is not mapped, it's correctly in the cfg file...
Thanks in advance

Try to add the full name of the class in the query:
Query query = session.createQuery("from it.besmart.models.Client where clientId = :c");
query.setString("c", clientId)
List result = query.list();
I also changed to late binding here which is preferable but has nothing to do with your problem.
As Maurice Perry pointed out in comment below:
- You can also specify the default package in the mapping file: <hibernate-mapping package="it.besmart.models">

Related

Hibernate SQL Error in generated query (Potentially a mapping mistake)

I'm making this simple app using Hibernate to do some CRUD operations. I'm having this error in the query generated by hibernate
Config XML:
<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://localhost:3306/library</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_schema">library</property>
<property name="hbm2ddl.auto" value="create"/>
<!-- Mappings -->
<mapping resource="librarysystem/mappings/Role.hbm.xml"/>
<mapping resource="librarysystem/mappings/Librarian.hbm.xml"/>
<mapping resource="librarysystem/mappings/Task.hbm.xml"/>
<mapping resource="librarysystem/mappings/Library.hbm.xml"/>
</session-factory>
</hibernate-configuration>
POJO:
public class Library implements java.io.Serializable {
private int id;
private String name;
private Set librarians = new HashSet(0);
//Constructors, getters and setters...
}
Mapping:
<hibernate-mapping>
<class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="45" />
</property>
<set name="librarians" table="librarian" inverse="true" lazy="true" fetch="select">
<key>
<column name="libraryid" not-null="true" />
</key>
<one-to-many class="librarysystem.entities.Librarian" />
</set>
</class>
</hibernate-mapping>
Code to get the list:
List<Library> libraries = session.createQuery("FROM Library").list();
When I run a query to get the list of libraries, an exception occurs saying that the query syntax is wrong
the log output of the query is:
select
library0_.id as id1_2_,
library0_.name as name2_2_
from
library.library.library library0_
How did the library.library.library happened?
Any help please?
I've given the minimum details as I thought was necessary. If you need more code to find the error (like other POJOs and mappings), please inform me
How did the library.library.library happened? Any help please?
I'm no expert here but I think the tripple library here comes from the table being named library within a Database (catalog) named library.
<class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">
The table name and the catalog name are the same. So we are looking for a catalog called library library within which we want a table called library library.library. Although I'm not 100% on the third library, I think I'm in the right direction.
On a side note, I find it easier to work with hibernate annotation instead of xml mapping.
Difference between annotation and xml mapping in Hibernate
XML mapping example
Annotation example

to generate a table with list using hbm file

I am new to Hibernate and trying to make an insertion of ArrayList<String> in DB using .hbm mapping file.
I want no annotations to use.
I made a search on how to insert ArrayList<String> and found working code from here.
I was expecting that a new table containing entries of ArrayList<String> will be created on insert of POJO named ClassTime
here is ClassTime.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 20, 2015 2:47:36 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.img.yogesh.Gym.ClassTime" table="CLASS_TIME">
<id name="idclass_time" type="java.lang.Integer">
<column name="IDCLASS_TIME" />
<generator class="increment" />
</id>
<property name="class_name" type="java.lang.String">
<column name="CLASS_NAME" />
</property>
<property name="repeat_boolean" type="java.lang.String">
<column name="REPEAT_BOOLEAN" />
</property>
<property name="repeat_weeks" type="java.lang.String">
<column name="REPEAT_WEEKS" />
</property>
<property name="instructor_name" type="java.lang.String">
<column name="INSTRUCTOR_NAME" />
</property>
<property name="start_on_date" type="java.util.Date">
<column name="START_ON_DATE" />
</property>
<property name="start_time_date" type="java.util.Date">
<column name="START_TIME_DATE" />
</property>
<property name="end_on_date" type="java.util.Date">
<column name="END_ON_DATE" />
</property>
<property name="end_time_date" type="java.util.Date">
<column name="END_TIME_DATE" />
</property>
<list name="repeat_days_list" table="repeat_days_list" >
<key >
<column name="repeat_id" />
</key>
<list-index column="day"></list-index>
<element type="java.lang.String">
<column name="REPEAT_DAYS_LIST" />
</element>
</list>
<property name="repeat_days_list_string" type="java.lang.String">
<column name="REPEAT_DAYS_LIST_STRING" />
</property>
</class>
</hibernate-mapping>
and java code to save data into database
session.persist(addClassTime);
transaction.commit();
and my stack trace while running the code is
Caused by: java.sql.BatchUpdateException: Table 'gym.repeat_days_list' doesn't exist
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 40 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gym.repeat_days_list' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1976)
... 43 more
here is the hibernate.cfg.xml :
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/gym</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="question.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hoping to get an answer soon.
We can do this also with adding
<property name="hbm2ddl.auto">update</property>
in .cfg.xml file.
You need to tell Hibernate to create the table if it does not already exist. So add this line directly underneath <hibernate-mapping> in your ClassTime.hbm.xml:
<property name="hbm2ddl.auto" value="create"/>

Empty collection after hibernate select

I want find all records in table 'users' using hibernate but its allways return empty collection (im sure that some users are in db). My searching function looks like this:
public List<Users> findAll() {
DetachedCriteria criteria = DetachedCriteria.forClass(Users.class);
return getHibernateTemplate().findByCriteria(criteria);
}
Why is not working?
hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/intellidom</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<mapping file="mapping/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Users.hbm.xml:
<hibernate-mapping>
<class name="intellidom.entity.Users" table="users" schema="intellidom">
<id column="id" name="id" type="int" >
<generator class="increment"/>
</id>
<property name="login" column="login" type="java.lang.String"/>
<property name="password" column="password" type="java.lang.String"/>
<property name="firstName" column="firstName" type="java.lang.String"/>
<property name="lastName" column="lastName" type="java.lang.String"/>
</class>
</hibernate-mapping>

Why doesn't hibernate load persisted entity from second level cache?

Does hibernate put into second level cache entity which I persist with save method?
My code is :
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person("Volodia", "Levytskyi", "Mykolaiv");
session.save(person);
transaction.commit();
session.close();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Person load = (Person) session.load(Person.class, (long) 1);
System.out.println("load2=" + load);
transaction.commit();
session.close();
I expect hibernate to load from second level cache person but it issues select query to database when
session.load(Person.class, (long) 1);
is running.
My hibernate.cfg.xml:
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateSimpleDB</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="transaction.factory_class">org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.generate_statistics">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/masterhibernate/SimpleHibernateDemo/Person.hbm.xml"
/>
<!-- <mapping class="com.masterhibernate.SimpleHibernateDemo.Person" /> -->
</session-factory>
</hibernate-configuration>
My Person.hbm.xml :
<hibernate-mapping>
<class name="com.masterhibernate.SimpleHibernateDemo.Person"
table="Person">
<cache usage="read-write" />
<id name="id" column="PersonId">
<generator class="native" />
</id>
<property name="name">
<column name="name" length="16" not-null="true" />
</property>
<property name="surname">
<column name="surname" length="36"></column>
</property>
<property name="address">
<column name="address" length="22"></column>
</property>
</class>
</hibernate-mapping>
Why is this?
It seems like you have your database set up to generate primary keys for you. This means that, when you save the entity, it does not yet have an ID, as it will later be assigned by the database. This also means that hibernate cannot store it in the second-level cache, as it doesn't have a key to index it with yet. It should be stored in the second-level cache after retrieving it again from the database though.

Table missing error while maaping MySQL view to hibernate

I am trying to map a MySql view to hibernate. But when I am getting and Table missing error while starting the application.
view is
CREATE
ALGORITHM = UNDEFINED
DEFINER = `mydbadmin`#`%`
SQL SECURITY DEFINER
VIEW `Reservation_Transaction_view` AS
select
convert( concat(`apst`.`Tid`,
_utf8'-',
`apst`.`RoomIndex`,
_utf8'-',
`apst`.`Rid`) using utf8) AS `id`,
`apst`.`Tid` AS `transactionId`,
`apst`.`RoomIndex` AS `room no`,
`apst`.`Pid` AS `paymentId`,
`apst`.`Rid` AS `reservationId`
from
`agent_payment_sub_transaction` `apst`
where
(`apst`.`Rid` <> 0)
group by `apst`.`Tid` , `apst`.`RoomIndex`
The mapping
<?xml version="1.0"?>
<hibernate-mapping>
<class name="com.abc.def.entity.ReservationTransactionView"
table="Reservation_Transaction_view" catalog="abcd">
<id name="id" type="string" />
<property name="transactionId" type="java.lang.Integer">
<column name="transactionId" />
</property>
<property name="roomNo" type="java.lang.Integer">
<column name="`room no`" />
</property>
<property name="paymentId" type="java.lang.Integer">
<column name="paymentId" />
</property>
<property name="reservationId" type="java.lang.Integer">
<column name="reservationId" />
</property>
The error is as followed.
org.hibernate.HibernateException: Missing table: Reservation_Transaction_view
I am using MySQL and Hibernate3.0 Please Help me for this query.
Thanks in advance.

Categories

Resources