在实现单向多对一级联时,发生如下异常,哪位老师指导一下....
1.配置文件:hibernate.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"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration><session-factory>
<property name="myeclipse.connection.profile">
y2-hibernate-chap6
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=zf
</property>
<property name="connection.username">sa</property>
<property name="connection.password">0000</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">true</property>
<mapping resource="y2/hib/entity/TblJd.hbm.xml" />
<mapping resource="y2/hib/entity/TblQx.hbm.xml" /></session-factory></hibernate-configuration>
2.两个实体类及其映射文件:
TblJd.java:package y2.hib.entity;/**
 * TblJd entity.
 * 
 * @author MyEclipse Persistence Tools
 */public class TblJd implements java.io.Serializable { // Fields private Integer jdid;
private String jdName;
private TblQx qx; // Constructors /** default constructor */
public TblJd() {
} /** full constructor */
public TblJd(Integer jdid, String jd, TblQx qx) {
super();
this.jdid = jdid;
this.jdName = jd;
this.qx = qx;
} // Property accessors public Integer getJdid() {
return this.jdid;
} public void setJdid(Integer jdid) {
this.jdid = jdid;
} public String getJdName() {
return jdName;
} public void setJdName(String jdName) {
this.jdName = jdName;
} public TblQx getQx() {
return qx;
} public void setQx(TblQx qx) {
this.qx = qx;
}}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="y2.hib.entity.TblJd" table="TBL_JD" schema="dbo"
catalog="zf">
<id name="jdid" type="java.lang.Integer">
<column name="jdid" />
<generator class="native" />
</id>
<property name="jdName" type="java.lang.String">
<column name="jd" length="50" />
</property>
<many-to-one name="qx" class="TblQx" column="qxid"></many-to-one>
</class>
</hibernate-mapping>
TblQx .java:
package y2.hib.entity;/**
 * TblQx entity.
 * 
 * @author MyEclipse Persistence Tools
 */public class TblQx implements java.io.Serializable { // Fields private Integer qxid;
private String qxName; // Constructors /** default constructor */
public TblQx() {
} /** full constructor */
public TblQx(String qx) {
this.qxName = qx;
} // Property accessors public Integer getQxid() {
return this.qxid;
} public void setQxid(Integer qxid) {
this.qxid = qxid;
} public String getQxName() {
return qxName;
} public void setQxName(String qxName) {
this.qxName = qxName;
}}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="y2.hib.entity.TblQx" table="TBL_QX" schema="dbo" catalog="zf">
        <id name="qxid" type="java.lang.Integer">
            <column name="qxid" />
            <generator class="native" />
        </id>
        <property name="qxName" type="java.lang.String">
            <column name="qx" length="50" />
        </property>
    </class>
</hibernate-mapping>
3.测试代码:
public static void main(String[] args) {
test1 t = new test1();
TblQx qx = (TblQx) t.getById(TblQx.class, 1);
System.out.println(qx.getQxName());
TblJd jd = (TblJd) t.getById(TblJd.class, 1);
System.out.println(jd.getQx().getQxName());
}

解决方案 »

  1.   

    异常信息:
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: An association from the table TBL_JD refers to an unmapped class: TblQx
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1134)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)
    at y2.hib.impl.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
    at y2.hib.impl.BaseHibernateDAO.getSession(BaseHibernateDAO.java:14)
    at y2.hib.impl.BaseHibernateDAO.getById(BaseHibernateDAO.java:18)
    at y2.hib.test.test1.getById(test1.java:24)
    at y2.hib.test.test1.main(test1.java:35)
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Could not read mappings from resource: y2/hib/entity/TblJd.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
    at y2.hib.impl.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
    at y2.hib.impl.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
    at y2.hib.impl.BaseHibernateDAO.getSession(BaseHibernateDAO.java:14)
    at y2.hib.impl.BaseHibernateDAO.getById(BaseHibernateDAO.java:18)
    at y2.hib.test.test1.getById(test1.java:24)
    at y2.hib.test.test1.main(test1.java:35)
    Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping y2.hib.entity.TblJd
    at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:154)
    at org.hibernate.cfg.Configuration.add(Configuration.java:386)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
    ... 11 more
    Exception in thread "main" java.lang.NullPointerException
    at y2.hib.impl.BaseHibernateDAO.getById(BaseHibernateDAO.java:19)
    at y2.hib.test.test1.getById(test1.java:24)
    at y2.hib.test.test1.main(test1.java:35)
      

  2.   

    TblQx qx = (TblQx) t.getById(TblQx.class, 1); 
    改成TblQx qx = (TblQx) t.getById(TblQx.class, new Integer(1));
    试试,,,因为你的 配制文件中是<id name="qxid" type="java.lang.Integer">
    是Integer的,当你传个int进去时它就会报错
      

  3.   

    TblQx qx = (TblQx) t.getById(TblQx.class, new Integer(1)); 
      

  4.   

    谢谢了,不过我搞定了:映射文件的class=""要写全路径.!!<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
    <class name="y2.hib.entity.TblQx" table="TBL_QX" schema="dbo"
    catalog="zf">
    <id name="qxid" type="java.lang.Integer">
    <column name="qxid" />
    <generator class="native" />
    </id>
    <property name="qxName" type="java.lang.String">
    <column name="qx" length="50" />
    </property>
    <set name="jdSet" inverse="true" cascade="all">
    <key column="qxid"></key>
    <one-to-many class="y2.hib.entity.TblJd" />
    </set>
    </class>[code=XML]
    </hibernate-mapping>[/code]<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
    <class name="y2.hib.entity.TblJd" table="TBL_JD" schema="dbo"
    catalog="zf">
    <id name="jdid" type="java.lang.Integer">
    <column name="jdid" />
    <generator class="native" />
    </id>
    <property name="jdName" type="java.lang.String">
    <column name="jd" length="50" />
    </property>
    <many-to-one name="qxObj" class="y2.hib.entity.TblQx" column="qxid"></many-to-one>
    </class>
    </hibernate-mapping>