gpt4 book ai didi

java - 具有 2 个外键的 Hibernate 映射

转载 作者:行者123 更新时间:2023-12-01 10:04:35 25 4
gpt4 key购买 nike

从昨天开始我就尝试修复这个错误。我不知道映射数据库的问题出在哪里。当我添加 courseSubcaegro 时出现问题

类(class)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>

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

类(class)子类别 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>

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

部分 hibernate 配置:

    <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

最佳答案

从你的堆栈跟踪中我了解到 Hibernate 无法找到实体映射。

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

它缺少一个“t”:com.pb.coursesejb.model.CourseSubcategory”。
这是因为您的实体类是:CourseSubcategory。

要验证:您的表是否有“course_subcaegory”表?
这也可能是导致您出现问题的原因。

关于java - 具有 2 个外键的 Hibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36545808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com