欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

日期和枚举等字段类型的JPA映射

程序员文章站 2022-01-18 22:50:59
...
[color=red]枚举类[/color]
package cn.com.jyp.model;

import java.util.Date;

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

@Entity
@Table(name = "person")
public class Person {
private Integer id;
private String name;
private Date birthday;
private Gender gender;

@Id
@GeneratedValue
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Column(name = "personName", length = 15, 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;
}

@Enumerated(EnumType.STRING)
@Column(length = 5, nullable = false)
public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
this.gender = gender;
}

}



@Table(name="person")
指定表名
@Column(name = "personName", length = 15, nullable = false)
指定字段名,长度,不为空(nullable默认为true)
@Temporal(TemporalType.DATE)
映射date类型属性,TemporalType.DATE表示存到数据库为1988-02-04;TemporalType.TIME表示存到数据库为12:12:12
@Enumerated(EnumType.STRING)
映射枚举类型属性,EnumType.STRING表示把枚举类中的值存到数据库;EnumType.ORDINAL表示把枚举类中的索引存到数据库
相关标签: JPA