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

本文探讨了在Java中Hibernate如何处理日期时间类型的映射,重点介绍了使用@Temporal注解来实现不同Java日期时间类型与数据库列之间的转换。内容来源于《Hibernate精萃》,作者ShangBo于2019-06-19撰写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

– 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值