Hibernate Types是Hibernate默认不支持的一组额外类型。其中一种类型是java.time.YearMonth。这里展示一个Spring Boot应用程序,说明是如何使用Hibernate Type将YearMonthMySQL数据库存储为整数或日期。
关键点:
- 对于Maven,将Hibernate Types添加为依赖项pom.xml
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.3.5</version>
</dependency> - 在实体中,用@TypeDef映射typeClass到defaultForType
@Entity
@TypeDef(
typeClass = YearMonthIntegerType.class, // or, YearMonthDateType
defaultForType = YearMonth.class
)
public class Royalty implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private float amount;
private YearMonth payedOn;
源代码可以在这里找到