`
Odysseus_110
  • 浏览: 120622 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

fetch 属性和 property-ref属性

阅读更多

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语句的时候才会正确使用唯一键来关联而不是使用主键关联。

分享到:
评论

相关推荐

    Hibernate one to one详细配置

    这里,`name`属性指定了关联字段的名称,`class`定义了关联的实体类,`cascade`表示级联操作,`property-ref`则指定了在另一方实体上的外键属性。 **二、注解方式** 在使用注解的Hibernate项目中,一对一的配置会...

    Hibernate关联关系hbm.xml中的相关属性

    在Hibernate中,关联关系是对象关系映射(ORM)的核心组成部分,它允许我们将数据库的表结构映射到Java对象,并管理这些...在hbm.xml文件中正确地配置这些标签和属性,可以使Hibernate更高效、更灵活地管理对象关系。

    hibernate中的 一对一唯一外键双向关联

    2. **避免循环引用**:双向关联可能导致无限递归问题,因此在某些情况下可能需要使用`@ManyToOne`和`@OneToOne`注解的`fetch`属性或`@JsonIgnore`来打破循环。 3. **外键约束**:在数据库层面,需要创建外键约束来...

    Hibernate 对集合属性的操作.zip_hibernate

    - **一对一(OneToOne)**:通过`&lt;one-to-one&gt;`标签来实现,可以使用`property-ref`属性指定关联对象的主键字段。 - **一对多(OneToMany)**:使用`&lt;list&gt;`、`&lt;set&gt;`或`&lt;bag&gt;`,并通过`inverse`属性指定维护端,`...

    hibernate学习笔记

    - **property-ref**:可选属性,默认为无,表示引用的属性。 - **foreign-key**:可选属性,默认为无,表示外键约束。 - **on-delete**:可选属性,默认为 "noaction",表示删除操作时的行为。 - **not-null**:可选...

    详解Hibernate一对一映射配置

    - `property-ref`属性用于指定关联对象的属性名称。 - `access`属性定义了访问策略,可以是字段或属性访问。 - `formula`属性用于定义自定义SQL表达式来获取属性值。 - `lazy`属性用于控制关联对象的加载策略,`...

    Hibernate使用技巧汇总

    - **类名和属性名**: 必须注意大小写区分。 #### 七、实体关系管理 - **关系属性**: - `constrained`: 表示在主控表的主键上是否有外键约束。 - `property-ref`: 指定关联类中用于与主控类相关联的属性名,默认...

    Hibernate一对一主键映射

    然后,通过`&lt;one-to-one&gt;`标签定义一对一关系,`class`属性指定关联的实体类,`property-ref`属性指明共享的主键字段。 4. **实体类定义**: 在Java代码中,我们需要为两个实体类创建对应的POJO(Plain Old Java ...

    hibernate一对一关联映射

    &lt;one-to-one name="user" class="User" property-ref="account"/&gt; ``` 3. **关联映射的方向性** 一对一关联可以是单向或双向。单向关联只需在一方进行配置,而双向关联需要在双方都进行配置,并且要明确指定...

    hibrenate xml 配置 映射

    双方实体都有对方的引用,需要在两个实体的映射文件中都配置`&lt;one-to-one&gt;`标签,并设置`property-ref`属性。 #### 2.2.2 一对一主键双向关联 与一对一外键双向关联类似,但共享相同的主键。 双向关联增加了数据的...

    spring +xfire整合

    &lt;property name="serviceBean" ref="helloService"/&gt; &lt;property name="serviceName" value="{http://test/}HelloService"/&gt; &lt;property name="wsdlLocation" value="classpath:/helloService.wsdl"/&gt; ``` 5. 创建...

    ajax配置模版

    &lt;property name="dataSource"&gt;&lt;ref local="dataSource"/&gt;&lt;/property&gt; &lt;property name="mappingDirectoryLocations"&gt; &lt;value&gt;WEB-INF/classes/com/tarena/vo&lt;/value&gt; &lt;/property&gt; &lt;property name=...

    如何实现webwork+spring+hibernate框架结合

    &lt;property name="dataSource" ref="dataSource"/&gt; &lt;property name="packagesToScan" value="com.etong.bookstore.vo"/&gt; &lt;property name="hibernateProperties"&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate....

    mddr录音常见问题

    &lt;property name="dataSource" ref="ds"/&gt; &lt;property name="mappingResources"&gt; &lt;value&gt;com/threene/ssh/domain/Book.hbm.xml&lt;/value&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; ...

    java中(注解符)处理Clob(blob)类型

    &lt;property name="dataSource" ref="dataSource"/&gt; &lt;property name="hibernateProperties"&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect &lt;prop key="hibernate.show_sql"&gt;true ...

    Devart_UniDAC_7.4.12_Professional_D7-D10.3_Rio_Full_Source_Code

    Bug with fetch data when the FieldsAsString option is set to True in the Direct mode is fixed Bug with open REFCURSOR is fixed SQLServer data provider Bug with the "Cannot modify a read-only dataset" ...

    SpringMVC整合jdbc+json实例

    &lt;property name="dataSource" ref="dataSource" /&gt; ``` **3. 数据库操作** 使用JdbcTemplate或JPA进行数据库操作,例如查询、插入、更新和删除等。JdbcTemplate提供了一套简单的SQL执行方法,使得数据库操作变得...

    一个简单的spring依赖注入的例子

    &lt;property name="dao" ref="dao"/&gt; ``` 3. **使用ApplicationContext**: Spring提供`ApplicationContext`接口,它是访问Bean的主要入口点。通过加载XML配置文件,我们可以获取到Bean实例并进行操作。 ```java ...

Global site tag (gtag.js) - Google Analytics