我也初学hibernate,但是我写的程序没有这个问题,up&。

解决方案 »

  1.   

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration
        PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration>
    <session-factory>
    <!-- local connection properties -->
    <property name="hibernate.connection.url">
    jdbc:mysql://192.168.1.90/db_congress?useUnicode=true&amp;characterEncoding=gbk
    </property>
    <property name="hibernate.connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password" />
    <!-- property name="hibernate.connection.pool_size"></property -->
    <!-- dialect for MySQL -->
    <property name="dialect">
    net.sf.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.use_outer_join">true</property>
    <property name="hibernate.transaction.factory_class">
    net.sf.hibernate.transaction.JDBCTransactionFactory
    </property>
    <mapping resource="com/netec/congress/model/TbSysVocation.hbm" />
    <mapping resource="com/netec/congress/model/TbDeputyBaseinfo.hbm" />
    <mapping resource="com/netec/congress/model/TbSysMeetingtype.hbm" />
    <mapping resource="com/netec/congress/model/TbSysVotezone.hbm" />
    <mapping resource="com/netec/congress/model/TbMeetingdoc.hbm" />
    <mapping resource="com/netec/congress/model/TbSysClan.hbm" />
    <mapping resource="com/netec/congress/model/TbSysDegree.hbm" />
    <mapping resource="com/netec/congress/model/TbMeeting.hbm" />
    <mapping resource="com/netec/congress/model/TbAgendaitem.hbm" />
    <mapping resource="com/netec/congress/model/TbDeputation.hbm" />
    <mapping resource="com/netec/congress/model/TbSysNation.hbm" />
    <mapping resource="com/netec/congress/model/TbSysMeetingdoctype.hbm" />
    <mapping resource="com/netec/congress/model/TbSysPeriod.hbm" />
    <mapping resource="com/netec/congress/model/TbDeputyExtendinfo.hbm" />
    <mapping resource="com/netec/congress/model/TbSysConfig.hbm" />
    <mapping resource="com/netec/congress/model/TbAuditpersonInfo.hbm" />
    <mapping resource="com/netec/congress/model/TbSysAuditsort.hbm" />
    </session-factory>
    </hibernate-configuration>
      

  2.   

    public static void initialize (String configFileName) throws HibernateException {
    if (null == configFileName && sessionFactoryMap.size() > 0) return;
    else if (null != sessionFactoryMap.get(configFileName)) return;
    else {
    Configuration cfg = new Configuration();
    if (null == configFileName)
    cfg.configure();
    else
    cfg.configure(configFileName);
    setSessionFactory(configFileName, cfg.buildSessionFactory());
    }
    } /**
     * Set the session factory
     */
    protected static void setSessionFactory (SessionFactory sessionFactory) {
    setSessionFactory((String) null, sessionFactory);
    } /**
     * Set the session factory
     */
    protected static void setSessionFactory (String configFileName, SessionFactory sessionFactory) {
    sessionFactoryMap.put(configFileName, sessionFactory);
    } /**
     * Return the SessionFactory that is to be used by these DAOs.  Change this
     * and implement your own strategy if you, for example, want to pull the SessionFactory
     * from the JNDI tree.
     */
    protected SessionFactory getSessionFactory() throws HibernateException {
    return getSessionFactory (getConfigurationFileName());
    } private static SessionFactory getSessionFactory(String configFile) throws HibernateException {
    if (sessionFactoryMap.size() == 1) return (SessionFactory) sessionFactoryMap.values().toArray()[0];
    else {
         SessionFactory sessionFactory = (SessionFactory) sessionFactoryMap.get(configFile);
         if (null == sessionFactory)
         if (null == configFile)
         throw new RuntimeException("The session factory has not been initialized.");
         else
         throw new RuntimeException("The session factory for '" + configFile + "' has not been initialized.");
         else
         return sessionFactory;
    }
    } /**
     * Return a new Session object that must be closed when the work has been completed.
     * @return the active Session
     */
    protected Session getSession() throws HibernateException {
    return createSession();
    } /**
     * Return a new Session object that must be closed when the work has been completed.
     * @return the active Session
     */
    public static Session createSession() throws HibernateException {
    return createSession(null);
    } /**
     * Return a new Session object that must be closed when the work has been completed.
     * @param configFile the config file must match the meta attribute "config-file" in the hibernate mapping file
     * @return the active Session
     */
    public static Session createSession(String configFile) throws HibernateException {
    java.util.Stack sessionStack = (java.util.Stack) threadedSessions.get();
    Session session = null;
    if (null == sessionStack) {
    sessionStack = new java.util.Stack();
    threadedSessions.set(sessionStack);
    }
    if (sessionStack.size() > 0) {
    Object[] arr = (Object[]) sessionStack.peek();
    String cf = (String) arr[0];
    if (null == cf) {
    session = (Session) arr[1];
    }
    else if (null != cf && null != configFile) {
    if (cf.equals(configFile)) session = (Session) arr[1];
    }
    if (null == session) {
    session = getSessionFactory(configFile).openSession();
    arr = new Object[2];
    arr[0] = configFile;
    arr[1] = session;
    sessionStack.push(arr);
    }
    }
    else {
    session = getSessionFactory(configFile).openSession();
    Object[] arr = new Object[2];
    arr = new Object[2];
    arr[0] = configFile;
    arr[1] = session;
    sessionStack.push(arr);
    }
    return session;
    }
      

  3.   

    我用的是hibernate3。0,数据库连接是用的jndi。服务器是webshpere
      

  4.   

    控制台信息是:
    05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  Hibernate 3.0
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  loaded properties from resource hibernate.properties: {hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=org.hibernate.dialect.OracleDialect, hibernate.show_sql=true, hibernate.connection.datasource=jdbc/OracleDs}
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  using CGLIB reflection optimizer
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  JVM does not support Statement.getGeneratedKeys()
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  using workaround for JVM bug in java.sql.Timestamp
    [05-4-12 12:03:34:703 CST] 3cc2d191 Environment   I org.hibernate.cfg.Environment  using pre JDK 1.4 java.sql.Timestamp handling
    [
      

  5.   

    我的服务器是用的jdk1。3是不是这个的问题?
      

  6.   

    应当是这个问题,
    还有你在初始化得到factory 是不是静态的?
      

  7.   

    factory = new Configuration().configure(“初始化的配置文件”).buildSessionFactory();还有,你是初始化的时候慢还是在运行的时候慢呢>??
      

  8.   

    一般在初始化的时候就是慢些,hibernate要初始化很多东西,
    如果是运行的时候慢,看年看你的session怎么了用了。是不是每次对数据库操作都要创建新的session
      

  9.   

    初始化很慢是正常现像,可以写成plug,在服务器起动时初始化,
      

  10.   

    public class IntializtionServlet extends HttpServlet {
    public void init() throws ServletException {
    try {
                               ..........................
    factory = new Configuration().configure().buildSessionFactory();
    System.out.println("数据库初始化成功");
    } catch (HibernateException e) {
    e.printStackTrace();
    System.out.println("数据库初始化失败");
    }
    }然后在web.xml中加:
    <servlet>
    <servlet-name>CongressConfigInit</servlet-name>
    <servlet-class>
    XXX.IntializtionServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
      

  11.   

    在运行的时候快吗?还有就是你的XML中声明的DTD版本和hibernate的版本匹配吗?
      

  12.   

    我用的hb3。0。但是配置
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration>
    <session-factory name="foo">
    <mapping resource="struts/advinfo/db/Advisor.hbm.xml"/>
    <mapping resource="sys/bm/Duty.hbm.xml"/>
        <mapping resource="struts/message/bo/Accessories.hbm.xml"/>
    <mapping resource="struts/message/bo/Body.hbm.xml"/>
    <mapping resource="struts/message/bo/Send.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>
    没有下到3。0的配置摸班
      

  13.   

    问题解决了。是因为2。0的dtd和3。0的包不匹配造成的。