【问题标题】:Hibernate java web application休眠Java Web应用程序
【发布时间】:2013-04-20 18:44:50
【问题描述】:

我正在开发一个使用 hibernate 框架的 java JSP web 应用程序。

我是 JSP/hibernate 的初学者,我似乎无法让 hibernate 工作。 我已遵循本教程:https://netbeans.org/kb/docs/web/hibernate-webapp.html 这一切都奏效了。我将 xampp 与 phpmyadmin 一起使用,我可以通过 hibernate.cfg.xml 文件执行 HQL 查询。

然后我对用于 Web 应用程序的数据库尝试了同样的事情。遵循所有步骤并完成所有向导。但我无法执行 HQL 查询。

出现以下错误:

org.hibernate.MappingException: An association from the table campingsperfestival refers to an unmapped class: festivalOverview.Campings
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)

我遵循了 Hibernate Mapping Files and POJOs from Database 向导,它为数据库中的所有表生成了一个 .java 和 .hbm.xml 文件,除了一个表:“campingsperfestival”表。我再次完成了几次向导并重新开始,但它仍然没有为“campingsperfestival”表生成 .java 和 .hbm.xml 文件。

“campingsperfestival”表是一个有 2 个 id 的表,它们都有一个外键。有些节日和露营都有 ID,“campingsperfestival”在一张桌子上匹配这 2 个 ID。

Campings.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20-apr-2013 12:04:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="festivalOverview.Campings" table="campings" catalog="groep11_festivals">
        <id name="campId" type="java.lang.Integer">
            <column name="camp_id" />
            <generator class="identity" />
        </id>
        <property name="campAdres" type="string">
            <column name="camp_adres" not-null="true" />
        </property>
        <property name="campCap" type="int">
            <column name="camp_cap" not-null="true" />
        </property>
        <set name="festivalses" inverse="true" table="campingsperfestival">
            <key>
                <column name="camp_id" not-null="true" />
            </key>
            <many-to-many entity-name="festivalOverview.Festivals">
                <column name="fest_id" not-null="true" />
            </many-to-many>
        </set>
        <set name="facpercamps" inverse="true">
            <key>
                <column name="camp_id" not-null="true" />
            </key>
            <one-to-many class="festivalOverview.Facpercamp" />
        </set>
    </class>
</hibernate-mapping>

节日.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20-apr-2013 12:04:37 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="festivalOverview.Festivals" table="festivals" catalog="groep11_festivals">
        <id name="festId" type="java.lang.Integer">
            <column name="fest_id" />
            <generator class="identity" />
        </id>
        <property name="festNaam" type="string">
            <column name="fest_naam" length="100" not-null="true" />
        </property>
        <property name="festLocatie" type="string">
            <column name="fest_locatie" length="200" not-null="true" />
        </property>
        <property name="festDatum" type="date">
            <column name="fest_datum" length="10" not-null="true" />
        </property>
        <property name="festDuur" type="byte">
            <column name="fest_duur" not-null="true" />
        </property>
        <set name="ticketses" inverse="true">
            <key>
                <column name="fest_id" not-null="true" />
            </key>
            <one-to-many class="festivalOverview.Tickets" />
        </set>
        <set name="bandsperfestivals" inverse="true">
            <key>
                <column name="fest_id" not-null="true" />
            </key>
            <one-to-many class="festivalOverview.Bandsperfestival" />
        </set>
        <set name="campingses" inverse="false" table="campingsperfestival">
            <key>
                <column name="fest_id" not-null="true" />
            </key>
            <many-to-many entity-name="festivalOverview.Campings">
                <column name="camp_id" not-null="true" />
            </many-to-many>
        </set>
        <set name="tickettypesperfestivals" inverse="true">
            <key>
                <column name="fest_id" not-null="true" />
            </key>
            <one-to-many class="festivalOverview.Tickettypesperfestival" />
        </set>
    </class>
</hibernate-mapping>

我只是hibernate的初学者,真的不知道如何解决这个问题。非常感谢任何帮助!

【问题讨论】:

    标签: java hibernate jsp web-applications


    【解决方案1】:

    我假设 campingsperfestival 是 Campings 和 Festival 这两个类之间的连接表?您是否定义了这两个类及其映射?

    您遇到的错误是它无法创建 campingsperfestival,因为它指的是未定义为休眠类的 Campings。因此,请确保定义了 Campings,并且您的映射正确无误。

    如果您仍然不清楚,如果您展示您为露营和节日准备的 java/映射,我们可能会提供更多帮助。

    顺便说一句,如果这是您正在进行的一个新项目,我真的建议您使用基于注释的休眠类。您可能还会发现自己创建 hibernate 实体类而不是使用 netbeasns 是一种更有成效的学习体验——但这取决于个人喜好。

    【讨论】:

    • 所以我需要自己为 campingsperfestival 创建 .java 和 .hbm.xml 吗?为什么向导不这样做?
    • 我没有使用向导,您是否对表中的映射进行逆向工程?向导是否为露营和节日表创建了 java 和映射?
    • 是的,我是。向导已为 camping 和festivals 表创建了 java 和映射,但尚未为 campingsperfestival 表创建这些
    • 好的,那么你能检查一下连接表上的 fks 是否正确设置到两个表?另外,这可能是向导中的命令吗?向导是否尝试在处理露营之前生成连接表?
    • campingsperfestival 有 2 个属性:fest_id 和 camp_id。 fest_id 具有外键festivals.fest_id 和 camp_id 具有外键 campings.camp_id 所以应该是正确的。该向导非常简单,您无法选择映射哪些类,通常所有表都已映射但在我的情况下,campingsperfestival 没有被映射。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多