无法确定推荐的 JdbcType”错误与 Hibernate @Embeddable 注释

问题描述 投票:0回答:1

问题: 我在使用 Hibernate 注释时遇到错误,特别是在使用 @Embeddable 注释时。我收到的错误消息是:

无法确定 Java 类型“com.jack.secondTrail.Certificate”的推荐 JdbcType

背景: 我在我的 Java 项目中使用 Hibernate 注释,特别是 @Embeddable。 @Embeddable 注解用于指示一个类要嵌入到另一个实体中。就我而言,我有一个名为 Certificate 的类,我打算将其嵌入到另一个实体中。

整个代码:

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
    }
    
}

学生班级:

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;
    }

}
java spring hibernate
1个回答
0
投票

您需要将

@Embeddable
注释添加到您的类中,该类是嵌入式类,而不是您的主类。

含义:从您的

@Embeddable
类中删除
EmbedTable
并将其添加到
Certificate 
类中。

package com.jack.secondTrail;

@Embeddable
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
    }
    
}

另请参阅此处:https://openjpa.apache.org/embeddable-samples.html

© www.soinside.com 2019 - 2024. All rights reserved.