Hibernate @Embeddable注解报错:无法确定Certificate类的推荐JdbcType
使用Hibernate @Embeddable注解时的JdbcType错误解决
在Java项目中使用Hibernate的@Embeddable注解嵌入实体类时,遇到如下错误:
Could not determine recommended JdbcType for Java type 'com.jack.secondTrail.Certificate'
需求是将Certificate类嵌入到Student实体中,但代码存在配置错误导致该问题。
完整代码
EmbedTable.java 文件
package com.jack.secondTrail; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import jakarta.persistence.Embeddable; @Embeddable public class EmbedTable { public static void main(String[] args) { //configuration Configuration cfg = new Configuration(); cfg.configure(); SessionFactory factory = cfg.buildSessionFactory(); //1st student object Student student1 = new Student(); student1.setId(1254); student1.setName("Anuv Jain"); student1.setAddress("Mumbai"); //1st certificate obj for student 1 Certificate certificate = new Certificate(); certificate.setCourse("Java Development"); certificate.setCourse("5 months"); student1.setCerti(certificate); //2nd student object Student student2 = new Student(); student2.setId(1546); student2.setName("Nakul Dhull"); student2.setAddress("Gurgaon"); //2nd certificate object for student 2 Certificate certificate2 = new Certificate(); certificate2.setCourse("API Design"); certificate2.setDuration("4 months"); student2.setCerti(certificate2); //open session to changes Session se = factory.openSession(); Transaction tx = se.beginTransaction(); //for saving.. se.save(student1); se.save(student2); tx.commit(); se.close(); factory.close(); } }
Certificate.java 文件
package com.jack.secondTrail; public class Certificate { private String course; private String duration; public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public Certificate(String course, String duration) { super(); this.course = course; this.duration = duration; } public Certificate() { super(); // TODO Auto-generated constructor stub } }
Student.java 文件
package com.jack.secondTrail; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; @Entity public class Student { @Id private int id; private String name; private String address; private Certificate certi; public Student(int id, String name, String address, Certificate certi) { super(); this.id = id; this.name = name; this.address = address; this.certi = certi; } public Student() { super(); // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Certificate getCerti() { return certi; } public void setCerti(Certificate certi) { this.certi = certi; } @Override public String toString() { // TODO Auto-generated method stub return this.id+" : "+ this.name+" : "+ this.address; } }
问题修复步骤
1. 修正@Embeddable注解的位置
@Embeddable注解用于标记可嵌入到实体中的类,当前错误地添加到了作为程序入口的EmbedTable类上,需要将其移到Certificate类:
修改后的Certificate.java:
package com.jack.secondTrail; import jakarta.persistence.Embeddable; @Embeddable public class Certificate { // 原有属性和方法保持不变 }
同时删除EmbedTable类上的@Embeddable注解。
2. 修复student1的Certificate属性赋值错误
创建student1的Certificate对象时,重复调用setCourse方法导致duration属性未被设置,正确赋值应调用setDuration:
修改EmbedTable.java中的对应代码:
//1st certificate obj for student 1 Certificate certificate = new Certificate(); certificate.setCourse("Java Development"); certificate.setDuration("5 months"); // 替换原有的setCourse("5 months") student1.setCerti(certificate);
修复效果
完成上述修改后,Hibernate可正确识别Certificate为可嵌入类,将其字段映射到Student对应的数据库表中,不会再抛出JdbcType相关错误。
内容的提问来源于stack exchange,提问作者Hemant Garg
相关产品推荐
相关产品推荐

