`

Hibernate映射无主键表的问题

阅读更多
在ms sql server 数据库中,有一个表Authors(注:此表没有主键列),用MyEclipse的hibernate生成的映射中出现四个文件,AbstractAuthors.java,Authors.java,AuthorsId.java,Authors.hbm.xml


表中有一个列是AuthName,但这列不是主键,因为AuthName可以重复,没有自增列.

现在的问题是我应该怎么做才能把数据库的数据取出来?


回复:Struts+Hibernate映射无主键表的问题

只要你不根据主键id取单条记录
其他都是一样的
取出符合条件的list即可

回复:Struts+Hibernate映射无主键表的问题

但取出 list 之后也要类型转换啊!因为他生成了两个java文件,我不知道怎么处理了!

请指教~~~~

回复:Struts+Hibernate映射无主键表的问题

AbstractAuthors.java 
AuthorsId.java
不管它,你取出list集合用Authors.java转换
如果model里有AuthorsId的话就把它去掉.xml文件里也一样,保留里边的属性就可以了.

不过很奇怪,为什么你要这么做,没有主键的表,至少也要是个复合主键啊

回复:Struts+Hibernate映射无主键表的问题

这是映射文件

xml 代码
  1. <!---->xml version="1.0"?>  
  2. <!---->
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!---->  
  5. <hibernate-mapping>  
  6.     <class name="com.lichen.infocenter.hibernate.mapping.Wzcxtemp" table="WZCXTEMP" schema="EDDIE">  
  7.         <composite-id name="id" class="com.lichen.infocenter.hibernate.mapping.WzcxtempId">  
  8.             <key-property name="ch" type="java.lang.String">  
  9.                 <column name="CH" length="20" />  
  10.             key-property>  
  11.             <key-property name="cx" type="java.lang.String">  
  12.                 <column name="CX" length="16" />  
  13.             key-property>  
  14.             <key-property name="cz" type="java.lang.String">  
  15.                 <column name="CZ" length="80" />  
  16.             key-property>  
  17.             <key-property name="wzdate" type="java.lang.String">  
  18.                 <column name="WZDATE" length="20" />  
  19.             key-property>  
  20.             <key-property name="wzdd" type="java.lang.String">  
  21.                 <column name="WZDD" length="80" />  
  22.             key-property>  
  23.             <key-property name="wzxw" type="java.lang.String">  
  24.                 <column name="WZXW" length="80" />  
  25.             key-property>  
  26.             <key-property name="wzsxh" type="java.lang.String">  
  27.                 <column name="WZSXH" length="20" />  
  28.             key-property>  
  29.             <key-property name="cldw" type="java.lang.String">  
  30.                 <column name="CLDW" length="80" />  
  31.             key-property>  
  32.             <key-property name="pzdm" type="java.lang.String">  
  33.                 <column name="PZDM" length="40" />  
  34.             key-property>  
  35.             <key-property name="wzcldz" type="java.lang.String">  
  36.                 <column name="WZCLDZ" length="80" />  
  37.             key-property>  
  38.             <key-property name="lxdh" type="java.lang.String">  
  39.                 <column name="LXDH" length="20" />  
  40.             key-property>  
  41.             <key-property name="flag" type="java.lang.String">  
  42.                 <column name="FLAG" length="2" />  
  43.             key-property>  
  44.         composite-id>  
  45.     class>  
  46. hibernate-mapping>  



回复:Struts+Hibernate映射无主键表的问题

这是 Wzcxtemp 类::

package com.lichen.infocenter.hibernate.mapping;
// Generated by MyEclipse - Hibernate Tools



/**
 * Wzcxtemp generated by MyEclipse - Hibernate Tools
 */
public class Wzcxtemp extends AbstractWzcxtemp implements java.io.Serializable {

    // Constructors

    /** default constructor */
    public Wzcxtemp() {
    }

    
    /** full constructor */
    public Wzcxtemp(WzcxtempId id) {
        super(id);        
    }
   
}

这是 WzcxtempId 类::
package com.lichen.infocenter.hibernate.mapping;

/**
 * WzcxtempId generated by MyEclipse - Hibernate Tools
 */

public class WzcxtempId  implements java.io.Serializable {


    // Fields    

     private String ch;
     private String cx;
     private String cz;
     private String wzdate;
     private String wzdd;
     private String wzxw;
     private String wzsxh;
     private String cldw;
     private String pzdm;
     private String wzcldz;
     private String lxdh;
     private String flag;


    // Constructors

    /** default constructor */
    public WzcxtempId() {
    }

    

   
    // Property accessors

    public String getCh() {
        return this.ch;
    }
    
    public void setCh(String ch) {
        this.ch = ch;
    }

    public String getCx() {
        return this.cx;
    }
    
    public void setCx(String cx) {
        this.cx = cx;
    }

    public String getCz() {
        return this.cz;
    }
    
    public void setCz(String cz) {
        this.cz = cz;
    }

    public String getWzdate() {
        return this.wzdate;
    }
    
    public void setWzdate(String wzdate) {
        this.wzdate = wzdate;
    }

    public String getWzdd() {
        return this.wzdd;
    }
    
    public void setWzdd(String wzdd) {
        this.wzdd = wzdd;
    }

    public String getWzxw() {
        return this.wzxw;
    }
    
    public void setWzxw(String wzxw) {
        this.wzxw = wzxw;
    }

    public String getWzsxh() {
        return this.wzsxh;
    }
    
    public void setWzsxh(String wzsxh) {
        this.wzsxh = wzsxh;
    }

    public String getCldw() {
        return this.cldw;
    }
    
    public void setCldw(String cldw) {
        this.cldw = cldw;
    }

    public String getPzdm() {
        return this.pzdm;
    }
    
    public void setPzdm(String pzdm) {
        this.pzdm = pzdm;
    }

    public String getWzcldz() {
        return this.wzcldz;
    }
    
    public void setWzcldz(String wzcldz) {
        this.wzcldz = wzcldz;
    }

    public String getLxdh() {
        return this.lxdh;
    }
    
    public void setLxdh(String lxdh) {
        this.lxdh = lxdh;
    }

    public String getFlag() {
        return this.flag;
    }
    
    public void setFlag(String flag) {
        this.flag = flag;
    }
   



   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
 if ( (other == null ) ) return false;
 if ( !(other instanceof WzcxtempId) ) return false;
 WzcxtempId castOther = ( WzcxtempId ) other; 
         
 return ( (this.getCh()==castOther.getCh()) || ( this.getCh()!=null && castOther.getCh()!=null && this.getCh().equals(castOther.getCh()) ) )
 && ( (this.getCx()==castOther.getCx()) || ( this.getCx()!=null && castOther.getCx()!=null && this.getCx().equals(castOther.getCx()) ) )
 && ( (this.getCz()==castOther.getCz()) || ( this.getCz()!=null && castOther.getCz()!=null && this.getCz().equals(castOther.getCz()) ) )
 && ( (this.getWzdate()==castOther.getWzdate()) || ( this.getWzdate()!=null && castOther.getWzdate()!=null && this.getWzdate().equals(castOther.getWzdate()) ) )
 && ( (this.getWzdd()==castOther.getWzdd()) || ( this.getWzdd()!=null && castOther.getWzdd()!=null && this.getWzdd().equals(castOther.getWzdd()) ) )
 && ( (this.getWzxw()==castOther.getWzxw()) || ( this.getWzxw()!=null && castOther.getWzxw()!=null && this.getWzxw().equals(castOther.getWzxw()) ) )
 && ( (this.getWzsxh()==castOther.getWzsxh()) || ( this.getWzsxh()!=null && castOther.getWzsxh()!=null && this.getWzsxh().equals(castOther.getWzsxh()) ) )
 && ( (this.getCldw()==castOther.getCldw()) || ( this.getCldw()!=null && castOther.getCldw()!=null && this.getCldw().equals(castOther.getCldw()) ) )
 && ( (this.getPzdm()==castOther.getPzdm()) || ( this.getPzdm()!=null && castOther.getPzdm()!=null && this.getPzdm().equals(castOther.getPzdm()) ) )
 && ( (this.getWzcldz()==castOther.getWzcldz()) || ( this.getWzcldz()!=null && castOther.getWzcldz()!=null && this.getWzcldz().equals(castOther.getWzcldz()) ) )
 && ( (this.getLxdh()==castOther.getLxdh()) || ( this.getLxdh()!=null && castOther.getLxdh()!=null && this.getLxdh().equals(castOther.getLxdh()) ) )
 && ( (this.getFlag()==castOther.getFlag()) || ( this.getFlag()!=null && castOther.getFlag()!=null && this.getFlag().equals(castOther.getFlag()) ) );
   }
   
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + ( getCh() == null ? 0 : this.getCh().hashCode() );
         result = 37 * result + ( getCx() == null ? 0 : this.getCx().hashCode() );
         result = 37 * result + ( getCz() == null ? 0 : this.getCz().hashCode() );
         result = 37 * result + ( getWzdate() == null ? 0 : this.getWzdate().hashCode() );
         result = 37 * result + ( getWzdd() == null ? 0 : this.getWzdd().hashCode() );
         result = 37 * result + ( getWzxw() == null ? 0 : this.getWzxw().hashCode() );
         result = 37 * result + ( getWzsxh() == null ? 0 : this.getWzsxh().hashCode() );
         result = 37 * result + ( getCldw() == null ? 0 : this.getCldw().hashCode() );
         result = 37 * result + ( getPzdm() == null ? 0 : this.getPzdm().hashCode() );
         result = 37 * result + ( getWzcldz() == null ? 0 : this.getWzcldz().hashCode() );
         result = 37 * result + ( getLxdh() == null ? 0 : this.getLxdh().hashCode() );
         result = 37 * result + ( getFlag() == null ? 0 : this.getFlag().hashCode() );
         return result;
   }   
}

回复:Struts+Hibernate映射无主键表的问题

头有点晕

回复:Struts+Hibernate映射无主键表的问题

呵呵...可以使用 Query query=session.createQuery("from Wzcxtemp a");查询总记录数,

但不知道应该如何把数据显示出来!

回复:Struts+Hibernate映射无主键表的问题

解决了...
Session session=SessionFactory.currentSession();
Query query=session.createQuery("from Wzcxtemp a");
query.setMaxResults(10);

WzcxtempId wzid=new WzcxtempId();
Wzcxtemp wz=(Wzcxtemp)session.load(Wzcxtemp.class,wzid);

List list=query.list();
int size=list.size();

for(int i=0;i wz=(Wzcxtemp)list.get(i);
System.out.println(wz.getId().getCh());
}

就可以了


回复:Struts+Hibernate映射无主键表的问题

郁闷,如果是这样的话,那如果要进行条件查询呢?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics