简体   繁体   中英

Hibernate exception

im new to hibernate! i have followed the netbeans tutorial on creating a hibernate enabled application. after sucessfully creating a database in mysql workbench i reversed engineered the pojos etc and then tried to run a simple query(from Course) and got the following

org.hibernate.MappingException: An association from the table coursemodule refers to an unmapped class: DAL.Module
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)

heres the generated class for Course

package DAL;
// Generated 02-May-2010 16:41:16 by Hibernate Tools 3.2.1.GA


import java.util.HashSet;
import java.util.Set;

/**
 * Course generated by hbm2java
 */
public class Course  implements java.io.Serializable {


 private int id;
 private String name;
 private Set<Module> modules = new HashSet<Module>(0);

public Course() {
}


public Course(int id, String name) {
    this.id = id;
    this.name = name;
}
public Course(int id, String name, Set<Module> modules) {
   this.id = id;
   this.name = name;
   this.modules = modules;
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
public Set<Module> getModules() {
    return this.modules;
}

public void setModules(Set<Module> modules) {
    this.modules = modules;
}
}

and its config file course.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 02-May-2010 16:41:16 by Hibernate Tools 3.2.1.GA --> <hibernate-mapping>
    <class name="DAL.Course" table="course" catalog="walkthrough">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" not-null="true" />
        </property>
        <set name="modules" inverse="false" table="coursemodule">
            <key>
                <column name="courseId" not-null="true" unique="true" />
            </key>
            <many-to-many entity-name="DAL.Module">
                <column name="moduleId" not-null="true" unique="true" />
            </many-to-many>
        </set>
    </class> </hibernate-mapping>

hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="Walkthrough"/>
  <table-filter match-name="walkthrough"/>
  <table-filter match-name="course"/>
  <table-filter match-name="module"/>
  <table-filter match-name="studentmodule"/>
  <table-filter match-name="attendee"/>
  <table-filter match-name="student"/>
  <table-filter match-name="coursemodule"/>
  <table-filter match-name="session"/>
  <table-filter match-name="test"/>
</hibernate-reverse-engineering>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Walkthrough</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="DAL/Student.hbm.xml"/>
    <mapping resource="DAL/Walkthrough.hbm.xml"/>
    <mapping resource="DAL/Test.hbm.xml"/>
    <mapping resource="DAL/Module.hbm.xml"/>
    <mapping resource="DAL/Session.hbm.xml"/>
    <mapping resource="DAL/Course.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

any ideas on why im getting this exception? ps. test is just a table with an id in it and is not related to anything. running "from Test" works

as requested heres the module.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 02-May-2010 16:41:16 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="DAL.Module" table="module" catalog="walkthrough">
        <id name="id" type="int">
            <column name="Id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" not-null="true" />
        </property>
        <property name="studyLevel" type="int">
            <column name="studyLevel" not-null="true" />
        </property>
        <property name="academicYear" type="int">
            <column name="academicYear" not-null="true" />
        </property>
        <set name="courses" inverse="false" table="coursemodule">
            <key>
                <column name="ModuleId" not-null="true" unique="true" />
            </key>
            <many-to-many entity-name="DAL.Course">
                <column name="CourseId" not-null="true" unique="true" />
            </many-to-many>
        </set>
        <set name="students" inverse="false" table="studentmodule">
            <key>
                <column name="moduleId" not-null="true" unique="true" />
            </key>
            <many-to-many entity-name="DAL.Student">
                <column name="studentId" not-null="true" unique="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

after running a unit test on the HibernateUtil class the test failed obtaining the session factory

getSessionFactory
03-May-2010 23:14:27 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
03-May-2010 23:14:27 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
03-May-2010 23:14:27 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
03-May-2010 23:14:27 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
03-May-2010 23:14:27 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
03-May-2010 23:14:28 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Student.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Walkthrough.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Test.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Module.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Session.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : DAL/Course.hbm.xml
03-May-2010 23:14:28 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Student -> student
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Student.modules -> studentmodule
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Student.sessions -> attendee
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Walkthrough -> walkthrough
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Test -> test
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Module -> module
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Module.courses -> coursemodule
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Module.students -> studentmodule
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Session -> session
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Session.students -> attendee
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: DAL.Course -> course
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: DAL.Course.modules -> coursemodule
03-May-2010 23:14:29 org.hibernate.cfg.HbmBinder bindCollectionSecondPass
INFO: Mapping collection: DAL.Walkthrough.sessions -> session
03-May-2010 23:14:29 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
03-May-2010 23:14:29 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
03-May-2010 23:14:29 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
03-May-2010 23:14:29 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
03-May-2010 23:14:29 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/Walkthrough
03-May-2010 23:14:29 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****}
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.45-community
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
03-May-2010 23:14:30 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
03-May-2010 23:14:30 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
03-May-2010 23:14:30 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
03-May-2010 23:14:30 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
03-May-2010 23:14:30 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
03-May-2010 23:14:30 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
03-May-2010 23:14:32 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured

The problem is not really Course , the problem is the many-to-many association between Course and Module , the later being not recognized as a mapped class. Do you see any noticeable error messages when you create the SessionFactory ? Nothing about Module ? Can you show Module.hbm.xml ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM