java 配置的会话工厂:null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12952243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:56:25  来源:igfitidea点击:

Configured SessionFactory: null

javahibernatehibernate-mapping

提问by Piraba

I have created basic hibernate application. It throw error message.

我已经创建了基本的休眠应用程序。它抛出错误消息。

Error is:

错误是:

     Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/crmcall/entity/CallUsers.hbm.xml
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.crmcall.entity.CallUsers -> crmcallusers
Oct 18, 2012 3:36:13 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/crmcall/entity/Customers.hbm.xml
Oct 18, 2012 3:36:14 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.crmcall.entity.Customers -> crmcustomermaster
Oct 18, 2012 3:36:14 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/crmcall/entity/User.hbm.xml
Oct 18, 2012 3:36:14 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.crmcall.entity.User -> crmusers
Oct 18, 2012 3:36:14 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Initial SessionFactory creation failed.org.hibernate.MappingException: component class not found: string
Exception in thread "main" java.lang.ExceptionInInitializerError
        at com.crmcall.util.HibernateUtil.<clinit>(HibernateUtil.java:27)
        at com.crmcall.dao.UserDAO.<init>(UserDAO.java:23)
        at com.crmcall.dao.UserDAO.main(UserDAO.java:36)
Caused by: org.hibernate.MappingException: component class not found: string
        at org.hibernate.mapping.Component.getComponentClass(Component.java:104)
        at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:133)
        at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:43)
        at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:38)
        at org.hibernate.tuple.component.ComponentEntityModeToTuplizerMapping.<init>(ComponentEntityModeToTuplizerMapping.java:52)
        at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:50)
        at org.hibernate.mapping.Component.buildType(Component.java:152)
        at org.hibernate.mapping.Component.getType(Component.java:145)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:193)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
        at com.crmcall.util.HibernateUtil.<clinit>(HibernateUtil.java:24)
        ... 2 more
Caused by: java.lang.ClassNotFoundException: string
        at java.net.URLClassLoader.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
        at org.hibernate.mapping.Component.getComponentClass(Component.java:101)

This is my Hibernate.cfg.xml file:

这是我的 Hibernate.cfg.xml 文件:

     <hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://192.168.1.5:3306/crmtest</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <!-- Enable Hibernate 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>

    <mapping resource="com/crmcall/entity/CallUsers.hbm.xml"/>
    <mapping resource="com/crmcall/entity/Customers.hbm.xml"/>
    <mapping resource="com/crmcall/entity/User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

This is my User.hbm.xml

这是我的 User.hbm.xml

    <hibernate-mapping>
  <class name="com.crmcall.entity.User" table="crmusers">
      <composite-id name="userPK" >
           <key-property name="businessUnit" column="BusinessUnit" type="string"/>
           <key-property name="userID" column="UserID" type="string"/>
      </composite-id>

        <property name="recID" >
          <column name="RecID"/>
        </property>

        <property name="password">
          <column name="Password"/>
        </property>

        <property name="userName">
          <column name="UserName"/>
        </property>

        <property name="userType">
          <column name="UserType"/>
        </property>

        <property name="userLevel">
          <column name="UserLevel"/>
        </property>

        <property name="customerCode">
          <column name="CustomerCode"/>
        </property>

        <property name="customerCodeson">
          <column name="CustomerCodeson"/>
        </property>

        <property name="locationCode">
          <column name="LocationCode"/>
        </property>

        <property name="lastUpdatedBy">
          <column name="LastUpdatedBy"/>
        </property>

        <property name="lastUpdatedOn" type="timestamp">
          <column name="LastUpdatedOn"/>
        </property>

      <property name="email" type="string">
          <column name="Email"/>
        </property>

  </class>

</hibernate-mapping>

This is my calling place :

这是我打电话的地方:

     public class UserDAO {
    private Session session = null;

    public UserDAO() {
        session =  HibernateUtil.currentSession();
    }

     public List<User> getAllUsers() {
        Transaction tn = session.beginTransaction();
        List<User> users = session.createQuery("from crmusers cu order by cu.UserID").list();
        System.out.println("==" + users.size());
        tn.commit();
        return users;
    }


     public static void main(String[] args){
         UserDAO userDAO = new UserDAO();
         userDAO.getAllUsers();
     }
}

This is my project folder structre: enter image description here

这是我的项目文件夹结构: 在此处输入图片说明

Please tell me what is an issue in my code?

请告诉我我的代码有什么问题?

Thanks in advance..

提前致谢..

回答by Johanna

In User.hbm.xml you have to use type="java.lang.String"(with a big 'S'). That's it.

在 User.hbm.xml 中,您必须使用type="java.lang.String"(带有一个大的“S”)。而已。

回答by dimas

AS I understand the problem in your code was caused by your User.hbm.xmlmapping file. To be more precised by type="string"attribute of composite-idtab.

据我了解,您的代码中的问题是由您的User.hbm.xml映射文件引起的。通过复合 ID选项卡的type="string"属性更精确。

As I understand you don't need to put type attribute obligatory so try to skip it at all; hibernate should detect it automatically. I'm not sure in last sentece because I have not used composite keys, but there are a lot of examples where peoples don't define type explicitly.

据我了解,您不需要强制输入 type 属性,因此请尝试跳过它;休眠应该自动检测它。我不确定最后一句,因为我没有使用复合键,但是有很多例子人们没有明确定义类型。

回答by Anders R. Bystrup

Hmmm, in your mapping file you have

嗯,在你的映射文件中你有

<key-property name="businessUnit" column="BusinessUnit" type="string"/>
<key-property name="userID" column="UserID" type="string"/>

That should be ...type="java.lang.String"...instead, but most likely you really don't need it - Hibernate will make a (usually very good) educated guess.

那应该是...type="java.lang.String"...,但很可能你真的不需要它 - Hibernate 会做出(通常非常好的)有根据的猜测。

Cheers,

干杯,

回答by subodh

Hibernate use the reflectionto determine the mapping type at runtime. It seems your mapping is correct, But if you still getting the same problem. I will suggestyou to remove the type attributefor the time being test, from all the Stringtype properties such as email,businessUnit,userId

Hibernate 使用reflection来确定映射类型runtime。看起来你的映射是正确的,但如果你仍然遇到同样的问题。我会建议type attribute暂时从所有String类型属性(例如电子邮件、业务单位、用户 ID)中删除测试

try this way for all the Stringtype properties

对所有String类型属性尝试这种方式

       <property name="email">
          <column name="Email"/>
        </property>

or you can try with java.lang.Stringalso

或者你可以尝试java.lang.String

       <property name="email" type="java.lang.String">
          <column name="Email"/>
        </property>

回答by Steve Ebersole

Both "string" and "java.lang.String" are aliases for org.hibernate.type.StringType, so either should work in terms of naming types.

"string" 和 "java.lang.String" 都是 org.hibernate.type.StringType 的别名,所以两者都应该在命名类型方面起作用。

I am actually not so sure the problem is in the mapping for User. That mapping looks fine. Based on that exception I would more expect that somewhere you have

我实际上不太确定问题出在用户的映射中。那个映射看起来不错。基于那个例外,我更希望你在某个地方

<composite-id ... class="string">

or

或者

<component ... class="string">

or something like that.

或类似的东西。

回答by mrArbi

<composite-id name="userPK" >
       <key-property name="businessUnit" column="BusinessUnit" type="String"/>
       <key-property name="userID" column="UserID" type="String"/>
  </composite-id>

try with changing string to String

尝试将字符串更改为字符串