JPA 学习日志2

二、注解Annotation:

1.       实体类:@Entity @Table(name="user")

2.       主键:@Id @GeneratedValue

n         @GeneratedValue里面可以放两个属性

 String

generator 
          (Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.

 GenerationType

strategy 
          (Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.

n         GenerationType

AUTO 自动生成,是默认选项,MySQL自动生成
          Indicates that the persistence provider should pick an appropriate strategy for the particular database.

IDENTITY 自己指定主键
          Indicates that the persistence provider must assign primary keys for the entity using database identity column.

SEQUENCE 
          Indicates that the persistence provider must assign primary keys for the entity using database sequence column.

TABLE 
          Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

 

3.       字段:@Column(length=10, nullable=false, name="XXX") 

n         定义长度,不允许为空,自定义数据库字段名称

4.       日期:@Temporal(TemporalType.DATE)

n         DATE(日期), TIME(时间), TIMESTAMP(日期+时间)

n         private Date birthday;

5.       枚举(enum)@Enumerated(EnumType.STRING)

n         @Column(length=5,nullable=false)

n         EnumType.ORDINAL枚举的序列(从0开始)放到数据库中是序列数字,EnumType.STRING字符串值

n         设置默认值:private Gender gender = Gender.MAN;  

6.       大文本:@Lob public String getInfo(){return info;}

n         用字符串生成的最大长度是255,这时就要用到Lob

n         mysql中是LONGTEXT类型,

7.       文件:@Lob @Basic(fetch=FetchType.LAZY)

n         public Byte[] getFile(){return file;}

n         二进制数据,mysqlLONGBLOB,从文件中读到字节数据,然后放到数据库。当加载时,我们延迟加载,当需要时再加载这个文件,这样就不会在不需要文件时占用内存。但在访问时EntityManager,session必需是打开的,不需就会有错误。

8.       隐藏字段:@Transient属性不成为持久化字段(数据库中不生成对应字段)

package cn.itcast.bean.user;

 

import java.util.Date;

 

import javax.persistence.Basic;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Lob;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

import javax.persistence.Transient;

 

@Entity

public class Person {

    private Integer id;

    private String name;

    private Date birthday;

    private Gender gender = Gender.MAN;

    private String info;

    private Byte[] file;

    private String imagepath;

    @Id @GeneratedValue

    public Integer getId() {

       return id;

    }

    public void setId(Integer id) {

       this.id = id;

    }

    @Column(length=30, nullable=false)

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    @Temporal(TemporalType.DATE)

    public Date getBirthday() {

       return birthday;

    }

    public void setBirthday(Date birthday) {

       this.birthday = birthday;

    }

    @Column(length=5, nullable=false)

    public Gender getGender() {

       return gender;

    }

    public void setGender(Gender gender) {

       this.gender = gender;

    }

    @Lob

    public String getInfo() {

       return info;

    }

    public void setInfo(String info) {

       this.info = info;

    }

    @Lob @Basic(fetch=FetchType.LAZY)

    //由于我们在加载Person的时间必不立即加载文件,所以选择延迟加载

    public Byte[] getFile() {

       return file;

    }

    public void setFile(Byte[] file) {

       this.file = file;

    }

    @Transient

    //该属性就不会出现在数据库字段中

    public String getImagepath() {

       return imagepath;

    }

    public void setImagepath(String imagepath) {

       this.imagepath = imagepath;

    }

}

 

 

 

package cn.itcast.bean.user;

import java.util.Date;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.EnumType;

import javax.persistence.Enumerated;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToOne;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

 

@Entity

public class Buyer {

    /** 用户名 **/

    private String username;//只允许字母/数字/下划线

    /** 密码 **/

    private String password;//采用MD5加密

    /** 真实姓名 **/

    private String realname;

    /** 电子邮箱 **/

    private String email;

    /** 性别 **/

    private Gender gender=Gender.MAN;

    /** 联系信息 **/

    private ContactInfo contactInfo;

    /** 是否启用 **/

    private Boolean visible=true;

    /** 注册时间 **/

    private Date regTime = new Date();

   

    public Buyer() {

    }

    public Buyer(String username) {

       this.username = username;

    }

    public Buyer(String username, String password, String email) {

       this.username = username;

       this.password = password;

       this.email = email;

    }

    @Id @Column(length=20)

    public String getUsername() {

       return username;

    }

    public void setUsername(String username) {

       this.username = username;

    }

    @Column(length=32, nullable=false)

    public String getPassword() {

       return password;

    }

    public void setPassword(String password) {

       this.password = password;

    }

    @Column(length=10)

    public String getRealname() {

       return realname;

    }

    public void setRealname(String realname) {

       this.realname = realname;

    }

    @Column(length=40,nullable=false)

    public String getEmail() {

       return email;

    }

    public void setEmail(String email) {

       this.email = email;

    }

    @Enumerated(EnumType.STRING) @Column(length=5,nullable=false)

    public Gender getGender() {

       return gender;

    }

    public void setGender(Gender gender) {

       this.gender = gender;

    }

   

    @OneToOne(cascade=CascadeType.ALL)

    @JoinColumn(name="contact_id")

    public ContactInfo getContactInfo() {

        return contactInfo;

    }

    public void setContactInfo(ContactInfo contactInfo) {

       this.contactInfo = contactInfo;

    }

    @Column(nullable=false)

    public Boolean getVisible() {

       return visible;

    }

    public void setVisible(Boolean visible) {

       this.visible = visible;

    }

    @Column(nullable=false) @Temporal(TemporalType.TIMESTAMP)

    public Date getRegTime() {

       return regTime;

    }

    public void setRegTime(Date regTime) {

       this.regTime = regTime;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值