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.   @Table(name="_teacher")
  3.   public class Teacher {
  4.   private int id;
  5.   private String name;
  6.   private String title;
  7.   private String yourWifeName;
  8.   private Date birthDate;
  9.   private boolean good;
  10.   private Gender gender;
  11.   @Enumerated(EnumType.STRING)
  12.   public Gender getGender() {
  13.   return gender;
  14.   }
  15.   public void setGender(Gender gender) {
  16.   this.gender = gender;
  17.   }
  18.   public boolean isGood() {
  19.   return good;
  20.   }
  21.   public void setGood(boolean good) {
  22.   this.good = good;
  23.   }
  24.   @Transient
  25.   public String getYourWifeName() {
  26.   return yourWifeName;
  27.   }
  28.   public void setYourWifeName(String yourWifeName) {
  29.   this.yourWifeName = yourWifeName;
  30.   }
  31.   @Id
  32.   public int getId() {
  33.   return id;
  34.   }
  35.   public void setId(int id) {
  36.   this.id = id;
  37.   }
  38.   public String getName() {
  39.   return name;
  40.   }
  41.   public void setName(String name) {
  42.   this.name = name;
  43.   }
  44.   public String getTitle() {
  45.   return title;
  46.   }
  47.   public void setTitle(String title) {
  48.   this.title = title;
  49.   }
  50.   @Temporal(TemporalType.TIME)
  51.   public Date getBirthDate() {
  52.   return birthDate;
  53.   }
  54.   public void setBirthDate(Date birthDate) {
  55.   this.birthDate = birthDate;
  56.   }
  57.   }
  58. 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

时间: 2024-10-20 06:04:15

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

一步一步学EF系列【2、Fluent API的方式来处理实体与数据表之间的映射关系。】

EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是Fluent API,通过新增相应的配置类来覆盖默认配置另外.我们主要学习Fluent API,Data Annotations可以自行去学习一下. 补充一下为什么要用Fluent API 使用DataAnnotation非常简单,但对于EntityFramework中的特性,就要在实体类中引入Ent

修改Linux主机名与IP之间的映射关系

linux主机版本: Distributor ID: UbuntuDescription: Ubuntu 14.10Release: 14.10 一.修改linux主机名 1.使用hostname命令可以查看当前主机名 2.在 /etc/hostname文件中修改主机名后保存(有的版本在/etc/sysconfig/network中修改主机名) 二.修改主机名与IP之间的映射 在/etc/hosts中修改主机名和IP之间的映射 完成这两部之后重启系统. 然后使用命令ping新的主机名 发现主机名

中间人攻击——ARP欺骗 就是中间人攻击 利用报文欺骗并修改IP和mac地址之间的映射关系 让传送数据的地址失效

中间人攻击——ARP欺骗的原理.实战及防御 0x01 网关是啥? 网关是工作在OSI七层模型中的传输层或者应用层,用于高层协议的不同 网络之间的连接,网关就好比一个房间通向另一个房间的一扇门. 0x02 ARP协议 ARP(Address Resolution Protocol)地址转换协议,工作在OSI模型的数据链路层,在以太网中,网络设备之间互相通信是用MAC地址而不是IP地址,ARP协议就是用来把IP地址转换为MAC地址的.而RARP和ARP相反,它是反向地址转换协议,把MAC地址转换为I

Otter-安装配置-(6)批量导入映射关系

1.新增源实例数据源 2.新增目标实例库数据源 3.新增Canal配置 4.新增通道 5.新增Pipeline 6.新增映射 选择批量新增 7.编辑新增批量 city_order_refund_service,(.*).(.*),15,city_order_refund_service,(.*).(.*),5 city_order_service,(.*).(.*),15,city_order_service,(.*).(.*),5 city_bid_service,(.*).(.*),15,c

高斯分布和均匀分布之间的映射关系

step1 生成服从U(0,1)分布的u1,u2; step2 令 y = [-2*ln(u1)]^0.5*sin(2*pi*u2); step3 令 x = miu + y*delta,其中miu为均值,delta为标准差 代码: clear all; close all; xaxis = -5:0.1:5; %均值为1方差为1 miu = 1; delta = 1; PDFGaussian = 1/sqrt(2*pi*delta^2)*exp(-1/2*((xaxis - miu)/delt

windows server/windows同一系统下建立两个目录之间的映射关系

应用场景,如下: 当两个不同的项目共享同一个资源目录.同一个数据库时,由于两项目根目录不一样,再加上部分项目可能有入口重写规则限制了用户的访问权限. 因此,我们可以利用window 服务器给我们提供的mklink命令来建立两个项目资源目录的映射关系. 使用步骤: 1)进入cmd命令行模式 2)在cmd里面输入: mklink /J "D:\back1" "D:\back" 注意:执行的时候,back1 文件夹必须不存在:必须在NTFS 盘上进行才可以. 实质是,创建

Hibernate之Hibernate环境配置

Hibernate之Hibernate环境配置 一.Hibernate环境搭建的步骤 1.添加Hibernate && SQLServer 的Jar antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar j

Hibernate 零配置之Annotation注解

JPA规范推荐使用Annotation来管理实体类与数据表之间的映射关系,从而避免同时维护两份文件(Java 实体类 和 XML 映射文件),将映射信息(写在Annotation中)与实体类集中在一起. 以下我将使用eclipse来构建一个简单使用注解取代*.hbm.xml的查询小例子.(p.s 建议不要使用Myeclipse,他很方便但是对于初学者来说没有eclipse学得牢靠) 1.在数据库中构建一张表 2.生成相应的hibernate.cfg.xml文件 <?xml version="

Hibernate——hibernate的配置测试

Hibernate Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. 语言特点 将对数据库的操作转换为对Java对象的操作,从而简化开发.通过修改一个"持久化"对象的属性从而修改数据库表中对应的记录数据. 提供线程和进程两个级别的缓存提升应用程序性能. 有丰富的映射方