Search code examples
javamysqlhibernateannotations

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


I'm using Hibernate for CRUD operation that I referred from this website I wrote everything as it is just to be sure that no error should occur, but its displays an Exception Exception in thread "main" org.hibernate.MappingException: Could not determine type for:

I fear this is a very common problem, but I was not able to solve it anyway. I know this question have been asked several times but, they didn't help me. I have the following test:

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)

Please help me with this..


Solution

  • You placed the hibernate annotations on the field (for the ID) and also on the getter (for the address). Hibernate does not know how to handle such mixed configuration.

    You should move one of them, so you'll have both on either fields or getters.