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

Hibernate 列映射 -- 日期时间类型映射

程序员文章站 2022-04-23 22:09:20
...

– Start
Java 类和数据库表有很多不同,其中之一是字段与列的数据类型不一致。Java 提供了很多日期时间类型,如何映射这些类型呢?我们可以使用注解 @Temporal

package shangbo.hibernate.demo012;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Customer {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customerId-generator")
	@SequenceGenerator(name = "customerId-generator", sequenceName = "CUSTOMER_ID_SEQ")
	private Integer customerId;

	private String customerName;

	@Temporal(TemporalType.DATE)
	private Date birthday;

	public Customer() {
	}

	public Customer(String customerName, Date birthday) {
		this.customerName = customerName;
		this.birthday = birthday;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

– 更多参见:Hibernate 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-06-19
– Written by ShangBo on 2019-06-19
– End