Spring Boot 2.7.2升级至3.0.0-SNAPSHOT:@Type注解报错如何修复?
Spring Boot 3.0升级后@Type注解编译错误的修复方案
问题场景
将Spring Boot从2.7.2升级到3.0.0-SNAPSHOT后,实体类中使用@Type(type = "org.hibernate.type.TextType")注解出现编译错误,相关实体类代码如下:
package com.example.BLModel; import org.hibernate.annotations.Type; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MapsId; import javax.persistence.Table; @Entity @Table(name = "email_template_custom") public class EmailTemplateCustom { @EmbeddedId private EmailTemplateCustomId id; @MapsId("tenantId") @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "tenant_id", nullable = false) private Tenant tenant; @Column(name = "email_subject", nullable = false, length = 1000) private String emailSubject; @Column(name = "email_content") @Type(type = "org.hibernate.type.TextType") private String emailContent; // getter和setter方法省略 }
错误信息
D:\github\spring_jwt\src\main\java\com\example\BLModel\EmailTemplateCustom.java:29:11 java: cannot find symbol symbol: method type() location: @interface org.hibernate.annotations.Type
修复方案
原因说明
Spring Boot 3.0对应的Hibernate版本是6.x,该版本对@Type注解做了API调整:原有的type()属性被移除,替换为value()属性;同时Hibernate 6对String类型映射到数据库TEXT字段的支持更完善,推荐使用JPA标准注解替代Hibernate特定注解。
方式1:适配Hibernate 6的@Type注解
将原注解的type参数改为value参数即可:
@Column(name = "email_content") @Type(value = "org.hibernate.type.TextType") private String emailContent;
方式2:使用JPA标准注解(更推荐)
直接通过@Column的columnDefinition属性指定数据库字段类型,完全脱离Hibernate特定注解,更符合JPA规范:
@Column(name = "email_content", columnDefinition = "TEXT") private String emailContent;
内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ
相关产品推荐
相关产品推荐

