JPA注解数据类型

 

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="person_jpa")
public class PersonJPA {
 private Integer id;     //主键,自增
 private String name;
 private Date birthday;  //日期类型
 private Gender gender = Gender.MAN; //枚举
 private byte[] image;   //存放很大的字节流
 private String info;    //存放很大的文件
 private String imgpath; //不想映射到数据库中的表字段
 
 public PersonJPA(){}
 public PersonJPA(String name){
  this.name=name;
 }
 
 //id为主键,策略为auto,它为默认值,也可以省略下面括号里面的内容
 @Id @GeneratedValue(strategy=GenerationType.AUTO)
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 @Column(length=20,name="person_name",nullable=false)
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 /**
  * 使用@Temporal注解,可以设置日期类型
  * @return
  */
 @Temporal(TemporalType.DATE) @Column(length=12)
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 /**
  * @Enumerated表示枚举注解
  * @return
  */
 @Enumerated(EnumType.STRING) @Column(nullable=false,length=8)
 public Gender getGender() {
  return gender;
 }
 public void setGender(Gender gender) {
  this.gender = gender;
 }
 /**
  * 使用@Basic注解,表示当获取person对象时,并不会加载该属性的值。只有当需要获取它的值时且
  * EntityManager是打开的,那么只需要调用它的get方法就可以了。有点类似懒加载原理。
  * @return
  */
 @Lob @Basic(fetch=FetchType.LAZY)
 public byte[] getImage() {
  return image;
 }
 public void setImage(byte[] image) {
  this.image = image;
 }
 /**
  * 使用@Lob注解,表示设置大字符串,例如一片文章等
  * @return
  */
 @Lob
 public String getInfo() {
  return info;
 }
 public void setInfo(String info) {
  this.info = info;
 }
 /**
  * 使用@Transient注解,表示该字段并不会映射到数据库的表字段
  * @return
  */
 @Transient
 public String getImgpath() {
  return imgpath;
 }
 public void setImgpath(String imgpath) {
  this.imgpath = imgpath;
 }

}

public enum Gender {
 MAN,WOMAN

}

 

posted @ 2013-04-15 20:20  若 ♂ 只如初见  阅读(1634)  评论(0编辑  收藏  举报