GWT Hibernate:数据库连接错误&例外

时间:2011-12-24 19:55:53

标签: hibernate gwt rpc

我正在开发一个简单的网络应用程序来存储&在eclipse环境中使用带有Hibernate的GWT RPC从数据库中检索数据。我面临的问题是当我在映射文件中使用时,它工作正常。但是当我使用时,它开始引发数据库连接错误。除连接错误外,它还会引发一些例外情况。

这是堆栈跟踪:

137 [btpool0-0] INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
241 [btpool0-0] INFO org.hibernate.cfg.Environment - hibernate.properties not found
363 [btpool0-0] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
434 [btpool0-0] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
702 [btpool0-0] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
702 [btpool0-0] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
3508 [btpool0-0] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : user.hbm.xml
4658 [btpool0-0] INFO org.hibernate.cfg.HbmBinder - Mapping class: rpctest.shared.User -> users
4820 [btpool0-0] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
5133 [btpool0-0] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
5133 [btpool0-0] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 1
5133 [btpool0-0] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
5331 [btpool0-0] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/userdata
5331 [btpool0-0] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
7405 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.5.16
7405 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq )
7467 [btpool0-0] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
7546 [btpool0-0] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
7586 [btpool0-0] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
7586 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
7587 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
7587 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
7587 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
7590 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
7590 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
7590 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
7592 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
7637 [btpool0-0] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
7638 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
7638 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
7638 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
7638 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
7687 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
7687 [btpool0-0] INFO org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge - Cache provider: org.hibernate.cache.NoCacheProvider
7687 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
7687 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
7758 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
7773 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
7773 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
7773 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
7773 [btpool0-0] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
9024 [btpool0-0] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
Initial SessionFactory creation failed.org.hibernate.MappingException: could not instantiate id generator [entity-name=rpctest.shared.User]
Starting Jetty on port 8888
   [WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract rpctest.shared.User[] rpctest.client.RpctestService.getUser()' threw an unexpected exception: java.lang.ExceptionInInitializerError
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.lang.ExceptionInInitializerError
    at rpctest.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
    at rpctest.server.HibernateUtil.<clinit>(HibernateUtil.java:7)
    at rpctest.server.RpctestServiceImpl.getUser(RpctestServiceImpl.java:54)
    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:569)
    ... 22 more
Caused by: org.hibernate.MappingException: could not instantiate id generator [entity-name=rpctest.shared.User]
    at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:132)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:224)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at rpctest.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:11)
    ... 29 more
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: assign
    at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:151)
    at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:124)
    ... 33 more
[ERROR] 500 - POST /rpctest/testService (127.0.0.1) 57 bytes
   Request headers
      Accept: */*
      Accept-Language: en-gb
      User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
      Accept-Encoding: gzip, deflate
      Host: 127.0.0.1:8888
      Connection: Keep-Alive
      Referer: http://127.0.0.1:8888/Rpctest.html?gwt.codesvr=127.0.0.1:9997
      X-GWT-Permutation: HostedMode
      X-GWT-Module-Base: http://127.0.0.1:8888/rpctest/
      Content-Type: text/x-gwt-rpc; charset=utf-8
      Content-Length: 118
      Cache-Control: no-cache
   Response headers
      Content-Type: text/plain

这是 Hibernate POJO文件:

package rpctest.shared;

import java.io.Serializable;

public class User implements Serializable {
 private Integer id;
 private String firstName;
 private String lastName;

 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
}

映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="rpctest.shared.User" table="users" >
  <id name="id" type="int" column="id" >
   <generator class="assign"/>
  </id>

  <property name="firstName">
   <column name="first_name" />
  </property>
  <property name="lastName">
   <column name="last_name"/>
  </property>
 </class>
</hibernate-mapping>

cfg文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/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/userdata</property>
  <property name="connection.username">root</property>
  <property name="connection.password"></property>

  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">1</property>

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

  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>

  <!-- Disable the second-level cache -->
  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>

  <!-- Drop and re-create the database schema on startup -->
  <property name="hbm2ddl.auto">update</property>

  <!-- Mapping files -->
  <mapping resource="user.hbm.xml"/>


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

GWT服务IMPL文件:

package rpctest.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
//import com.hib.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;
import rpctest.shared.User;
import rpctest.client.RpctestService;

public class RpctestServiceImpl extends RemoteServiceServlet  implements RpctestService {

        public String addUser(String id, String name1, String name2)
            throws IllegalArgumentException {

              int userID = Integer.parseInt(id);        
              Transaction trns = null;
              Session session = HibernateUtil.getSessionFactory().openSession();
              try {
               trns = session.beginTransaction();

               User user = new User();
               user.setId(userID); 
               user.setFirstName(name1);
               user.setLastName(name2);

               session.save(user);

               session.getTransaction().commit();
              } catch (RuntimeException e) {
               if(trns != null){
                trns.rollback();
               }
               e.printStackTrace();
              } finally{
               session.flush();
               session.close();
              }

        return name1+name2; // to test flextable entris only 
    }

    @Override
    public  User[] getUser()
             {

              List<User> getUser = null;

              Transaction trns = null;
              Session session = HibernateUtil.getSessionFactory().openSession();
              try {
               trns = session.beginTransaction();
               getUser = session.createQuery("from User").list();
               trns.commit();
              } catch (RuntimeException e) {
               if(trns != null){
                trns.rollback();
               }
               e.printStackTrace();
              } finally{
               session.flush();
               session.close();
             }
              return getUser.toArray(new User[getUser.size()]);
        }
}

1 个答案:

答案 0 :(得分:0)

<generator class="assign"/>需要像

一样
<generator class="assigned"/>