Tuesday, March 17, 2015

Some Exceptions coming while Create Maven project with hibernate

Below i am describing some of the exceptions and error which might come while you are trying to build project with hibernate annotation.
In maven i have used below dependency


org.hibernate hibernate-core 4.3.8.Final org.hibernate hibernate-annotations 3.5.6-Final org.hibernate hibernate-commons-annotations 3.3.0.ga junit junit 4.8.1

we have created dto class UserDetails.java (dto.user.UserDetails)

below is the hibernate.cfg.xml configuration file.

false com.mysql.jdbc.Driver root123 jdbc:mysql://localhost:3306/test1 root org.hibernate.dialect.MySQLDialect true create



If you can see above, we have include "com.common.Stock" for mapping and we have not defined this class in the project. So this will lead to below error. Before that below is our test code

------ public class hibernate { public static void main(String[] args) { UserDetails user = new UserDetails(); user.setUserId(1); user.setUserName("First User"); // create session factory - one object- get session from SessionFactory // need configuration to build sessionFactory // new Configuration().configure() // u need to have hibernate.cfg.xml in default place resources SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); session.close(); } }

-------------------------------

Exception in thread "main" org.hibernate.MappingException: Unable to load class [ com.common.Stock] declared in Hibernate configuration entry at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2281) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2229) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2209) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2162) at org.hibernate.cfg.Configuration.configure(Configuration.java:2077) at org.hibernate.cfg.Configuration.configure(Configuration.java:2056) at hibernate.main(hibernate.java:17) Caused by: java.lang.ClassNotFoundException: com.common.Stock at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:193) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2278) ... 6 more --------------------------------------

As you have not defined you should either remove from hibernate configuration class or comment that. Best is remove.



--- 2. Error number 2 : missing of driver class for Mysql If you can look at the above mentioned dependencies, I have not included a driver for Mysql. This will give below error. Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:243) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) at hibernate.main(hibernate.java:18) Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:228) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:240) ... 15 more -------------

Solution 2: Include dependency for driver:Mysql

mysql mysql-connector-java 5.1.9



-------------- It will work fine and you will have below out in the eclipse. Note : we have used "create"\ create where this will lead to drop exist table and create every time runs. Table will have same name as DTO. ie: table name=Userdetails fields=userId,Username and userId will be primary ----------------------------------



Mar 17, 2015 1:38:07 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} Mar 17, 2015 1:38:07 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.8.Final} Mar 17, 2015 1:38:07 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Mar 17, 2015 1:38:07 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Mar 17, 2015 1:38:07 PM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml Mar 17, 2015 1:38:07 PM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml Mar 17, 2015 1:38:07 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! Mar 17, 2015 1:38:07 PM org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null Mar 17, 2015 1:38:08 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) Mar 17, 2015 1:38:08 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test1] Mar 17, 2015 1:38:08 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000046: Connection properties: {user=root, password=****} Mar 17, 2015 1:38:08 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false Mar 17, 2015 1:38:08 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 (min=1) Mar 17, 2015 1:38:13 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Mar 17, 2015 1:38:13 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 Mar 17, 2015 1:38:14 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) Mar 17, 2015 1:38:14 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory INFO: HHH000397: Using ASTQueryTranslatorFactory Mar 17, 2015 1:38:17 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000227: Running hbm2ddl schema export Hibernate: drop table if exists UserDetails Hibernate: create table UserDetails (userId integer not null, userName varchar(255), primary key (userId)) Mar 17, 2015 1:38:19 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Hibernate: insert into UserDetails (userName, userId) values (?, ?)

No comments :