0

我正在尝试实现一对一的关联映射,但它提供了 MappingException
Student.hbm.xml 文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
 <hibernate-mapping>
   <class name="com.sst.student.Student" table="STUDENT">
    <id name="studentId" type="java.lang.Long">
        <column name="STUDENT_ID" />
        <generator class="native" />
    </id>
    <property name="studentName" type="string" length="20" column="STUDENT_NAME"/>
   <many-to-one name="studentAddress" 
   column="STUDENT_ADDRESS"  class="com.sst.student.Address" cascade="all" 
   unique="true"/>  
    </class>

   </hibernate-mapping>

我的 Address.hbm.xml 文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Address" table="ADDRESS">
    <id name="addressId" type="java.lang.Long">
        <column name="ADDRESS_ID" />
        <generator class="native" />
    </id>
    <property name="street" type="string" column="STREET" length="20"/>
    <property name="city" type="string" column="CITY" length="20"/>
    <property name="state" type="string" column="STATE" length="20"/>
    <property name="country" type="string" column="COUNTRY" length="20"/>
</class>
</hibernate-mapping>

在代码生成时,我收到以下异常:

  org.hibernate.MappingException: An association from the table STUDENT refers to an   
  unmapped class: com.sst.student.Address

我的 cfg 文件“one2one.cfg.xml”:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root$12</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306 
    /mysql</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <mapping resource="com/sst/student/Student.hbm.xml"/>
    <mapping resource="com/sst/student/Address.hbm.xml"/>
 </session-factory>
 </hibernate-configuration>
4

1 回答 1

0

您需要在地址 hbm.xml 中进行一对多映射。

于 2012-11-16T12:57:35.903 回答