java pojo类和hibernate映射文件分别为:
/**
* SysRepeaterMng entity.
*
* @author MyEclipse Persistence Tools
*/
public class SysRepeaterMng implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = -7028035445611074921L;
private Long id;
private String reptSid;
private String reptNam;
private String reptIp;
private Date joinTime;
private String memo;
private Long state;
private String password;
private Long isserver;
private Long publishmode;
//private String stateCN;//服务器状态 中文名称
//private String isserverCN;//服务器类型 中文名称
//private String publishmodeCN;//服务器发布方式 中文名称
private Set itemPublishFails;
// Constructors
/**
* @return the itemPublishFails
*/
public Set getItemPublishFails() {
return itemPublishFails;
}
/**
* @param itemPublishFails the itemPublishFails to set
*/
public void setItemPublishFails(Set itemPublishFails) {
this.itemPublishFails = itemPublishFails;
}
/** default constructor */
public SysRepeaterMng() {
}
/** minimal constructor */
public SysRepeaterMng(Long id, String reptSid, String reptNam,
String reptIp, Date joinTime, Long state,Set itemPublishFails) {
this.id = id;
this.reptSid = reptSid;
this.reptNam = reptNam;
this.reptIp = reptIp;
this.joinTime = joinTime;
this.state = state;
this.itemPublishFails = itemPublishFails;
}
/** full constructor */
public SysRepeaterMng(Long id, String reptSid, String reptNam,
String reptIp, Date joinTime, String memo, Long state,
String password,Set itemPublishFails) {
this.id = id;
this.reptSid = reptSid;
this.reptNam = reptNam;
this.reptIp = reptIp;
this.joinTime = joinTime;
this.memo = memo;
this.state = state;
this.password = password;
this.itemPublishFails = itemPublishFails;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getReptSid() {
return this.reptSid;
}
public void setReptSid(String reptSid) {
this.reptSid = reptSid;
}
public String getReptNam() {
return this.reptNam;
}
public void setReptNam(String reptNam) {
this.reptNam = reptNam;
}
public String getReptIp() {
return this.reptIp;
}
public void setReptIp(String reptIp) {
this.reptIp = reptIp;
}
public Date getJoinTime() {
return this.joinTime;
}
public void setJoinTime(Date joinTime) {
this.joinTime = joinTime;
}
public String getMemo() {
return this.memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public Long getState() {
return this.state;
}
public void setState(Long state) {
this.state = state;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* @return the isserver
*/
public Long getIsserver() {
return isserver;
}
/**
* @param isserver
* the isserver to set
*/
public void setIsserver(Long isserver) {
this.isserver = isserver;
}
/**
* @return the publishmode
*/
public Long getPublishmode() {
return publishmode;
}
/**
* @param publishmode
* the publishmode to set
*/
public void setPublishmode(Long publishmode) {
this.publishmode = publishmode;
}
}
<?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.SysRepeaterMng" table="SYS_REPEATER_MNG" schema="ZHENGJIANG"> <id name="id" type="java.lang.Long"> <column name="ID" precision="10" scale="0" /> <generator class="sequence"> <param name="sequence">SYSREPEATERMNG_sequence</param> </generator> </id> <property name="reptSid" type="java.lang.String"> <column name="REPT_SID" length="10" not-null="true" unique="true" /> </property> <property name="reptNam" type="java.lang.String"> <column name="REPT_NAM" length="50" not-null="true" /> </property> <property name="reptIp" type="java.lang.String"> <column name="REPT_IP" length="50" not-null="true" /> </property> <property name="joinTime" type="java.util.Date"> <column name="JOIN_TIME" length="7" not-null="true" /> </property> <property name="memo" type="java.lang.String"> <column name="MEMO" length="256" /> </property> <property name="state" type="java.lang.Long"> <column name="STATE" precision="1" scale="0" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="20" /> </property> <property name="isserver" type="java.lang.Long"> <column name="IS_SERVER" precision="1" scale="0" not-null="true" /> </property> <property name="publishmode" type="java.lang.Long"> <column name="PUBLISH_MODE" precision="1" scale="0" not-null="true" /> </property> <set name="itemPublishFails" table="ITEM_PUBLISH_FAIL" inverse="true" lazy="false" cascade="all"> <key column="REPT_SID"></key> <one-to-many class="com.ItemPublishFail"/> </set> </class> </hibernate-mapping>
/**
* ItemPublishFail entity.
*
* @author MyEclipse Persistence Tools
*/
public class ItemPublishFail implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = -3002520851890289401L;
private Long itemId;
private String channelNam;
private Integer channelTpid;
private String mediaType;
private Integer codeRate;
private Integer publishMode;
private Integer pubPrv;
private Date failTime;
private String channelUrl;
private String channelPic;
private String channelDesc;
private String keyWord;
private SysRepeaterMng sysRepeaterMng;
// Constructors
/** default constructor */
public ItemPublishFail() {
}
/** full constructor */
public ItemPublishFail(String channelNam, Integer channelTpid,
String mediaType, Integer codeRate, Integer publishMode, Integer pubPrv,
Date failTime, String channelUrl, String channelPic,
String channelDesc, String keyWord, SysRepeaterMng sysRepeaterMng) {
this.channelNam = channelNam;
this.channelTpid = channelTpid;
this.mediaType = mediaType;
this.codeRate = codeRate;
this.publishMode = publishMode;
this.pubPrv = pubPrv;
this.failTime = failTime;
this.channelUrl = channelUrl;
this.channelPic = channelPic;
this.channelDesc = channelDesc;
this.keyWord = keyWord;
this.sysRepeaterMng = sysRepeaterMng;
}
// Property accessors
public Long getItemId() {
return this.itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
public String getChannelNam() {
return this.channelNam;
}
public void setChannelNam(String channelNam) {
this.channelNam = channelNam;
}
public Integer getChannelTpid() {
return this.channelTpid;
}
public void setChannelTpid(Integer channelTpid) {
this.channelTpid = channelTpid;
}
public String getMediaType() {
return this.mediaType;
}
public void setMediaType(String mediaType) {
this.mediaType = mediaType;
}
public Integer getCodeRate() {
return this.codeRate;
}
public void setCodeRate(Integer codeRate) {
this.codeRate = codeRate;
}
public Integer getPublishMode() {
return this.publishMode;
}
public void setPublishMode(Integer publishMode) {
this.publishMode = publishMode;
}
public Integer getPubPrv() {
return this.pubPrv;
}
public void setPubPrv(Integer pubPrv) {
this.pubPrv = pubPrv;
}
public Date getFailTime() {
return this.failTime;
}
public void setFailTime(Date failTime) {
this.failTime = failTime;
}
public String getChannelUrl() {
return this.channelUrl;
}
public void setChannelUrl(String channelUrl) {
this.channelUrl = channelUrl;
}
public String getChannelPic() {
return this.channelPic;
}
public void setChannelPic(String channelPic) {
this.channelPic = channelPic;
}
public String getChannelDesc() {
return this.channelDesc;
}
public void setChannelDesc(String channelDesc) {
this.channelDesc = channelDesc;
}
public String getKeyWord() {
return this.keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
/**
* @return the sysRepeaterMng
*/
public SysRepeaterMng getSysRepeaterMng() {
return sysRepeaterMng;
}
/**
* @param sysRepeaterMng the sysRepeaterMng to set
*/
public void setSysRepeaterMng(SysRepeaterMng sysRepeaterMng) {
this.sysRepeaterMng = sysRepeaterMng;
}
}
<?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.ItemPublishFail" table="ITEM_PUBLISH_FAIL" schema="ZHENGJIANG" > <id name="itemId" type="java.lang.Long"> <column name="ITEM_ID" precision="6" scale="0" /> <generator class="sequence"> <param name="sequence">ITEMPUBLISHFAIL_SEQUENCE</param> </generator> </id> <property name="channelNam" type="java.lang.String"> <column name="CHANNEL_NAM" length="100" not-null="true" /> </property> <property name="channelTpid" type="java.lang.Integer"> <column name="CHANNEL_TPID" precision="3" scale="0" not-null="true" /> </property> <property name="mediaType" type="java.lang.String"> <column name="MEDIA_TYPE" length="10" not-null="true" /> </property> <property name="codeRate" type="java.lang.Integer"> <column name="CODE_RATE" precision="10" scale="0" not-null="true" /> </property> <property name="publishMode" type="java.lang.Integer"> <column name="PUBLISH_MODE" precision="1" scale="0" not-null="true" /> </property> <property name="pubPrv" type="java.lang.Integer"> <column name="PUB_PRV" precision="1" scale="0" not-null="true" /> </property> <property name="failTime" type="java.util.Date"> <column name="FAIL_TIME" length="7" not-null="true" /> </property> <property name="channelUrl" type="java.lang.String"> <column name="CHANNEL_URL" not-null="true" /> </property> <property name="channelPic" type="java.lang.String"> <column name="CHANNEL_PIC" not-null="true" /> </property> <property name="channelDesc" type="java.lang.String"> <column name="CHANNEL_DESC" length="256" /> </property> <property name="keyWord" type="java.lang.String"> <column name="KEY_WORD" length="30" /> </property> <many-to-one name="sysRepeaterMng" class="com.SysRepeaterMng" property-ref="reptSid" column="REPT_SID" cascade="none" outer-join="auto" access="property" not-null="true" lazy="false" > </many-to-one> </class> </hibernate-mapping>
问题一:属性fetch="join|select" 配置
hibernate 官方网站对 many-to-one 属性 fetch="join|select" 的解释:
fetch (optional - defaults to select): Chooses between outer-join fetching or sequential select fetching.
如果该属性设置成join 时,hibernate会使用 sql 中的 outer-join 来关联两张表取数据,如果设置成select时,则
hibernate会分别用两条select语句来取数据。
当使用Criteria 的 criteria.list();来取数据时,如果设置成select时,则hibernate会先将SysRepeaterMng 填充进
ItemPublishFail 中 再把数据填充到list 中,正确显示。
如果设置成join时则hibernate会分别将SysRepeaterMng 和 ItemPublishFail 对象都放入list中,造成错误,具体原因还不清楚。
问题二:属性property-ref配置(该属性适用于遗留的数据库,既与一方关联的不是主键,而是唯一键)
hibernate 数据库多对一关联时,如果一的那一方关联的字段不是表的主键,而是唯一键的话。则需要加另外的属性property-ref配置。
查看hibernate官方文档:
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html#mapping-declaration-manytoone
many-to-one 属性
property-ref: (optional) The name of a property of the associated class that is joined to
this foreign key. If not specified, the primary key of the associated class is used.
如果没有设置该属性,则hibernate会默认使用一方的主键,所以需要将该属性设置成一方的唯一键,这样hibernate在生成sql语句的时候才会正确使用唯一键来关联而不是使用主键关联。