Hi
I am new to Hibernate.
I am using annotation to create mapping. I am able to create table and insert record in the Database(MySQL 5.1)but not able to retrieve records from tables. I am using Eclipse Helios Service Release 2
Please help me in finding the reason for it.
Thank you.
Here is the code and mapping file:
/* Employee.java */Code:
package com.hibernat.chapter1;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="EmployeeInfo")
public class Employee {
private int empID;
private String empName;
private String empPassword;
private String empEmailAddress;
private boolean isPermanent;
private Calendar empJoinDate;
private Date empLoginDate;
/* Not to create Column:Transient */
@Transient
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
/* create Not Null Column:nullable is false */
@Column(nullable=false)
public String getEmpEmailAddress() {
return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
@Basic
public boolean isPermanent() {
return isPermanent;
}
public void setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
}
@Temporal(TemporalType.DATE)
public Calendar getEmpJoinDate() {
return empJoinDate;
}
public void setEmpJoinDate(Calendar empJoinDate) {
this.empJoinDate = empJoinDate;
}
@Temporal(TemporalType.TIME)
public Date getEmpLoginDate() {
return empLoginDate;
}
public void setEmpLoginDate(Date empLoginDate) {
this.empLoginDate = empLoginDate;
}
@Id
@TableGenerator(name="empid",table="emppktb",pkColumnName="empkey",pkColumnValue="empvalue",allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE,generator="empid")
@Column(name="EmployeeID")
public int getEmpID() {
return empID;
}
public void setEmpID(int empID) {
this.empID = empID;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
/* TestEmpolyee.java */Code:
package com.hibernat.chapter1;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.hibernat.chapter1.Employee;
public class TestEmpolyee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Employee.class);
config.configure();
SessionFactory factory= config.buildSessionFactory();
Session session= factory.getCurrentSession();
session.beginTransaction();
Query query = (Query) session.createQuery(" Select e.empId, e.empName FROM employee e ");
List empl=query.list();
Iterator itr=empl.iterator();
while(itr.hasNext())
{
Employee emp=(Employee)itr.next();
System.out.println("Value is :"+emp.getEmpID()+" "+emp.getEmpName());
}
session.getTransaction().commit();
}
}
/* hibernate.cfg.xml */Code:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/HibernetDB</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- <property name="hibernet.default_schema">TestSchema</property> -->
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<mapping class="com.hibernat.chapter1.Employee" />
</session-factory>
</hibernate-configuration>
/* Trace */Quote:
14:25:46,812 INFO Version:15 - Hibernate Annotations 3.4.0.GA
14:25:46,828 INFO Environment:560 - Hibernate 3.3.2.GA
14:25:46,828 INFO Environment:593 - hibernate.properties not found
14:25:46,843 INFO Environment:771 - Bytecode provider name : javassist
14:25:46,843 INFO Environment:652 - using JDK 1.4 java.sql.Timestamp handling
14:25:46,906 INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
14:25:46,906 INFO Configuration:1474 - configuring from resource: /hibernate.cfg.xml
14:25:46,906 INFO Configuration:1451 - Configuration resource: /hibernate.cfg.xml
14:25:46,968 INFO Configuration:1589 - Configured SessionFactory: null
14:25:46,984 INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
14:25:47,015 INFO AnnotationBinder:419 - Binding entity from annotated class: com.hibernat.chapter1.Employee
14:25:47,062 INFO EntityBinder:422 - Bind entity com.hibernat.chapter1.Employee on table EmployeeInfo
14:25:47,109 INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
14:25:47,140 INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
14:25:47,140 INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 2
14:25:47,140 INFO DriverManagerConnectionProvider:68 - autocommit mode: false
14:25:47,156 INFO DriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/HibernetDB
14:25:47,156 INFO DriverManagerConnectionProvider:109 - connection properties: {user=root, password=****}
14:25:47,375 INFO SettingsFactory:114 - RDBMS: MySQL, version: 5.1.36-community
14:25:47,375 INFO SettingsFactory:115 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.11 ( Revision: ${svn.Revision} )
14:25:47,406 INFO Dialect:175 - Using dialect: org.hibernate.dialect.MySQL5Dialect
14:25:47,406 INFO TransactionFactoryFactory:59 - Using default transaction strategy (direct JDBC transactions)
14:25:47,406 INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
14:25:47,406 INFO SettingsFactory:161 - Automatic flush during beforeCompletion(): disabled
14:25:47,406 INFO SettingsFactory:165 - Automatic session close at end of transaction: disabled
14:25:47,406 INFO SettingsFactory:172 - JDBC batch size: 15
14:25:47,406 INFO SettingsFactory:175 - JDBC batch updates for versioned data: disabled
14:25:47,406 INFO SettingsFactory:180 - Scrollable result sets: enabled
14:25:47,406 INFO SettingsFactory:188 - JDBC3 getGeneratedKeys(): enabled
14:25:47,406 INFO SettingsFactory:196 - Connection release mode: auto
14:25:47,406 INFO SettingsFactory:220 - Maximum outer join fetch depth: 2
14:25:47,406 INFO SettingsFactory:223 - Default batch fetch size: 1
14:25:47,406 INFO SettingsFactory:227 - Generate SQL with comments: disabled
14:25:47,406 INFO SettingsFactory:231 - Order SQL updates by primary key: disabled
14:25:47,406 INFO SettingsFactory:235 - Order SQL inserts for batching: disabled
14:25:47,406 INFO SettingsFactory:397 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
14:25:47,406 INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
14:25:47,406 INFO SettingsFactory:243 - Query language substitutions: {}
14:25:47,406 INFO SettingsFactory:248 - JPA-QL strict compliance: disabled
14:25:47,406 INFO SettingsFactory:253 - Second-level cache: enabled
14:25:47,406 INFO SettingsFactory:257 - Query cache: disabled
14:25:47,406 INFO SettingsFactory:382 - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
14:25:47,406 INFO RegionFactoryCacheProviderBridge:61 - Cache provider: org.hibernate.cache.NoCacheProvider
14:25:47,406 INFO SettingsFactory:267 - Optimize cache for minimal puts: disabled
14:25:47,406 INFO SettingsFactory:276 - Structured second-level cache entries: disabled
14:25:47,421 INFO SettingsFactory:296 - Echoing all SQL to stdout
14:25:47,421 INFO SettingsFactory:305 - Statistics: disabled
14:25:47,421 INFO SettingsFactory:309 - Deleted entity synthetic identifier rollback: disabled
14:25:47,421 INFO SettingsFactory:324 - Default entity-mode: pojo
14:25:47,421 INFO SettingsFactory:328 - Named query checking : enabled
14:25:47,453 INFO SessionFactoryImpl:193 - building session factory
14:25:47,593 INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: employee is not mapped [ Select e.empId, e.empName FROM employee e ]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:94)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:316)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3228)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy12.createQuery(Unknown Source)
at com.hibernat.chapter1.TestEmpolyee.main(TestEmpolyee.java:52)