1
我用:休眠:從表中application_category的關聯是指未映射類:allin.beans1.Application
的NetBeans 8.x的
Mysql數據庫
analyseSI爲設計數據庫
作爲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>
這裏我休眠的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」:
在那裏我認爲錯誤是(在文件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);
謝謝您幫幫我。 我在我的文章中添加了數據庫腳本。 application_id在用戶表中不存在。 如果我將其更改爲用戶,我將不會在用戶和應用程序之間建立關聯...我需要將其設置爲應用程序 –