具有 2 个外键的 Hibernate 映射
Hibernate mapping with 2 foregin keys
从昨天开始我就试图修复这个错误。我不知道映射数据库的问题在哪里。当我添加 courseSubcaegro
时问题开始了
课程 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>
课程模型:
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;
}
}
子类别 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>
子类模型:
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;
}
课程子类别 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>
课程子类模型:
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;
}
休眠配置的一部分:
<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>
最后是堆栈跟踪:
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
不确定这是否与错误有关。但是,我确实观察到您的映射定义指定了 package
属性,并且 class
属性也具有包定义。而且他们完全不同。
<hibernate-mapping package="com.apress.springrecipes.course">
<class name="com.pb.coursesejb.model.Course" table="course" catalog="">
从你的堆栈跟踪中我了解到 Hibernate 找不到实体映射。
<class name="com.pb.coursesejb.model.CourseSubcaegory" table="course_subcaegory" catalog="">
它在 com.pb.coursesejb.model.CourseSubcategory"."t" 中遗漏了一个 "t"。
这是因为您的实体 class 是:CourseSubcategory.
验证一下:你的 table 有 "course_subcaegory" table 吗?
这也可能是您遇到问题的原因。
从昨天开始我就试图修复这个错误。我不知道映射数据库的问题在哪里。当我添加 courseSubcaegro
时问题开始了课程 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>
课程模型:
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;
}
}
子类别 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>
子类模型:
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;
}
课程子类别 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>
课程子类模型:
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;
}
休眠配置的一部分:
<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>
最后是堆栈跟踪:
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
不确定这是否与错误有关。但是,我确实观察到您的映射定义指定了 package
属性,并且 class
属性也具有包定义。而且他们完全不同。
<hibernate-mapping package="com.apress.springrecipes.course">
<class name="com.pb.coursesejb.model.Course" table="course" catalog="">
从你的堆栈跟踪中我了解到 Hibernate 找不到实体映射。
<class name="com.pb.coursesejb.model.CourseSubcaegory" table="course_subcaegory" catalog="">
它在 com.pb.coursesejb.model.CourseSubcategory"."t" 中遗漏了一个 "t"。
这是因为您的实体 class 是:CourseSubcategory.
验证一下:你的 table 有 "course_subcaegory" table 吗?
这也可能是您遇到问题的原因。