Hibernate生成了POJO

时间:2010-11-24 03:08:45

标签: java hibernate orm netbeans

我是hibernate的新手,但我一直在使用Netbeans和hibernate逆向工程工具从现有架构生成POJO。除了连接表之外,每个表的所有POJO都被创建得很好,我相信它应该是这样的,因为我可以看到在映射文件中创建了关联。但问题是当我尝试执行HBL查询时,我得到一个异常,我的某个连接表未映射。我知道配置文件中有一个映射条目,我唯一能想到的是我的数据模型不正确。我有一个相当大的ER模型,但问题表是

我有User,Student,Major和StudentMajor表。以下是我的创建声明

CREATE  TABLE IF NOT EXISTS `Portfolio`.`User` (
  `919Number` INT(11) NOT NULL ,
  `loginId` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(8) NOT NULL ,
  `userType` VARCHAR(10) NOT NULL ,
  `lastName` VARCHAR(45) NULL DEFAULT NULL ,
  `firstName` VARCHAR(45) NULL DEFAULT NULL ,
  PRIMARY KEY (`919Number`) );

CREATE  TABLE IF NOT EXISTS `Portfolio`.`Student` (
  `919Number` INT(11) NOT NULL ,
  `LEVL_CODE` VARCHAR(10) NOT NULL ,
  PRIMARY KEY (`919Number`) ,
  INDEX `919Number` (`919Number` ASC) ,
  CONSTRAINT `919Number`
    FOREIGN KEY (`919Number` )
    REFERENCES `Portfolio`.`User` (`919Number` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

CREATE  TABLE IF NOT EXISTS `Portfolio`.`Major` (
  `majorCode` VARCHAR(10) NOT NULL ,
  `majorDescription` VARCHAR(45) NULL DEFAULT NULL ,
  PRIMARY KEY (`majorCode`) );

CREATE  TABLE IF NOT EXISTS `Portfolio`.`StudentMajor` (
  `919Number` INT(11) NOT NULL ,
  `majorCode` VARCHAR(10) NOT NULL ,
  PRIMARY KEY (`919Number`, `majorCode`) ,
  INDEX `studentmajor_919Number` (`919Number` ASC) ,
  INDEX `studentmajor_majorCode` (`majorCode` ASC) ,
  CONSTRAINT `studentmajor_919Number`
    FOREIGN KEY (`919Number` )
    REFERENCES `Portfolio`.`Student` (`919Number` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `studentmajor_majorCode`
    FOREIGN KEY (`majorCode` )
    REFERENCES `Portfolio`.`Major` (`majorCode` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

这是我的hibernate配置文件

<?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/portfolio</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">jta</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
    <mapping resource="com/portfolio/hibernate/mappings/User.hbm.xml"/>
    <mapping resource="com/portfolio/hibernate/mappings/Student.hbm.xml"/>
    <mapping resource="com/portfolio/hibernate/mappings/Major.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

我正在使用Netbeans hibernate POJO生成工具。但是当我运行查询时,我得到以下异常:

  

org.hibernate.MappingException:An   表中的关联   studentmajor指的是未映射的   类:   com.portfolio.hibernate.mappings.Student     在   org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)     在   org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)     在   org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)     在   org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)

这个错误是否可能是由我对表格进行建模的?任何帮助将不胜感激。

根据建议,我包含了Student和Major的hbm.xml文件的内容。

学生:::

<hibernate-mapping>
    <class name="com.jopos.Student" table="student" catalog="portfolio">
        <id name="nineOneNumber" type="int">
            <column name="nineOneNumber" />
            <generator class="assigned" />
        </id>
        <many-to-one name="user" class="com.jopos.User" update="false" insert="false"  fetch="select">
            <column name="nineOneNumber" not-null="true" unique="true" />
        </many-to-one>
        <property name="levlCode" type="string">
           <column name="LEVL_CODE" length="10" not-null="true" />
    </property>
    <set name="faculties" inverse="false" table="advises">
        <key>
            <column name="studentnineOneNumber" not-null="true" />
        </key>
        <many-to-many entity-name="com.jopos.Faculty">
            <column name="facultynineOneNumber" not-null="true" />
        </many-to-many>
    </set>
    <set name="majors" inverse="false" table="studentmajor">
        <key>
            <column name="nineOneNumber" not-null="true" />
        </key>
        <many-to-many entity-name="com.jopos.Major">
            <column name="majorCode" length="10" not-null="true" />
        </many-to-many>
    </set>
    <set name="enrolls" inverse="true">
        <key>
            <column name="nineOneNumber" not-null="true" />
        </key>
        <one-to-many class="com.jopos.Enroll" />
    </set>
    </class>
    </hibernate-mapping>

主要:::

<hibernate-mapping>
   <class name="com.jopos.Major" table="major" catalog="portfolio">
       <id name="majorCode" type="string">
           <column name="majorCode" length="10" />
           <generator class="assigned" />
       </id>
       <property name="majorDescription" type="string">
           <column name="majorDescription" length="45" />
       </property>
       <set name="students" inverse="true" table="studentmajor">
          <key>
            <column name="majorCode" length="10" not-null="true" />
           </key>
           <many-to-many entity-name="com.jopos.Student">
              <column name="nineOneNumber" not-null="true" />
           </many-to-many>
       </set>
    </class>
   </hibernate-mapping>

2 个答案:

答案 0 :(得分:0)

我们遇到了类似的问题。当我们将MySql表从MyISAM更改为InnoDB时,它就解决了。

我希望它有所帮助

马克西姆

答案 1 :(得分:0)

您的异常所引用的Student类来自:

com.portfolio.hibernate.mappings.Student

您的Student.hbm.xml认可学生参与此方:

com.jopos.Student

解决参考问题。确保com.jopos.Major在其导入中引用com.jopos.Student。并确保表studentmajor及其映射存在。