Java 8 LocalDateTime和Hibernate 4

gst*_*low 18 hibernate date java-8

我有以下类描述片段:

... 
@Column(name = "invalidate_token_date")
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime invalidateTokenDate;
....
Run Code Online (Sandbox Code Playgroud)

此代码不起作用,因为@Temporal不支持LocalDateTime.我看到的建议如何使用LocalDateTime乔达时间,但我使用Java 8.

请给我一些建议.


PS
这是我目前的JPA依赖:

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 43

对于任何Hibernate 5.x用户,都有

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

你不需要做任何其他事情.只需添加依赖项,Java 8时间类型应该像任何其他基本类型一样工作,不需要注释.

private LocalDateTime invalidateTokenDate;
Run Code Online (Sandbox Code Playgroud)

注意:timestamp虽然这不会保存到类型.使用MySQL进行测试,它会保存到datetime类型.

  • 谢谢。在Spring 4.2.5和Hibernate 5.1.31中,添加hibernate-java8工件对我有用。正在获取org.hibernate.type.SerializationException:无法在具有LocalDateTime字段的实体上反序列化。 (2认同)

And*_*sne 22

由于Hibernate不支持它,因此您需要实现一个用户类型,如例所示.

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.usertype.EnhancedUserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class LocalDateTimeUserType implements EnhancedUserType, Serializable {

    private static final int[] SQL_TYPES = new int[]{Types.TIMESTAMP};

    @Override
    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    @Override
    public Class returnedClass() {
        return LocalDateTime.class;
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) {
            return true;
        }
        if (x == null || y == null) {
            return false;
        }
        LocalDateTime dtx = (LocalDateTime) x;
        LocalDateTime dty = (LocalDateTime) y;
        return dtx.equals(dty);
    }

    @Override
    public int hashCode(Object object) throws HibernateException {
        return object.hashCode();
    }


    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
        Object timestamp = StandardBasicTypes.TIMESTAMP.nullSafeGet(resultSet, names, session, owner);
        if (timestamp == null) {
            return null;
        }
        Date ts = (Date) timestamp;
        Instant instant = Instant.ofEpochMilli(ts.getTime());
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
        if (value == null) {
            StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, null, index, session);
        } else {
            LocalDateTime ldt = ((LocalDateTime) value);
            Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
            Date timestamp = Date.from(instant);
            StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, timestamp, index, session);
        }
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    @Override
    public Object assemble(Serializable cached, Object value) throws HibernateException {
        return cached;
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    @Override
    public String objectToSQLString(Object object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toXMLString(Object object) {
        return object.toString();
    }

    @Override
    public Object fromXMLString(String string) {
        return LocalDateTime.parse(string);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后可以在带有@Type注释的映射中使用新的usertype.例如

@Type(type="com.hibernate.samples.type.LocalDateTimeUserType")
@Column(name = "invalidate_token_date")
private LocalDateTime invalidateTokenDate;
Run Code Online (Sandbox Code Playgroud)

@Type注释需要实现userType接口的类的完整路径; 这是用于生成映射列的目标类型的工厂.

以下是如何在JPA2.1中执行相同的操作


Pet*_*nka 9

如果您可以使用Java EE 7,那么有更优雅的解决方案:

>>实现这个:

@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDateTime date) {
        if (date == null){
            return null;
        }
        return date.toDate();
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Date value) {
        if (value == null) {
            return null;
        }
        return LocalDateTime.fromDateFields(value);
    }
} 
Run Code Online (Sandbox Code Playgroud)

>>像这样使用:

... 
@Column(name = "invalidate_token_date")
private LocalDateTime invalidateTokenDate;
....
Run Code Online (Sandbox Code Playgroud)

(autoApply = true)表示@Converter自动用于转换LocalDateTimeJPA实体中的每个属性.

顺便说一句,AttributeConverter也非常适合映射枚举.

  • 它实际上是一个 JPA 特性,它也适用于 Spring Data JPA,不仅适用于 Java EE。 (2认同)

Vla*_*cea 7

自2.2版本,JPA提供了测绘的Java 8日期/时间API,像支持LocalDateTimeLocalTimeLocalDateTimeTimeOffsetDateTimeOffsetTime

此外,即使使用 JPA 2.1,Hibernate 5.2 默认也支持所有 Java 8 Date/Time API。

在 Hibernate 5.1 和 5.0 中,您必须添加hibernate-java8Maven 依赖项。

因此,让我们假设我们有以下实体:

@Entity(name = "UserAccount")
@Table(name = "user_account")
public class UserAccount {

    @Id
    private Long id;

    @Column(name = "first_name", length = 50)
    private String firstName;

    @Column(name = "last_name", length = 50)
    private String lastName;

    @Column(name = "subscribed_on")
    private LocalDateTime subscribedOn;

    //Getters and setters omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)

请注意,该subscribedOn属性是一个LocalDateTimeJava 对象。

当坚持UserAccount

UserAccount user = new UserAccount()
    .setId(1L)
    .setFirstName("Vlad")
    .setLastName("Mihalcea")
    .setSubscribedOn(
        LocalDateTime.of(
            2020, 5, 1,
            12, 30, 0
        )
    );

entityManager.persist(user);
Run Code Online (Sandbox Code Playgroud)

Hibernate 生成正确的 SQL INSERT 语句:

INSERT INTO user_account (
    first_name, 
    last_name, 
    subscribed_on, 
    id
) 
VALUES (
    'Vlad', 
    'Mihalcea', 
    '2020-05-01 12:30:00.0', 
    1
)
Run Code Online (Sandbox Code Playgroud)

在获取UserAccount实体时,我们可以看到LocalDateTime从数据库中正确获取了该实体:

UserAccount userAccount = entityManager.find(
    UserAccount.class, 1L
);

assertEquals(
    LocalDateTime.of(
        2020, 5, 1,
        12, 30, 0
    ),
    userAccount.getSubscribedOn()
);
Run Code Online (Sandbox Code Playgroud)