java - Hibernate 3 - MappingException - AnnotationConfiguration instance is required to use <mapping class="xxxxclass"/> -
i tried hand @ writing simple hibernate 3 java program - fetch data db, works fine when try configure hbm.xml
file when use model class configure same, face issues below. ie
<mapping class="example.pojo_stock"/>
instead of
<mapping resource="pojo_stock.hbm.xml" />
although tried related queries on stackoverflow (many marked duplicate) unable find solution. kindly help
public class mainapp { private static sessionfactory factory; public static void main(string[] args) { try{ system.out.println("in main method of mainapp..."); configuration configure=new configuration(); configure.configure("hibernate.cfg.xml"); factory = configure.buildsessionfactory(); mainapp mainapp = new mainapp(); mainapp.listdetails_stock(); } catch(throwable ex){ system.err.println("failed create sessionfactory object." + ex); throw new exceptionininitializererror(ex); } } public void listdetails_stock(){ system.out.println("in listdetails_stock method of mainapp..."); session session = factory.opensession(); transaction tx = null; try{ tx = session.begintransaction(); list<pojo_stock> details = session.createquery("from pojo_stock").list(); for(pojo_stock detail : details){ system.out.print("\nstockid: " + detail.getstockid()); system.out.print("\nstockcode: " + detail.getstockcode()); system.out.print("\nstockname: " + detail.getstockname()); } tx.commit(); } catch (hibernateexception e) { if (tx!=null) tx.rollback(); e.printstacktrace(); }finally { session.close(); } }
the model class looks like
@entity @table(name = "stock") public class pojo_stock { int stockid; string stockcode; string stockname; // getters & setters }
and hibernate config looks below,
<property name="hibernate.dialect"> org.hibernate.dialect.oracledialect </property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.oracledriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@xxxx </property> <property name="hibernate.connection.username"> xxxx </property> <property name="hibernate.connection.password"> xxxx </property> <property name="show_sql">true</property> <!-- list of xml mapping files --> <!-- <mapping resource="pojo_stock.hbm.xml" /> --> <mapping class="example.pojo_stock"/> </session-factory>
the error on console below,
failed create sessionfactory object.org.hibernate.mappingexception: annotationconfiguration instance required use <mapping class="example.pojo_stock"/> exception in thread "main" java.lang.exceptionininitializererror @ example.mainapp.main(mainapp.java:28) caused by: org.hibernate.mappingexception: annotationconfiguration instance required use <mapping class="example.pojo_stock"/> @ org.hibernate.cfg.configuration.parsemappingelement(configuration.java:1524) @ org.hibernate.cfg.configuration.parsesessionfactory(configuration.java:1479) @ org.hibernate.cfg.configuration.doconfigure(configuration.java:1458) @ org.hibernate.cfg.configuration.doconfigure(configuration.java:1432) @ org.hibernate.cfg.configuration.configure(configuration.java:1352) @ tutorialspoint.example.mainapp.main(mainapp.java:19)
i tried use annotationconfiguration()
instead of configuration()
not work instead throws below error,
exception in thread "main" java.lang.incompatibleclasschangeerror: org.hibernate.cfg.mappings @ java.lang.classloader.defineclass(classloader.java:262) @ java.security.secureclassloader.defineclass(secureclassloader.java:69) @ java.net.urlclassloader.defineclass(urlclassloader.java:540) @ java.net.urlclassloader.defineclass(urlclassloader.java:451) @ java.net.urlclassloader.access$300(urlclassloader.java:79) @ java.net.urlclassloader$classfinder.run(urlclassloader.java:1038) @ java.security.accesscontroller.doprivileged(accesscontroller.java:314) @ java.net.urlclassloader.findclass(urlclassloader.java:429) @ java.lang.classloader.loadclasshelper(classloader.java:665) @ java.lang.classloader.loadclass(classloader.java:644) @ sun.misc.launcher$appclassloader.loadclass(launcher.java:358) @ java.lang.classloader.loadclass(classloader.java:627) @ java.lang.j9vminternals.verifyimpl(native method) @ java.lang.j9vminternals.verify(j9vminternals.java:85) @ java.lang.j9vminternals.initialize(j9vminternals.java:162) @ postgres.hibernate.lessons.mainapp.main(mainapp.java:28)
i think there issue .jar
files. not using latest .jars
. trying annotational configuration in pojo
.
@entity @table(name = "stock") public class pojo_stock { int stockid; string stockcode; string stockname; // getters & setters }
so, first please use annotationconfiguration()
instead of configuration()
; have tried this.
so, think (possibly) using incompatible versions of hibernate core , annotations. 1 of possible combination hibernate core 3.3.2 hibernate annotations 3.4 , try. have @ compatibility matrix here more details.
or
you may try link hibernate-error-an-annotationconfiguration-instance-is-required
Comments
Post a Comment