`
shaoxiongwang#21cn.com
  • 浏览: 74407 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

关于Hibernate自动生成工具的问题

阅读更多
java 代码
 
  1. <hibernate-mapping>  
  2.     <class name="com.pilotek.ups.pk.hbm.Table1" table="TABLE1" schema="dbo" catalog="UpsMonitor">  
  3.         <composite-id name="id" class="com.pilotek.ups.pk.hbm.Table1Id">  
  4.             <key-property name="id" type="java.lang.String">  
  5.                 <column name="id" length="50" />  
  6.             </key-property>  
  7.             <key-property name="name" type="java.lang.String">  
  8.                 <column name="name" length="10" />  
  9.             </key-property>  
  10.         </composite-id>  
  11.     </class>  
  12. </hibernate-mapping>  
  13.   
  14. pojo  
  15. public class Table1  implements java.io.Serializable {  
  16.   
  17.   
  18.     // Fields      
  19.   
  20.      private Table1Id id;  
  21.   
  22.   
  23.     // Constructors  
  24.   
  25.     /** default constructor */  
  26.     public Table1() {  
  27.     }  
  28.   
  29.      
  30.     // Property accessors  
  31.   
  32.     public Table1Id getId() {  
  33.         return this.id;  
  34.     }  
  35.       
  36.     public void setId(Table1Id id) {  
  37.         this.id = id;  
  38.     }  
  39.   
  40. }  
  41.   
  42. public class Table1Id  implements java.io.Serializable {  
  43.   
  44.   
  45.     // Fields      
  46.   
  47.      private String id;  
  48.      private String name;  
  49.   
  50.   
  51.     // Constructors  
  52.   
  53.     /** default constructor */  
  54.     public Table1Id() {  
  55.     }  
  56.   
  57.     /** minimal constructor */  
  58.     public Table1Id(String id) {  
  59.         this.id = id;  
  60.     }  
  61.       
  62.     /** full constructor */  
  63.     public Table1Id(String id, String name) {  
  64.         this.id = id;  
  65.         this.name = name;  
  66.     }  
  67.   
  68.      
  69.     // Property accessors  
  70.   
  71.     public String getId() {  
  72.         return this.id;  
  73.     }  
  74.       
  75.     public void setId(String id) {  
  76.         this.id = id;  
  77.     }  
  78.   
  79.     public String getName() {  
  80.         return this.name;  
  81.     }  
  82.       
  83.     public void setName(String name) {  
  84.         this.name = name;  
  85.     }  
  86.      
  87.   
  88.   
  89.   
  90.    public boolean equals(Object other) {  
  91.          if ( (this == other ) ) return true;  
  92.          if ( (other == null ) ) return false;  
  93.          if ( !(other instanceof Table1Id) ) return false;  
  94.          Table1Id castOther = ( Table1Id ) other;   
  95.            
  96.          return ( (this.getId()==castOther.getId()) || ( this.getId()!=null && castOther.getId()!=null && this.getId().equals(castOther.getId()) ) )  
  97.  && ( (this.getName()==castOther.getName()) || ( this.getName()!=null && castOther.getName()!=null && this.getName().equals(castOther.getName()) ) );  
  98.    }  
  99.      
  100.    public int hashCode() {  
  101.          int result = 17;  
  102.            
  103.          result = 37 * result + ( getId() == null ? 0 : this.getId().hashCode() );  
  104.          result = 37 * result + ( getName() == null ? 0 : this.getName().hashCode() );  
  105.          return result;  
  106.    }    
  107. }  
最近遇到很怪的问题,搞了几天才弄明白, 使用myeclipse和hibernate-tool工具,自动生成代码.hbm.xml,pojo,自动产生的代码见上面, <hibernate-mapping> <class catalog="UpsMonitor" schema="dbo" table="TABLE1" name="com.pilotek.ups.pk.hbm.Table1"> <composite-id class="com.pilotek.ups.pk.hbm.Table1Id" name="id"> <key-property type="java.lang.String" name="id"> <column length="50" name="id"> </column> <key-property type="java.lang.String" name="name"> <column length="10" name="name"> </column> </key-property> </key-property> </composite-id>全部是以组合键的形式出现,搞了半天,把eclipse也折腾了几次,差点崩溃,差点全部要手写了,最后发现原来表必须要设某列为主键,否则产生的代码就是这样,就是以组合键的方式出现.</class></hibernate-mapping>
分享到:
评论
1 楼 adler_sui 2009-11-30  
我也遇到同样的问题,也是看了楼主的帖子,才恍然大悟

相关推荐

Global site tag (gtag.js) - Google Analytics