Issue:
I’m encountering an error while working with Hibernate annotations, specifically when using the @Embeddable annotation. The error message I’m receiving is:
Could not determine recommended JdbcType for Java type ‘com.jack.secondTrail.Certificate’
Context:
I’m utilizing Hibernate annotations, particularly @Embeddable, in my Java project. The @Embeddable annotation is used to indicate that a class is to be embedded into another entity. In my case, I have a class named Certificate that I intend to embed into another entity.
The whole code:
EmbedTable.java file
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 file:
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 Class:
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;
}
}
Hemant Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.