gpt4 book ai didi

java - Hibernate - 将 java.util.Calendar 映射到 MySQL BIGINT

转载 作者:行者123 更新时间:2023-11-29 00:28:38 25 4
gpt4 key购买 nike

我的实体中有一个 Calendar 字段

@Column(nullable = false)
private Calendar transmissionDate;

需要毫秒精度。照原样,Hibernate 生成一个模式,将这个字段映射到一个

+-------------------+--------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+-------------------+--------------+------+-----+---------+
| transmission_date | datetime | NO | | NULL |
+-------------------+--------------+------+-----+---------+

在 MySQL 中。 The datetime type in MySQL discards everything after the second ,所以我失去了我的精度。我现在一直在使用的解决方案是

@Column(nullable = false)
private Long transmissionDate;

并在需要时从中生成一个 Calendar 实例。

这是一个巨大的麻烦,我想知道 Hibernate 是否具有可以克服它的功能。 This question展示了如何使用自定义类型,但是在实现它时,Hibernate 仍然映射到 datetime 列类型。

如何在我的实体中仍然使用 Calendar 类型的同时保持毫秒精度?

最佳答案

我使用将 Calendar 映射到 BIGINT 的自定义 UserType 让它工作。

public class CalendarType implements UserType {

@Override
public int[] sqlTypes() {
return new int[] {Types.BIGINT};
}

@Override
public Class<?> returnedClass() {
return Calendar.class;
}

@Override
public boolean equals(Object x, Object y) throws HibernateException {
return x.equals(y);
}

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

@Override
public Object nullSafeGet(ResultSet resultSet, String[] names,SessionImplementor session, Object owner) throws HibernateException, SQLException {
Long timeInMillis = resultSet.getLong(names[0]);
if (timeInMillis == null) {
return null;
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
return calendar;
}
}

@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
Calendar calendar = (Calendar) value;
preparedStatement.setLong(index, calendar.getTimeInMillis());
}

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

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

@Override
public Serializable disassemble(Object value) throws HibernateException {
Calendar calendar = (Calendar) value;
return calendar.getTimeInMillis();
}

@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
Long timeInMillis = (Long) cached;

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
return calendar;
}

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

然后我的实体有

@TypeDef(name = "calendarType", typeClass = CalendarType.class)
@Entity
@Table
public class Entity {

@Type(type = "calendarType")
@Column(nullable = false)
private Calendar transmissionDate;

...
}

Hibernate 真是太神奇了。

关于java - Hibernate - 将 java.util.Calendar 映射到 MySQL BIGINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17688182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com