0

I am working on a java JSP webapplication that uses the hibernate framework.

I am a total beginner with JSP/hibernate, and I can't seem to get hibernate to work. I have followed this tutorial: https://netbeans.org/kb/docs/web/hibernate-webapp.html It all worked. I used xampp with phpmyadmin, and I could execute HQL query's through the hibernate.cfg.xml file.

Then I was trying the same thing with the database I am using for the web application. Followed all steps and went through all the wizards. But I can't execute HQL query's.

the following error is given:

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)

I followed the Hibernate Mapping Files and POJOs from Database wizard, and it generated a .java and .hbm.xml file for all tables in the database except for one table: the 'campingsperfestival' table. I've done the wizard again a few times and started it all over but it still doesn't generate the .java and .hbm.xml file for the 'campingsperfestival' table.

The 'campingsperfestival' table is a table with 2 id's that both have a foreign key. There are festivals and campings that both have ID's, the 'campingsperfestival' matches those 2 id's in one table.

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>

Festivals.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>

I'm only a beginner with hibernate and really don't know how to solve this problem. Any help is greatly appreciated!

1 Answer 1

1

I assume campingsperfestival is a join table between two classes, Campings and Festival? Have you defined both these classes and their mapping?

The error you have there is saying it can't create campingsperfestival because it is referring to Campings, which is not defined as as hibernate class. So make sure Campings is defined and you have the mapping correct.

If you are still unclear we may be able to help more if you show the java/mappings you have for campings and festival.

As an aside, if this is a new project you are embarking on, I would really recommend using annotation based hibernate classes. You may also find it a more productive learning experience to create your hibernate entity classes yourself rather than using netbeasns - but that's down to personal preference.

Sign up to request clarification or add additional context in comments.

6 Comments

So i need to create the .java and .hbm.xml for the campingsperfestival myself? Why didnt the wizard do this?
I've not used the wizard, are you reverse engineering the mapping from the tables? Has the wizard created the java and mappings for the campings and festivals tables?
Yeah, I am. The wizard has created the java and mapping for camping and festivals tables, but has not created the ones for the campingsperfestival tables
OK, so can you check that the fks are properly set on the join table to the two tables? Also, is it maybe an order thing in the wizard? Is the wizard trying to generate the join table before processing campings?
campingsperfestival has 2 properties: fest_id and camp_id. fest_id has the foreign key festivals.fest_id and camp_id has the foreign key campings.camp_id so that should be right. The wizard is very simple, you can't choose which classes are mapped, normally all tables are mapped but in my case, campingsperfestival is not being mapped.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.