The Criteria API from Hibernate and HQL are behaving differently in respect of loading the Class Mapping. If I load the mapping from XML, both ways are able to load the Entity class. However, if I use annotations, only Criteria api is able to load the mapping. Please help me understand the reason for this.
I am using Hibernate version :
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
Hibernate Configuration
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.archive.autodetection">class, hbm</property>
<!--<mapping class="com.mk.hibernate.User" resource="User.hbm.xml"/>-->
<mapping class="com.mk.hibernate.User"/>
</session-factory>
</hibernate-configuration>
Hibernate Mapping File :
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping
http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd">
<class name="com.mk.hibernate.User" table="USERS">
<id name="username" column="USERNAME" type="java.lang.String" >
<generator class="assigned"></generator>
</id>
<property name="firstname" column="FIRSTNAME" type="java.lang.String"/>
<property name="lastname" column="LASTNAME" type="java.lang.String"/>
<property name="doj" column="DOJ" type="java.util.Date"/>
</class>
</hibernate-mapping>
I have an Entity
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
import java.util.Objects;
@Entity(name = "USERS")
public class User implements java.io.Serializable {
@Id
@Column(name = "USERNAME")
private String username;
@Column(name = "FIRSTNAME")
private String firstname;
@Column(name = "LASTNAME")
private String lastname;
@Column(name = "DOJ")
private Date doj;
...
...
I have two methods to fetch data for this entity
public List<User> getAllUsersByCriteria() {
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(User.class);
List<User> users = cr.list();
System.out.println(users.size());
session.close();
return users;
}
public List<User> getAllUsers() {
Session session = sessionFactory.openSession();
List<User> users = session.createQuery(" from User").list();
session.close();
return users;
}
When I execute following code
System.out.println("====By Criteria===");
controller.getAllUsersByCriteria().forEach(u -> {
System.out.println(u.getFirstname());
});
System.out.println("====By Query===");
controller.getAllUsers().forEach(u -> {
System.out.println(u.getFirstname());
});
I receive the following exception
====By Criteria===
Hibernate: select this_.USERNAME as USERNAME1_0_0_, this_.DOJ as DOJ2_0_0_, this_.FIRSTNAME as FIRSTNAM3_0_0_, this_.LASTNAME as LASTNAME4_0_0_ from USERS this_
2
User1
User2
====By Query===
problem creating session factory!
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [ from User]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at com.mk.hibernate.UserServiceImpl.getAllUsers(UserServiceImpl.java:33)
at com.mk.hibernate.UserControllerImpl.getAllUsers(UserControllerImpl.java:27)
at Main.main(Main.java:35)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:338)
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:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 10 more
Feb 26, 2018 5:32:27 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/demo]
I think you're misuderstanding how the name
property of the @Entity
annotation is supposed to work.
@Entity(name = "USERS")
maps the hql name of the User entity to USERS, which defaults to the classname if no value is present. So a valid hql query from that would be "from USERS"
instead of "from User"
.
You want the annotation @Table(name = "USERS")
to define the table name of the Entity.
Try changing your mapping annotations to the following:
@Entity
@Table(name = "USERS")
public class User implements java.io.Serializable {
....
}