9_hibernate基础配置_注解

以下讲解部分注解的作用,其中需要在xml中修改的注解,在下图中展示:


1、当表明和类名不一致时,增加注解@table(name="sqlTableName")(from:javax.persistence)   

package com.mym.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;

    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}

执行测试文件,控制台显示:

create table _Teacher (
        id integer not null,
        name varchar(255),
        title varchar(255),
        primary key (id)
    )
10:44:39,818  INFO SchemaExport:268 - schema export complete
Hibernate:
    insert
    into
        _Teacher
        (name, title, id)
    values
        (?, ?, ?)

2、字段名和属性相同

     所有没有定义注解的属性等价于在其上面添加了
                                                                      @Basic注解

3、字段名与属性名不同
     在getter方法上加注解:     
               @Cloumn(name ="sqlCloumnName")
package com.mym.hibernate.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;

    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}

控制台显示:

drop table if exists _Teacher
14:59:48,873 DEBUG SchemaExport:377 -
    create table _Teacher (
        id integer not null,
        _name varchar(255),
        title varchar(255),
        primary key (id)
    )
14:59:48,950  INFO SchemaExport:268 - schema export complete
Hibernate:
    insert
    into
        _Teacher
        (_name, title, id)
    values
        (?, ?, ?)


4、若实体类中的某个属性不想在数据库中生成,则可以用注解@Transient,即透明的
package com.mym.hibernate.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;
    private String yourWifeName;

    @Transient
    public String getYourWifeName() {
        return yourWifeName;
    }
    public void setYourWifeName(String yourWifeName) {
        this.yourWifeName = yourWifeName;
    }
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}

控制台显示(并没有显示建立加过@Transient注解的字段):

create table _Teacher (
        id integer not null,
        _name varchar(255),
        title varchar(255),
        primary key (id)
    )
15:10:04,473  INFO SchemaExport:268 - schema export complete
Hibernate:
    insert
    into
        _Teacher
        (_name, title, id)
    values
        (?, ?, ?)


在xml中如果不想要某个字段,直接不写即可

5、映射日期与时间类型,指定时间精度
     a、Annotation:@Temporal

如果在实体类中直接建立了Date(java.util.Date)类型的数据,注解为@Basic时,数据库中会自动生成datetime类型的数据2017-05-19 15:24:08,即包括日期,也包括时间,
package com.mym.hibernate.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;
    private String yourWifeName;
    private Date birthDate;


    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    @Transient
    public String getYourWifeName() {
        return yourWifeName;
    }
    public void setYourWifeName(String yourWifeName) {
        this.yourWifeName = yourWifeName;
    }
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}


数据库中显示:



可以通过@Temporal( TemporalType.DATE)设置只显示日期;
或者 @ Temporal(TemporalType. TIME )设置只显示时间
package com.mym.hibernate.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;
    private String yourWifeName;
    private Date birthDate;

    @Temporal(TemporalType.DATE)
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    @Transient
    public String getYourWifeName() {
        return yourWifeName;
    }
    public void setYourWifeName(String yourWifeName) {
        this.yourWifeName = yourWifeName;
    }
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}

6、映射枚举类型
     Annotation:@Enumerated
     xml:麻烦
新建一个Enume类型的类,命名为ZhiCheng


package com.mym.hibernate.model;

public enum ZhiCheng {
     A,B,C
}

给Teacher建立ZhiCheng属性,并且使用 @Enumerated (EnumType. STRING )和 @Enumerated (EnumType. ORDINAL )分别对getter方法修饰

package com.mym.hibernate.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Teacher {
    private int id;
    private String name;
    private String title;
    private String yourWifeName;
    private Date birthDate;
    private ZhiCheng zhiCheng;

    @Enumerated(EnumType.STRING)
    public ZhiCheng getZhiCheng() {
        return zhiCheng;
    }
    public void setZhiCheng(ZhiCheng zhiCheng) {
        this.zhiCheng = zhiCheng;
    }
    @Temporal(TemporalType.DATE)
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    @Transient
    public String getYourWifeName() {
        return yourWifeName;
    }
    public void setYourWifeName(String yourWifeName) {
        this.yourWifeName = yourWifeName;
    }
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name="_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}


测试文件中:


package com.mym.hibernate.model;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class TeacherTest {
    private static SessionFactory sf = null;

    @BeforeClass
    public static void beforeClass(){
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }

    @Test
    public void testTeacherSave(){
        Teacher t = new Teacher();
        t.setId(6);
        t.setName("t1");
        t.setTitle("tt2");
        t.setBirthDate(new Date());
        t.setZhiCheng(ZhiCheng.A);

        Session session = sf.openSession();
        session.beginTransaction();
        session.save(t);
        session.getTransaction().commit();
        session.close();
    }

    @AfterClass
    public static void afterClass(){
        sf.close();
    }
}

情况一:
当使用 @Enumerated (EnumType. STRING )修饰时,是以varchar(字符串)的类型将枚举(Enume)类型的值存入数据库


当使用@Enumerated(EnumType.ORDINAL)修饰时,是以integer(int)的类型将枚举(Enum)类中值的数组下标的值存入数据库中


7、字段映射的位置
     放到field(成员变量)上和get方法上效果一样
     建议放在get方法上
     因为field 定义的是private类型的,既然要定义为私有,就是不想再被进行更改。而如果将注解放到field上,就相当于hibernate通过映射,访问了field,不符合我们的想法,所以不建议放到field即成员变量上





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值