Hibernate基本配置:类与表之间的映射关系

只介绍用Annotation的方式进行映射关系的配置。

  1. 如果表名与对象名不一致,则要对表名进行配置。

  使用@Table

  2. 如果字段名与属性名不一致。

  使用@Column

  3. 不需要persistentence的字段

  使用@Persistentence

  4.映射日期与时间类型,指定时间精度

  使用@Temporal

  默认会直接记录日期与时分秒,可以进行定制。 @Temporal(TemporalType.?),?可以取3个值,DTAE(只记录日期),TIME(记录时分秒),TIMESTAMP(日期与时分秒都记录)。

  5.映射枚举类型

  使用@Enumerated

  @Enumerated(EnumType.?)。?可取STRING、ORIDINAL,STRING表示在数据库中将以string进行储存,直接会储存枚举值。ORIDINAL表示将以数字进行储存,数字为该枚举值在枚举类型定义中所对应的位置。

  范例:

Java代码  
  1. @Entity  
  2.   
  3.   @Table(name="_teacher")  
  4.   
  5.   public class Teacher {  
  6.   
  7.   private int id;  
  8.   
  9.   private String name;  
  10.   
  11.   private String title;  
  12.   
  13.   private String yourWifeName;  
  14.   
  15.   private Date birthDate;  
  16.   
  17.   private boolean good;  
  18.   
  19.   private Gender gender;  
  20.   
  21.   @Enumerated(EnumType.STRING)  
  22.   
  23.   public Gender getGender() {  
  24.   
  25.   return gender;  
  26.   
  27.   }  
  28.   
  29.   public void setGender(Gender gender) {  
  30.   
  31.   this.gender = gender;  
  32.   
  33.   }  
  34.   
  35.   public boolean isGood() {  
  36.   
  37.   return good;  
  38.   
  39.   }  
  40.   
  41.   public void setGood(boolean good) {  
  42.   
  43.   this.good = good;  
  44.   
  45.   }  
  46.   
  47.   @Transient  
  48.   
  49.   public String getYourWifeName() {  
  50.   
  51.   return yourWifeName;  
  52.   
  53.   }  
  54.   
  55.   public void setYourWifeName(String yourWifeName) {  
  56.   
  57.   this.yourWifeName = yourWifeName;  
  58.   
  59.   }  
  60.   
  61.   @Id  
  62.   
  63.   public int getId() {  
  64.   
  65.   return id;  
  66.   
  67.   }  
  68.   
  69.   public void setId(int id) {  
  70.   
  71.   this.id = id;  
  72.   
  73.   }  
  74.   
  75.   public String getName() {  
  76.   
  77.   return name;  
  78.   
  79.   }  
  80.   
  81.   public void setName(String name) {  
  82.   
  83.   this.name = name;  
  84.   
  85.   }  
  86.   
  87.   public String getTitle() {  
  88.   
  89.   return title;  
  90.   
  91.   }  
  92.   
  93.   public void setTitle(String title) {  
  94.   
  95.   this.title = title;  
  96.   
  97.   }  
  98.   
  99.   @Temporal(TemporalType.TIME)  
  100.   
  101.   public Date getBirthDate() {  
  102.   
  103.   return birthDate;  
  104.   
  105.   }  
  106.   
  107.   public void setBirthDate(Date birthDate) {  
  108.   
  109.   this.birthDate = birthDate;  
  110.   
  111.   }  
  112.   
  113.   }  
  114. http://www.iteye.com/problems/123708
    http://www.iteye.com/problems/123709
    http://www.iteye.com/problems/123710
    http://www.iteye.com/problems/123711
    http://www.iteye.com/problems/123712
    http://www.iteye.com/problems/123713
    http://www.iteye.com/problems/123714
    http://www.iteye.com/problems/123715
    http://www.iteye.com/problems/123716
    http://www.iteye.com/problems/123717
    http://www.iteye.com/problems/123718
    http://www.iteye.com/problems/123719
    http://www.iteye.com/problems/123720
    http://www.iteye.com/problems/123721
    http://www.iteye.com/problems/123722
    http://www.iteye.com/problems/123723
    http://www.iteye.com/problems/123724
    http://www.iteye.com/problems/123725
    http://www.iteye.com/problems/123726
    http://www.iteye.com/problems/123727
    http://www.iteye.com/problems/123728
    http://www.iteye.com/problems/123729
    http://www.iteye.com/problems/123730
    http://www.iteye.com/problems/123731
    http://www.iteye.com/problems/123732
    http://www.iteye.com/problems/123733
    http://www.iteye.com/problems/123734
    http://www.iteye.com/problems/123735
    http://www.iteye.com/problems/123736
    http://www.iteye.com/problems/123737
    http://www.iteye.com/problems/123738
    http://www.iteye.com/problems/123739
    http://www.iteye.com/problems/123740
    http://www.iteye.com/problems/123741
    http://www.iteye.com/problems/123742
    http://www.iteye.com/problems/123743
    http://www.iteye.com/problems/123744
    http://www.iteye.com/problems/123745
    http://www.iteye.com/problems/123746
    http://www.iteye.com/problems/123747
    http://www.iteye.com/problems/123748
    http://www.iteye.com/problems/123749
    http://www.iteye.com/problems/123750
    http://www.iteye.com/problems/123751
    http://www.iteye.com/problems/123752
    http://www.iteye.com/problems/123753
    http://www.iteye.com/problems/123754
    http://www.iteye.com/problems/123755
    http://www.iteye.com/problems/123756
    http://www.iteye.com/problems/123757
    http://www.iteye.com/problems/123758
    http://www.iteye.com/problems/123759
    http://www.iteye.com/problems/123760
    http://www.iteye.com/problems/123761
    http://www.iteye.com/problems/123762
    http://www.iteye.com/problems/123763
    http://www.iteye.com/problems/123764
    http://www.iteye.com/problems/123765
    http://www.iteye.com/problems/123766
    http://www.iteye.com/problems/123767
    http://www.iteye.com/problems/123768
    http://www.iteye.com/problems/123769
    http://www.iteye.com/problems/123770
    http://www.iteye.com/problems/123771
    http://www.iteye.com/problems/123772
    http://www.iteye.com/problems/123773
    http://www.iteye.com/problems/123774
    http://www.iteye.com/problems/123775
    http://www.iteye.com/problems/123776
    http://www.iteye.com/problems/123777
    http://www.iteye.com/problems/123778
    http://www.iteye.com/problems/123779
    http://www.iteye.com/problems/123780
    http://www.iteye.com/problems/123781
    http://www.iteye.com/problems/123782
    http://www.iteye.com/problems/123799
    http://www.iteye.com/problems/123798
    http://www.iteye.com/problems/123797
    http://www.iteye.com/problems/123794
    http://www.iteye.com/problems/123792
    http://www.iteye.com/problems/123791
    http://www.iteye.com/problems/123790
    http://www.iteye.com/problems/123786
    http://www.iteye.com/problems/123763
    http://www.iteye.com/problems/123764
    http://www.iteye.com/problems/123765
    http://www.iteye.com/problems/123766
    http://www.iteye.com/problems/123767
    http://www.iteye.com/problems/123785
    http://www.iteye.com/problems/123784
    http://www.iteye.com/problems/123783
    http://www.iteye.com/problems/123781
    http://www.iteye.com/problems/123779
    http://www.iteye.com/problems/123778
    http://www.iteye.com/problems/123777
    http://www.iteye.com/problems/123776
    http://www.iteye.com/problems/123775
    http://www.iteye.com/problems/123774
    http://www.iteye.com/problems/123773
    http://www.iteye.com/problems/123768
    http://www.iteye.com/problems/123772
    http://www.iteye.com/problems/123771
    http://www.iteye.com/problems/123770
    http://www.iteye.com/problems/123769
    http://www.iteye.com/problems/123762
    http://www.iteye.com/problems/123760
    http://www.iteye.com/problems/123759
    http://www.iteye.com/problems/123878
    http://www.iteye.com/problems/123880
    http://www.iteye.com/problems/123883
    http://www.iteye.com/problems/123885
    http://www.iteye.com/problems/123888
    http://www.iteye.com/problems/123890
    http://www.iteye.com/problems/123893
    http://www.iteye.com/problems/123895
    http://www.iteye.com/problems/123897
    http://www.iteye.com/problems/123898
    http://www.iteye.com/problems/123899
    http://www.iteye.com/problems/123900
    http://www.iteye.com/problems/123902
    http://www.iteye.com/problems/123904
    http://www.iteye.com/problems/123906
    http://www.iteye.com/problems/123909
    http://www.iteye.com/problems/123910
    http://www.iteye.com/problems/123912
    http://www.iteye.com/problems/123914
    http://www.iteye.com/problems/123916
    http://www.iteye.com/problems/123917
    http://www.iteye.com/problems/123921
    http://www.iteye.com/problems/123923
    http://www.iteye.com/problems/123925
    http://www.iteye.com/problems/123926
    http://www.iteye.com/problems/123928
    http://www.iteye.com/problems/123930
    http://www.iteye.com/problems/123933
    http://www.iteye.com/problems/123935
    http://www.iteye.com/problems/123938
    http://www.iteye.com/problems/123868
    http://www.iteye.com/problems/123870
    http://www.iteye.com/problems/123872
    http://www.iteye.com/problems/123873
    http://www.iteye.com/problems/123874
    http://www.iteye.com/problems/123875
    http://www.iteye.com/problems/123963
    http://www.iteye.com/problems/123971
    http://www.iteye.com/problems/123926
    http://www.iteye.com/problems/124102
    http://www.iteye.com/problems/124101
    http://www.iteye.com/problems/124100
    http://www.iteye.com/problems/124099
    http://www.iteye.com/problems/124103
    http://www.iteye.com/problems/124104
    http://www.iteye.com/problems/124105
    http://www.iteye.com/problems/124106
    http://www.iteye.com/problems/124107
    http://www.iteye.com/problems/124108
    http://www.iteye.com/problems/124110
    http://www.iteye.com/problems/124111
    http://www.iteye.com/problems/124112
    http://www.iteye.com/problems/124113
    http://www.iteye.com/problems/124109
    http://www.iteye.com/problems/124096
    http://www.iteye.com/problems/124097
    http://www.iteye.com/problems/124098
    http://www.iteye.com/problems/123917

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。