无法创建sessionfactory:annotationConfiguration实例是必需的

时间:2014-06-22 12:37:00

标签: hibernate exception gwt rpc sessionfactory

我无法让我的hibernate函数(例如getusers())在RPC实现中工作(例如在login()中)。

我把所有的hibernate函数放到服务器上的dataServiceImpl类中,但是当我有一个自己的类函数并运行它作为普通的java应用程序时,它工作正常,所以我不知道为什么我不能使用相同的在我的rpc funkctions中。

在我的RPC登录功能中,我尝试使用以下测试:

List<User> userlist = getUsers();
        Window.alert(userlist.toString());

这里的getusers功能:

public List<User> getUsers(){
        try{
             Configuration configuration = new Configuration();
             configuration.configure("/swmhibernate.cfg.xml");
             ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
             factory = configuration.buildSessionFactory(serviceRegistry); }
            //factory = new Configuration().configure("swmhibernate.cfg.xml").buildSessionFactory();}
         catch (Throwable ex) { 
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex); 

         }

         Session session= factory.openSession();
         Transaction tx = null;
         List <User> users=new ArrayList<User>();
         try{

             tx = session.beginTransaction();
            users=session.createCriteria(User.class).list();
             Query q= session.createQuery("select u from User u");
             List<User> u1=q.list();

             System.out.println("data loaded here");
             for(User u2:u1){
                 if(u2.getCustomerList().size()>1)
                 {System.out.println(u2.getUserid()+ "  ADDED USER");
                 users.add(u2);
                 }
             }
         }catch (RuntimeException e) {
              if (tx != null && tx.isActive()) {
                    try {
            // Second try catch as the rollback could fail as well
                      tx.rollback();
                    } catch (HibernateException e1) {
                      e1.printStackTrace();
                    }
            // throw again the first exception
                    throw e;
                  }
                }finally{session.close();}
         return users;
     }

swmhibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <!-- connecting to the database --> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">url</property> 
    <property name="connection.username">user</property>
    <property name="connection.password">pw</property>  


    <property name="connection.pool_size">1</property>

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

     <property name="hibernate.connection.autocommit">false</property>
     <property name="show_sql">true</property>
     <property name="format_sql">true</property>
     <property name="use_sql_comments">true</property>
     <!-- for jdbc transaction -->
     <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

     <!-- mapping file -->
     <mapping class="de.team2.kraftwerk.hibernate.Address"/>
     <mapping class="de.team2.kraftwerk.hibernate.Plant"/>
     <mapping class="de.team2.kraftwerk.hibernate.Plantprocessinfo"/>
     <mapping class="de.team2.kraftwerk.hibernate.User"/>
     <mapping class="de.team2.kraftwerk.hibernate.Directmarketing"/>
     <mapping class="de.team2.kraftwerk.hibernate.Account"/>
     <mapping class="de.team2.kraftwerk.hibernate.Customer"/>
     <mapping class="de.team2.kraftwerk.hibernate.Report"/>
     <mapping class="de.team2.kraftwerk.hibernate.Mrl"/>
     <mapping class="de.team2.kraftwerk.hibernate.Srl"/>
     <mapping class="de.team2.kraftwerk.hibernate.MrlmarketingInfo"/>
     <mapping class="de.team2.kraftwerk.hibernate.Srl_outputpreis"/>
      <mapping class="de.team2.kraftwerk.hibernate.Srl_workprice"/>
     <mapping class="de.team2.kraftwerk.hibernate.InvoiceMailingaddress"/>

    </session-factory>
</hibernate-configuration>

我正在使用以下.jar文件:

antlr-2.7.7.jar
datanucleus-api-jdo-3.1.3.jar
datanucleus-api-jpa-3.1.3.jar
datanucleus-appengine.2.1.2.jar
datanucleus-core-3.1.3.jar
dom4j-1.6.1.jar
geronimo-jpa_2.0_spec-1.0.jar
gwt-servlet.jar
hibernate-commons-annotations-4.0.4.Final.jar
hibernate-core-4.3.5.Final.jar
hibernate-entitiymanager-4.3.5.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jdo-api-3.0.1.jar
jsr107cache-1.1.jar
jta-1.1.jar
mysql-connector-java-5.1.30-bin.jar

我的gwt应用程序抛出以下错误:

[WARN] Server class 'org.eclipse.jetty.servlet.listener.ELContextCleaner' could not be found in the web app, but was found on the system classpath
   [WARN] Adding classpath entry 'file:/C:/Program%20Files/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.6.0/gwt-2.6.0/gwt-dev.jar' to the web app classpath for this session
   For additional info see: file:/C:/Program%20Files/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.6.0/gwt-2.6.0/doc/helpInfo/webAppClassPath.html
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Environment <clinit>
Information: Hibernate 3.0.5
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Environment <clinit>
Information: hibernate.properties not found
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Environment <clinit>
Information: using CGLIB reflection optimizer
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Environment <clinit>
Information: using JDK 1.4 java.sql.Timestamp handling
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Configuration configure
Information: configuring from resource: /swmhibernate.cfg.xml
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Configuration configure
Information: configuring from resource: /swmhibernate.cfg.xml
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Configuration getConfigurationInputStream
Information: Configuration resource: /swmhibernate.cfg.xml
Jun 22, 2014 1:54:05 PM org.hibernate.cfg.Configuration getConfigurationInputStream
Information: Configuration resource: /swmhibernate.cfg.xml
Failed to create sessionFactory object.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping clazz="de.team2.kraftwerk.hibernate.Address"/>Starting Jetty on port 8888
   [WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract de.team2.kraftwerk.client.UserDTO de.team2.kraftwerk.client.DataService.loginServer(java.lang.String,java.lang.String)' threw an unexpected exception: java.lang.ExceptionInInitializerError

    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:265)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:305)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:68)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
    at de.team2.kraftwerk.server.DataServiceImpl.getUsers(DataServiceImpl.java:239)
    at de.team2.kraftwerk.server.DataServiceImpl.loginServer(DataServiceImpl.java:42)
    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 com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
    ... 30 more
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping clazz="de.team2.kraftwerk.hibernate.Address"/>
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1273)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
    at de.team2.kraftwerk.server.DataServiceImpl.getUsers(DataServiceImpl.java:233)
    ... 36 more
[ERROR] 500 - POST /kraftwerk/data (127.0.0.1) 57 bytes
   Request headers
      Host: 127.0.0.1:8888
      User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
      Accept-Encoding: gzip, deflate
      X-GWT-Permutation: HostedMode
      X-GWT-Module-Base: http://127.0.0.1:8888/kraftwerk/
      Content-Type: text/x-gwt-rpc; charset=utf-8
      Referer: http://127.0.0.1:8888/Kraftwerk.html?gwt.codesvr=127.0.0.1:9997
      Content-Length: 173
      Connection: keep-alive
      Pragma: no-cache
      Cache-Control: no-cache
   Response headers
      Content-Type: text/plain

我希望有人可以提供帮助, 谢谢realizze

编辑我删除了一些.jar文件,现在出现了同样的错误,但其原因是:

Caused by: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/Session;
    at de.team2.kraftwerk.hibernate.Funktionen.getUsers(Funktionen.java:42)
    at de.team2.kraftwerk.server.DataServiceImpl.loginServer(DataServiceImpl.java:31)
    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 com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
    ... 30 more

1 个答案:

答案 0 :(得分:0)

现在我解决了这个问题。如果你使用hibernate版本4.1.0而不是hibernate4.3.5就没有问题了。