javahibernatehbmxml

Hibernate mapping with 2 foregin keys


Since yesterday I tried to fix this error . I do not know where is the problem with mapping db. Problems started out when I added courseSubcaegro

Course hbm:

<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.apress.springrecipes.course">
  <class name="com.pb.coursesejb.model.Course" table="course" catalog="">
    <id name="courseId" type="java.lang.Integer">
        <column name="course_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="city" class="com.pb.coursesejb.model.City" fetch="select">
        <column name="city_id" not-null="true" />
    </many-to-one>
    <many-to-one name="currency" class="com.pb.coursesejb.model.Currency" fetch="select">
        <column name="currency_id" not-null="true" />
    </many-to-one>
    <property name="name" type="string">
        <column name="name" length="256" not-null="true" unique="true" />
    </property>
    <property name="desc" type="string">
        <column name="[desc]" not-null="true" unique="true" />
    </property>
    <property name="price" type="float">
        <column name="price" not-null="true" unique="true" />
    </property>
    <property name="from" type="timestamp">
        <column name="[from]" not-null="true" unique="true" />
    </property>
     <property name="to" type="timestamp">
        <column name="[to]" not-null="true" unique="true" />
    </property>
    <property name="registrationLock" type="timestamp">
        <column name="registration_lock" not-null="true" unique="true" />
    </property>
    <property name="link" type="string">
        <column name="link" length="120" not-null="true" unique="true" />
    </property>
    <set name="courseSubcategory" table="course_subcategory" 
            inverse="true" lazy="true" fetch="select">
        <key>
            <column name="course_id" not-null="true" />
        </key>
        <one-to-many class="com.pb.coursesejb.model.CourseSubcategory" />
    </set>
</class>

Course model:

public class Course {

private Integer courseId;
private String name;
private String desc;
private Float price;
private Date from;
private Date to;
private Date registrationLock;
private City city;
private Currency currency;
private String link;
private Set<CourseSubcategory> courseSubcategory = 
        new HashSet<CourseSubcategory>(0);

public Course(){}

public Course(Integer courseId, String name, String desc, Float price, Date     from, Date to, Date registrationLock, City city, Currency currency, String link, Set<CourseSubcategory> courseSubcategory) {
    super();
    this.courseId = courseId;
    this.name = name;
    this.desc = desc;
    this.price = price;
    this.from = from;
    this.to = to;
    this.registrationLock = registrationLock;
    this.city = city;
    this.currency = currency;
    this.link = link;
    this.courseSubcategory = courseSubcategory;
}

public Integer getCourseId() {
    return courseId;
}

public void setCourseId(Integer courseId) {
    this.courseId = courseId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public Float getPrice() {
    return price;
}

public void setPrice(Float price) {
    this.price = price;
}

public Date getFrom() {
    return from;
}

public void setFrom(Date from) {
    this.from = from;
}

public Date getTo() {
    return to;
}

public void setTo(Date to) {
    this.to = to;
}

public Date getRegistrationLock() {
    return registrationLock;
}

public void setRegistrationLock(Date registrationLock) {
    this.registrationLock = registrationLock;
}

public City getCity() {
    return city;
}

public void setCity(City city) {
    this.city = city;
}

public Currency getCurrency() {
    return currency;
}

public void setCurrency(Currency currency) {
    this.currency = currency;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public Set<CourseSubcategory> getCourseSubcategory() {
    return courseSubcategory;
}

public void setCourseSubcategory(Set<CourseSubcategory> courseSubcategory) {
    this.courseSubcategory = courseSubcategory;
}
}

Subcategory hbm:

<hibernate-mapping package="com.apress.springrecipes.course">
  <class name="com.pb.coursesejb.model.Subcategory" table="subcategory"     catalog="">
    <id name="subcategoryId" type="java.lang.Integer">
        <column name="subcategory_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="category" class="com.pb.coursesejb.model.Category" fetch="select">
        <column name="category_id" not-null="true" />
    </many-to-one>
    <property name="name" type="string">
        <column name="name" length="80" not-null="true" unique="true" />
    </property>
    <set name="courseSubcategory" table="course_subcategory" 
            inverse="true" lazy="true" fetch="select">
        <key>
            <column name="subcategory_id" not-null="true" />
        </key>
        <one-to-many class="com.pb.coursesejb.model.CourseSubcategory" />
    </set>
</class>

Subcategory model:

public class Subcategory implements java.io.Serializable {

private Integer subcategoryId;
private String name;
private Category category;
private Set<CourseSubcategory> courseSubcategory = 
        new HashSet<CourseSubcategory>(0);

public Subcategory(){}

public Subcategory(Integer subcategoryId, String name, Category category, Set<CourseSubcategory> courseSubcategory) {
    super();
    this.subcategoryId = subcategoryId;
    this.name = name;
    this.category = category;
    this.courseSubcategory = courseSubcategory;
}

public Integer getSubcategoryId() {
    return subcategoryId;
}

public void setSubcategoryId(Integer subcategoryId) {
    this.subcategoryId = subcategoryId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public Set<CourseSubcategory> getCourseSubcategory() {
    return courseSubcategory;
}

public void setCourseSubcategory(Set<CourseSubcategory> courseSubcategory) {
    this.courseSubcategory = courseSubcategory;
}

CourseSubcategory hbm:

<hibernate-mapping package="com.apress.springrecipes.course">
  <class name="com.pb.coursesejb.model.CourseSubcaegory" table="course_subcaegory" catalog="">
    <id name="courseSubcategoryId" type="java.lang.Integer">
        <column name="course_subcategory_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="subcategory" class="com.pb.coursesejb.model.Subcategory" fetch="select">
        <column name="subcategory_id" not-null="true" />
    </many-to-one>
     <many-to-one name="course" class="com.pb.coursesejb.model.Course" fetch="select">
        <column name="course_id" not-null="true" />
    </many-to-one>
</class>

CourseSubcategory model:

public class CourseSubcategory  implements java.io.Serializable{

private Integer courseSubcategoryId;
private Course course;
private Subcategory subcategory;

public CourseSubcategory(){}

public CourseSubcategory(Integer courseSubcategoryId, Course course,   Subcategory subcategory) {
    super();
    this.courseSubcategoryId = courseSubcategoryId;
    this.course = course;
    this.subcategory = subcategory;
}

public Integer getCourseSubcategoryId() {
    return courseSubcategoryId;
}

public void setCourseSubcategoryId(Integer courseSubcategoryId) {
    this.courseSubcategoryId = courseSubcategoryId;
}

public Course getCourse() {
    return course;
}

public void setCourse(Course course) {
    this.course = course;
}

public Subcategory getSubcategory() {
    return subcategory;
}

public void setSubcategory(Subcategory subcategory) {
    this.subcategory = subcategory;
}

Part of the hibernate cfg:

    <mapping resource="com/pb/coursesejb/model/hbm/City.hbm.xml"></mapping>
    <mapping resource="com/pb/coursesejb/model/hbm/Category.hbm.xml"></mapping>
    <mapping resource="com/pb/coursesejb/model/hbm/Subcategory.hbm.xml"></mapping>
    <mapping resource="com/pb/coursesejb/model/hbm/Course.hbm.xml"></mapping>
    <mapping resource="com/pb/coursesejb/model/hbm/Currency.hbm.xml"></mapping>
    <mapping resource="com/pb/coursesejb/model/hbm/CourseSubcategory.hbm.xml"></mapping>

and finally stacktrace:

Caused by: org.hibernate.boot.MappingException: Association [com.pb.coursesejb.model.Subcategory.courseSubcategory] references an unmapped entity [com.pb.coursesejb.model.Subcategory.courseSubcategory] : origin(com/pb/coursesejb/model/hbm/Subcategory.hbm.xml)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.bindCollectionTable(ModelBinder.java:3209)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.doSecondPass(ModelBinder.java:3147)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1659)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1634)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.pb.cours

Solution

  • From your stacktrace I read that Hibernate cannot find the entity mapping.

    <class name="com.pb.coursesejb.model.CourseSubcaegory" table="course_subcaegory" catalog="">
    

    It misses a "t" in: com.pb.coursesejb.model.CourseSubcategory".
    This since your entity class is: CourseSubcategory.

    To verify: does your table have a "course_subcaegory" table?
    This might also be a cause of your problem.