1

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

错误是:

     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$1.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)

这是我的 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>

这是我的 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>

这是我打电话的地方:

     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();
     }
}

这是我的项目文件夹结构: 在此处输入图像描述

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

提前致谢..

4

6 回答 6

2

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

于 2012-10-18T10:18:51.663 回答
1

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

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

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

干杯,

于 2012-10-18T10:22:04.700 回答
1

“string”和“java.lang.String”都是 org.hibernate.type.StringType 的别名,因此它们都应该在命名类型方面起作用。

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

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

或者

<component ... class="string">

或类似的东西。

于 2012-10-18T13:18:08.040 回答
1

Hibernate 使用reflection来确定映射类型runtime。看来您的映射是正确的,但是如果您仍然遇到同样的问题。我建议type attribute暂时从所有String类型属性(例如 email、businessUnit、userId)中删除

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

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

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

       <property name="email" type="java.lang.String">
          <column name="Email"/>
        </property>
于 2012-10-18T11:11:31.017 回答
1
<composite-id name="userPK" >
       <key-property name="businessUnit" column="BusinessUnit" type="String"/>
       <key-property name="userID" column="UserID" type="String"/>
  </composite-id>

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

于 2014-04-02T15:03:49.890 回答
1

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

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.

于 2012-10-18T10:20:01.643 回答