如何在一对多映射中获取单行

时间:2014-02-19 06:48:41

标签: hibernate

public class ProfilePic  implements java.io.Serializable {
 private String picId;
 private String profilePicPath;
 private String smallPicPath;
 private String adddate;
 private Integer aid;
 //getter and setter

}

public class Atom implements java.io.Serializable {

private Integer id;
private String name;
//getter and setter
}

Atom.hbm.xml

<hibernate-mapping>
    <class name="test.Atom" table="atom" catalog="xrcwrn_sms">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity"></generator>
        </id>
        <property name="name" type="string">
            <column name="name" length="200" unique="true" />
        </property>
      </class>
</hibernate-mapping>

ProfilePic.hbm.xml

<hibernate-mapping>
    <class name="test.ProfilePic" table="profile_pic" catalog="xrcwrn_sms">
        <id name="picId" type="string">
            <column name="pic_id" length="200" />
            <generator class="assigned" />
        </id>
        <property name="profilePicPath" type="string">
            <column name="profile_pic_path" length="1000" />
        </property>
        <property name="smallPicPath" type="string">
            <column name="small_pic_path" length="1000" />
        </property>
        <property name="adddate" type="string">
            <column name="adddate" length="100" />
        </property>
        <property name="aid" type="java.lang.Integer">
            <column name="aid" />
        </property>
    </class>
</hibernate-mapping>

我想从ProfilePic只获取一行。 AtomProfilePic具有一对多映射(一个原子可以有多个profilePic)

如何映射。

1 个答案:

答案 0 :(得分:0)

您尚未在hbm.xml个文件中定义关系。请进行以下更改

Atom.hbm.xml

<hibernate-mapping>
 <class name="test.Atom" table="atom" catalog="xrcwrn_sms">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity"></generator>
    </id>
    <property name="name" type="string">
        <column name="name" length="200" unique="true" />
    </property>
    <set name="profilePics" table="profile_pic" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="aid" not-null="true" />
        </key>
        <one-to-many class="test.ProfilePic" />
    </set>
  </class>
</hibernate-mapping>

ProfilePic.hbm.xml

<hibernate-mapping>
 <class name="test.ProfilePic" table="profile_pic" catalog="xrcwrn_sms">
    <id name="picId" type="string">
        <column name="pic_id" length="200" />
        <generator class="assigned" />
    </id>
    <many-to-one name="atom" class="test.Atom" fetch="select">
       <column name="aid" />
    </many-to-one>
    <property name="profilePicPath" type="string">
        <column name="profile_pic_path" length="1000" />
    </property>
    <property name="smallPicPath" type="string">
        <column name="small_pic_path" length="1000" />
    </property>
    <property name="adddate" type="string">
        <column name="adddate" length="100" />
    </property>        
 </class>
</hibernate-mapping>

还需要更改.java个文件

Atom.java

public class Atom implements java.io.Serializable {
  private Integer id;
  private String name;
  private Set profilePics = new HashSet(0);
 //constructor, getter and setter
}

ProfilePic.java

public class ProfilePic  implements java.io.Serializable {
  private String picId;
  private String profilePicPath;
  private String smallPicPath;
  private String adddate;
  private Atom atom;
 //constructor, getter and setter
}

相关链接


您需要分享有关您的问题fetch one row from ProfilePic的更多信息。对我来说不是很清楚。