我用:
netbeans 8.x
Mysql数据库
设计数据库的analyseSI
Hibernate as ORM。
我尝试关联2个表:用户和应用程序。
我使用名为User_Application的关联表。
这是我的User.hmb.xml
<hibernate-mapping>
<class name="allin.beans1.User" table="user" catalog="allin" optimistic-lock="version">
<id name="userId" type="java.lang.Integer">
<column name="user_id" />
<generator class="identity" />
</id>
<property name="username" type="string">
<column name="username" length="254" />
</property>
<property name="password" type="string">
<column name="password" length="254" />
</property>
<property name="email" type="string">
<column name="email" length="254" />
</property>
<property name="firstName" type="string">
<column name="first_name" length="254" />
</property>
<property name="lastName" type="string">
<column name="last_name" length="254" />
</property>
<property name="age" type="java.lang.Short">
<column name="age" />
</property>
<property name="phone" type="java.lang.Integer">
<column name="phone" />
</property>
<property name="userCreation" type="timestamp">
<column name="user_creation" length="19" />
</property>
<property name="userLastReq" type="string">
<column name="user_last_req" length="65535" />
</property>
<property name="userError" type="string">
<column name="user_error" length="65535" />
</property>
<set name="applications" table="user_application" inverse="true" lazy="true" fetch="select">
<key>
<column name="user_id" not-null="true" />
</key>
<many-to-many entity-name="allin.beans1.Application">
<column name="application_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
这是我的Application.hmb.xml:
<hibernate-mapping>
<class name="allin.beans1.Application" table="application" catalog="allin" optimistic-lock="version">
<id name="applicationId" type="java.lang.Integer">
<column name="application_id" />
<generator class="identity" />
</id>
<property name="applicationName" type="string">
<column name="application_name" length="254" />
</property>
<property name="applicationDescription" type="string">
<column name="application_description" length="65535" />
</property>
<property name="applicationCreation" type="timestamp">
<column name="application_creation" length="19" />
</property>
<property name="applicationUpdate" type="timestamp">
<column name="application_update" length="19" />
</property>
<property name="applicationLastReq" type="string">
<column name="application_last_req" length="65535" />
</property>
<set name="users" table="user_application" inverse="true" lazy="true" fetch="select">
<key>
<column name="application_id" not-null="true" />
</key>
<many-to-many entity-name="allin.beans1.User">
<column name="user_id" not-null="true" />
</many-to-many>
</set>
<set name="categories" table="application_category" inverse="false" lazy="true" fetch="select">
<key>
<column name="application_id" not-null="true" />
</key>
<many-to-many entity-name="allin.beans1.Category">
<column name="category_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
这是我的hibernate conf:
<?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/allin?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<mapping resource="allin/beans1/Category.hbm.xml"/>
<mapping resource="allin/beans1/Application.hbm.xml"/>
<mapping resource="allin/beans1/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
运行像“From User”这样的简单命令时出现此错误:
org.hibernate.MappingException: An association from the table user_application refers to an unmapped class: allin.beans1.Application
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1805)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
我认为该错误的行(在文件User.hmb.xml中):
<many-to-many entity-name="allin.beans1.Application">
<column name="category_id" not-null="true" />
</many-to-many>
就好像它没有在User.hmb.xml中找到Application.hbm.xml文件......我忘记了一个标志或什么东西?
谢谢你帮帮我们!
编辑:
数据库脚本:
DROP TABLE IF EXISTS User ; CREATE TABLE User (user_id INT AUTO_INCREMENT NOT NULL, username VARCHAR(254), password VARCHAR(254), email VARCHAR(254), first_name VARCHAR(254), last_name VARCHAR(254), age SMALLINT, phone INT, user_creation DATETIME, user_last_req TEXT, PRIMARY KEY (user_id) ) ENGINE=InnoDB; DROP TABLE IF EXISTS Application ; CREATE TABLE Application (application_id INT AUTO_INCREMENT NOT NULL, application_name VARCHAR(254), application_description TEXT, application_creation DATETIME, application_update DATETIME, application_last_req TEXT, PRIMARY KEY (application_id) ) ENGINE=InnoDB; DROP TABLE IF EXISTS Category ; CREATE TABLE Category (category_id INT AUTO_INCREMENT NOT NULL, category_name VARCHAR(254), category_description TEXT, category_last_req TEXT, PRIMARY KEY (category_id) ) ENGINE=InnoDB; DROP TABLE IF EXISTS User_Application ; CREATE TABLE User_Application (user_id INT AUTO_INCREMENT NOT NULL, application_id INT NOT NULL, PRIMARY KEY (user_id, application_id) ) ENGINE=InnoDB; DROP TABLE IF EXISTS Application_Category ; CREATE TABLE Application_Category (application_id INT AUTO_INCREMENT NOT NULL, category_id INT NOT NULL, PRIMARY KEY (application_id, category_id) ) ENGINE=InnoDB; ALTER TABLE User_Application ADD CONSTRAINT FK_User_Application_user_id FOREIGN KEY (user_id) REFERENCES User (user_id); ALTER TABLE User_Application ADD CONSTRAINT FK_User_Application_application_id FOREIGN KEY (application_id) REFERENCES Application (application_id); ALTER TABLE Application_Category ADD CONSTRAINT FK_Application_Category_application_id FOREIGN KEY (application_id) REFERENCES Application (application_id); ALTER TABLE Application_Category ADD CONSTRAINT FK_Application_Category_category_id FOREIGN KEY (category_id) REFERENCES Category (category_id);
答案 0 :(得分:2)
可能 application_id存在于用户表中,但您提到了entity-name,因为应用程序将其更改为用户
<set name="applications" table="user_application" inverse="true" lazy="true" fetch="select">
<key>
<column name="user_id" not-null="true" />
</key>
<many-to-many entity-name="**allin.beans1.User**">
<column name="application_id" not-null="true" />
</many-to-many>
</set>