线程 "main" org.hibernate.MappingException 中的异常:无法确定类型:com.pojo.Address,位于 table:student_mto

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.pojo.Address, at table: student_mto

我正在使用 Hibernate 进行我从 this website 引用的 CRUD 操作22=]

我担心这是一个很常见的问题,但我还是没能解决它。 我知道这个问题已经被问过好几次了,但他们没有帮助我。我有以下测试:

Student.java

package com.pojo;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "student_mto")
public class Student {

    @Id
    @GeneratedValue
    private int studentId;
    private String studentName;
    private Address studentAddress;

    public Student(String studentName, Address studentAddress) {
        this.studentName = studentName;
        this.studentAddress = studentAddress;
    }
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "group_id")
    public Address getStudentAddress() {
        return studentAddress;
    }
    public void setStudentAddress(Address studentAddress) {
        this.studentAddress = studentAddress;
    }

}

Address.java

package com.pojo;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;


@Entity

@Table(name = "address_mto")

public class Address {

@Id
@GeneratedValue
private int addressId;
private String street;
private String city;
private String state;
private String zipcode;

public Address(String street, String city, String state, String zipcode) {
    this.street = street;
    this.city = city;
    this.state = state;
    this.zipcode = zipcode;
}
public int getAddressId() {
    return addressId;
}
public void setAddressId(int addressId) {
    this.addressId = addressId;
}
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getZipcode() {
    return zipcode;
}
public void setZipcode(String zipcode) {
    this.zipcode = zipcode;
}

}

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.pojo.Address, at table: student_mto, for columns: [org.hibernate.mapping.Column(studentAddress)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:486)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:453)
at org.hibernate.mapping.Property.isValid(Property.java:226)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624)
at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:347)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:466)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.Test.Test.main(Test.java:14)

请帮我解决这个问题..

您在字段(对于 ID)和 getter(对于地址)上放置了休眠注释。 Hibernate 不知道如何处理这种混合配置。

您应该移动其中一个,这样您就可以在任一字段或 getters 上同时拥有两个。