我用hibernate写了两个映射文件: 
UserServices.hbm.xml文件在com.tianlw.oss.user.pojo包下 
Pricing.hbm.xml文件在com.tianlw.oss.pricing.pojo包下 以下是两个映射文件 
<?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 package="com.tianlw.oss.user.pojo"> 
  <class name="UserServices" table="userservices"> 
    <id name="id" > 
        <generator class="native"> </generator> 
    </id> 
    <property name="loginname" not-null="true"> </property> 
    <property name="loginpassword" not-null="true" > </property> 
    <property name="labip" not-null="true" > </property>    
    <property name="status" not-null="true"> </property> 
    <property name="enrolldate" type="date" not-null="true"> </property> 
<property name="closedate" type="date"> </property> 
<many-to-one name="user" class="Users" column="userid"> </many-to-one> 
    <one-to-one name="pricing" class="com.tianlw.oss.pricing.pojo.Pricing" cascade="all" /> 
  </class> 
</hibernate-mapping> 
<?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 package="com.tianlw.oss.pricing.pojo"> 
  <class name="Pricing" table="pricings"> 
    <id name="id" > 
        <generator class="native"> </generator> 
    </id> 
    <property name="name" not-null="true"  unique="true"> </property> 
    <property name="basefee" not-null="true" > </property> 
    <property name="ratefee" not-null="true" > </property> 
    <property name="description"  > </property> 
    <many-to-one name="userService"  class="com.tianlw.oss.user.pojo.UserServices" column="priceid" unique="true" lazy="false"> </many-to-one> 
  </class> 
</hibernate-mapping> 我用junit测试报以下异常: 
org.springframework.dao.InvalidDataAccessResourceUsageException: could not load an entity: [com.tianlw.oss.user.pojo.UserServices#1]; nested exception is org.hibernate.exception.SQLGrammarException: could not load an entity: [com.tianlw.oss.user.pojo.UserServices#1] 
Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.tianlw.oss.user.pojo.UserServices#1] 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:1865) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42) 
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3038) 
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395) 
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375) 
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139) 
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195) 
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103) 
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808) 
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:471) 
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372) 
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:465) 
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:459) 
at com.tianlw.oss.user.dao.BusinessDAOImpl.findById(BusinessDAOImpl.java:41) 
at com.tianlw.oss.test.user.BusinessDAOTest.testFindById(BusinessDAOTest.java:41) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at junit.framework.TestCase.runTest(TestCase.java:154) 
at junit.framework.TestCase.runBare(TestCase.java:127) 
at junit.framework.TestResult$1.protect(TestResult.java:106) 
at junit.framework.TestResult.runProtected(TestResult.java:124) 
at junit.framework.TestResult.run(TestResult.java:109) 
at junit.framework.TestCase.run(TestCase.java:118) 
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) 
Caused by: java.sql.SQLException: Unknown column 'pricing1_.priceid' in 'field list' 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622) 
at com.mysql.jdbc.Connection.execSQL(Connection.java:2376) 
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1860) 
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1705) 
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93) 
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186) 
at org.hibernate.loader.Loader.getResultSet(Loader.java:1778) 
at org.hibernate.loader.Loader.doQuery(Loader.java:662) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:1851) 
... 33 more 
请问,是什么原因,该如何改正?谢谢大家

解决方案 »

  1.   

    Pricing对UserServices有many-to-one,UserServices里应该有one-to-many吧
      

  2.   

    <set name=""> 
    <key column="">//指定外键
    <one-to-one name="pricing" class="com.tianlw.oss.pricing.pojo.Pricing" cascade="all" />
    </set>
    你这么试试
      

  3.   

    你的两张表是什么关系呢,如果是多对多的话就建个中间表,形成两个多对一
    如果是多对一的话你的配置应该有个
    set name=""> 
    <key column="">//指定外键 
    <one-to-many name="pricing" class="com.tianlw.oss.pricing.pojo.Pricing" cascade="all" /> 
    </set> 
    另有一个<many-to-one name="userService"  class="com.tianlw.oss.user.pojo.UserServices" column="priceid" unique="true" lazy="false"> </many-to-one>