gpt4 book ai didi

java - Hibernate 生成 POJO

转载 作者:行者123 更新时间:2023-12-01 05:52:22 24 4
gpt4 key购买 nike

我是 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 association from the table studentmajor refers to an unmapped class: com.portfolio.hibernate.mappings.Student 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)

此错误是否有可能是由我对表格进行建模的方式引起的?任何帮助将不胜感激。

按照建议,我包含了学生和专业的 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>

最佳答案

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

希望对你有帮助

马克西姆

关于java - Hibernate 生成 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com