java - hibernate 异常

标签 java mysql hibernate orm netbeans

我是 hibernate 新手!我已经按照 netbeans 教程创建了一个支持 hibernate 的应用程序。在 mysql workbench 中成功创建数据库后,我对 pojos 等进行了逆向工程,然后尝试运行一个简单的查询(来自类(class))并获得以下内容

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)

这是为 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;
}
}

及其配置文件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>

关于为什么我得到这个异常(exception)的任何想法? 附言。 test 只是一个带有 id 的表,与任何东西都不相关。运行“从测试”工作

这里是 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>

在 HibernateUtil 类上运行单元测试后,测试无法获取 session 工厂

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

最佳答案

问题不是真正的Course,问题是CourseModule之间的多对多关联,后者不是被识别为映射类。您在创建 SessionFactory 时是否看到任何明显的错误消息?没有关于 Module 的内容吗?你能显示 Module.hbm.xml 吗?

关于java - hibernate 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2754452/

相关文章:

java - Hibernate - 实体管理器工厂

java - 使用javafx向mysql数据库插入数据时出错

java - 通过 JSON 传递非数字/字母的字符串

java - 将java转换为c#代码的工具

php - Mysql子查询带百分比分割(难查询)

java - Hibernate (MySQL) 上区分大小写的查询

java - 测试 Lucene Query 是否匹配所有文档

php - Codeigniter 3 中的 mysql 查询

mysql - 无法在 WAMP 服务器上的 WordPress 本地主机中选择数据库错误

java - Quarkus 延迟初始化