LocalDate到JPA的数据库 [英] LocalDate into database with JPA

查看:170
本文介绍了LocalDate到JPA的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表格:

I'm trying to create a table:

@Entity
@Table(name = "MENU")
public class Menu {                             
    private LocalDate date; 

    @Column(name = "DATE", nullable = false)
    public LocalDate getDate() {
        return date;
    }
public void setDate(LocalDate date) {
    this.date = date;
}

create表生成DATE raw(255)not null,我读入并尝试创建转换器:

The create table produces DATE raw(255) not null, and i need it to be date, i read into it and attempted to create a converter:

 @Converter(autoApply = true)
    public class LocalDatePersistenceConverter implements
            AttributeConverter<java.time.LocalDate, java.sql.Date> {
        @Override
        public java.sql.Date convertToDatabaseColumn(LocalDate entityValue) {
           return java.sql.Date.valueOf(entityValue);
        }

        @Override
        public LocalDate convertToEntityAttribute(java.sql.Date databaseValue) {
            return databaseValue.toLocalDate();
        }

但没有什么改变,我错过了什么?

But nothing changes, what have i missed?

推荐答案

使用 @ Column.columnDefinition 自定义此列的数据库类型:

Use @Column.columnDefinition to customize the DB type of this column:

@Column(name = "DATE", columnDefinition="DATE NOT NULL")
public LocalDate getDate() {
    return date;
}

祝你好运!

这篇关于LocalDate到JPA的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆