两个类文件package com.aptech.jb.epet.entity;import java.sql.Date;/**
 * PetDiary generated by MyEclipse Persistence Tools
 */public class PetDiary implements java.io.Serializable { // Fields
         //宠物日记类
private Integer diaryId;

private PetInfo petInfo = null; private Date diaryDate; private String diaryTitle; private String diaryWeather; private String diaryContext; private String diaryIsPublic = "off"; private Date diaryLastModify; private String diaryAuthor; private String diaryAuthorEmail; // Constructors /** default constructor */
public PetDiary() {
} /** full constructor */
public PetDiary(PetInfo petInfo, Date diaryDate, String diaryTitle,
String diaryWeather, String diaryContext, String diaryIsPublic,
Date diaryLastModify, String diaryAuthor, String diaryAuthorEmail) {
this.petInfo = petInfo;
this.diaryDate = diaryDate;
this.diaryTitle = diaryTitle;
this.diaryWeather = diaryWeather;
this.diaryContext = diaryContext;
this.diaryIsPublic = diaryIsPublic;
this.diaryLastModify = diaryLastModify;
this.diaryAuthor = diaryAuthor;
this.diaryAuthorEmail = diaryAuthorEmail;
} // Property accessors public Integer getDiaryId() {
return this.diaryId;
} public void setDiaryId(Integer diaryId) {
this.diaryId = diaryId;
}// public Integer getDiaryPetId() {
// return this.diaryPetId;
// }
//
// public void setDiaryPetId(Integer diaryPetId) {
// this.diaryPetId = diaryPetId;
// } public Date getDiaryDate() {
return this.diaryDate;
} public void setDiaryDate(Date diaryDate) {
this.diaryDate = diaryDate;
} public String getDiaryTitle() {
return this.diaryTitle;
} public void setDiaryTitle(String diaryTitle) {
this.diaryTitle = diaryTitle;
} public String getDiaryWeather() {
return this.diaryWeather;
} public void setDiaryWeather(String diaryWeather) {
this.diaryWeather = diaryWeather;
} public String getDiaryContext() {
return this.diaryContext;
} public void setDiaryContext(String diaryContext) {
this.diaryContext = diaryContext;
} public String getDiaryIsPublic() {
return this.diaryIsPublic;
} public void setDiaryIsPublic(String diaryIsPublic) {
this.diaryIsPublic = diaryIsPublic;
} public Date getDiaryLastModify() {
return this.diaryLastModify;
} public void setDiaryLastModify(Date diaryLastModify) {
this.diaryLastModify = diaryLastModify;
} public String getDiaryAuthor() {
return this.diaryAuthor;
} public void setDiaryAuthor(String diaryAuthor) {
this.diaryAuthor = diaryAuthor;
} public String getDiaryAuthorEmail() {
return this.diaryAuthorEmail;
} public void setDiaryAuthorEmail(String diaryAuthorEmail) {
this.diaryAuthorEmail = diaryAuthorEmail;
} public PetInfo getPetInfo() {
return petInfo;
} public void setPetInfo(PetInfo petInfo) {
this.petInfo = petInfo;
}}
//宠物用户类
package com.aptech.jb.epet.entity;import java.io.Serializable;/**
 * PetInfo generated by MyEclipse Persistence Tools
 */public class PetInfo implements Serializable { // Fields private Integer petId; private String petName; private String petSex; private Integer petStrength; private Integer petCute; private Integer petLove; private String petIntro; private String petOwnerName; private String petOwnerEmail; private String petPassword; private String petPic; private Integer petType; // Constructors /** default constructor */
public PetInfo() {
} /** full constructor */
public PetInfo(String petName, String petSex, Integer petStrength,
Integer petCute, Integer petLove, String petIntro,
String petOwnerName, String petOwnerEmail, String petPassword,
String petPic, Integer petType) {
this.petName = petName;
this.petSex = petSex;
this.petStrength = petStrength;
this.petCute = petCute;
this.petLove = petLove;
this.petIntro = petIntro;
this.petOwnerName = petOwnerName;
this.petOwnerEmail = petOwnerEmail;
this.petPassword = petPassword;
this.petPic = petPic;
this.petType = petType;
} // Property accessors public Integer getPetId() {
return this.petId;
} public void setPetId(Integer petId) {
this.petId = petId;
} public String getPetName() {
return this.petName;
} public void setPetName(String petName) {
this.petName = petName;
} public String getPetSex() {
return this.petSex;
} public void setPetSex(String petSex) {
this.petSex = petSex;
} public Integer getPetStrength() {
return this.petStrength;
} public void setPetStrength(Integer petStrength) {
this.petStrength = petStrength;
} public Integer getPetCute() {
return this.petCute;
} public void setPetCute(Integer petCute) {
this.petCute = petCute;
} public Integer getPetLove() {
return this.petLove;
} public void setPetLove(Integer petLove) {
this.petLove = petLove;
} public String getPetIntro() {
return this.petIntro;
} public void setPetIntro(String petIntro) {
this.petIntro = petIntro;
} public String getPetOwnerName() {
return this.petOwnerName;
} public void setPetOwnerName(String petOwnerName) {
this.petOwnerName = petOwnerName;
} public String getPetOwnerEmail() {
return this.petOwnerEmail;
} public void setPetOwnerEmail(String petOwnerEmail) {
this.petOwnerEmail = petOwnerEmail;
} public String getPetPassword() {
return this.petPassword;
} public void setPetPassword(String petPassword) {
this.petPassword = petPassword;
} public String getPetPic() {
return this.petPic;
} public void setPetPic(String petPic) {
this.petPic = petPic;
} public Integer getPetType() {
return this.petType;
} public void setPetType(Integer petType) {
this.petType = petType;
}}

解决方案 »

  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="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.url">
    jdbc:sqlserver://localhost:1433;DataBaseName=epet
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">3189start</property>
    <property name="connection.driver_class">
    com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="show_sql">true</property>
    <mapping resource="com/aptech/jb/epet/entity/PetDiary.hbm.xml" />
    <mapping resource="com/aptech/jb/epet/entity/PetInfo.hbm.xml" />
    </session-factory></hibernate-configuration>
    PetDiary.hbm.xml
    <?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="com.aptech.jb.epet.entity.PetDiary" table="PetDiary"
    schema="dbo" catalog="epet">
    <id name="diaryId" type="java.lang.Integer">
    <column name="diary_id" />
    <generator class="native" />
    </id>
    <property name="diaryDate" type="java.sql.Date">
    <column name="diary_date" length="23" not-null="true" />
    </property>
    <property name="diaryTitle" type="java.lang.String">
    <column name="diary_title" length="300" not-null="true" />
    </property>
    <property name="diaryWeather" type="java.lang.String">
    <column name="diary_weather" length="30" not-null="true" />
    </property>
    <property name="diaryContext" type="java.lang.String">
    <column name="diary_context" length="7000" not-null="true" />
    </property>
    <property name="diaryIsPublic" type="java.lang.String">
    <column name="diary_is_public" length="3" />
    </property>
    <property name="diaryLastModify" type="java.sql.Date">
    <column name="diary_last_modify" length="23"
    not-null="false" />
    </property>
    <property name="diaryAuthor" type="java.lang.String">
    <column name="diary_author" length="30" not-null="true" />
    </property>
    <property name="diaryAuthorEmail" type="java.lang.String">
    <column name="diary_author_email" length="200"
    not-null="true" />
    </property>
    <many-to-one name="petInfo" column="petId"
    class="PetInfo" />
    </class>
    </hibernate-mapping>PetInfo.hbm.xml
    <?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="com.aptech.jb.epet.entity.PetInfo" table="PetInfo" schema="dbo" catalog="epet">
            <id name="petId" type="java.lang.Integer">
                <column name="pet_id" />
                <generator class="native" />
            </id>
            <property name="petName" type="java.lang.String">
                <column name="pet_name" length="50" not-null="true" />
            </property>
            <property name="petSex" type="java.lang.String">
                <column name="pet_sex" length="2" not-null="true" />
            </property>
            <property name="petStrength" type="java.lang.Integer">
                <column name="pet_strength" not-null="true" />
            </property>
            <property name="petCute" type="java.lang.Integer">
                <column name="pet_cute" not-null="true" />
            </property>
            <property name="petLove" type="java.lang.Integer">
                <column name="pet_love" not-null="true" />
            </property>
            <property name="petIntro" type="java.lang.String">
                <column name="pet_intro" length="7000" not-null="true" />
            </property>
            <property name="petOwnerName" type="java.lang.String">
                <column name="pet_owner_name" length="30" not-null="true" />
            </property>
            <property name="petOwnerEmail" type="java.lang.String">
                <column name="pet_owner_email" length="100" not-null="true" />
            </property>
            <property name="petPassword" type="java.lang.String">
                <column name="pet_password" length="30" not-null="true" />
            </property>
            <property name="petPic" type="java.lang.String">
                <column name="pet_pic" length="300" not-null="true" />
            </property>
            <property name="petType" type="java.lang.Integer">
                <column name="pet_type" not-null="true" />
            </property>
        </class>
    </hibernate-mapping>
    其中获取日记集合的方法
    public List search(PetDiary item) {
    session = getSession();
    String hql = "from PetDiary pd where pd.diary_pet_id=:petId";
    Query query = session.createQuery(hql);
    query.setInteger("petId", Integer.parseInt(item.getPetInfo().getPetId()
    .toString()));
    return query.list();
    }
    异常org.hibernate.MappingException: An association from the table PetDiary refers to an unmapped class: PetInfo%%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Could not read mappings from resource: com/aptech/jb/epet/entity/PetDiary.hbm.xmlCaused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.aptech.jb.epet.entity.PetDiary
      

  2.   

    你这种多对一的关系,在数据库中体现出来了吗? 如果数据库里面没有体现出来,那么hibernate映射的文件就是one-to-one   如果数据库中有外键,那么生成的字段中体现出one-to-many   
      

  3.   

    Hibernate 配置文件添加 实体类映射文件。看看你的还有么?
      

  4.   

    <many-to-one name="petInfo" column="petId"
                class="PetInfo" />     class="PetInfo"   PetInfo 改成路径 比如 class="com.gotop.sisp.unit.hbm.Info"  把全路径加上
      

  5.   


    <many-to-one name="petInfo" column="petId"
                class="PetInfo" />
    改<many-to-one name="petInfo" column="petId"
                class="com.aptech.jb.epet.entity.PetInfo" />