I use :
- netbeans 8.x
- Mysql database
- analyseSI for design database
- Hibernate as ORM.
I try to associate 2 tables : User and Application.
I use an association table named : User_Application.
Here my User.hmb.xml
Code:
<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>
Here my Application.hmb.xml :
Code:
<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>
Here my hibernate conf :
Code:
<?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>
I have this error when runing a simple command like "From User" :
Code:
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)
The line where i think the bug is (in file User.hmb.xml) :
Code:
<many-to-many entity-name="allin.beans1.Application">
<column name="category_id" not-null="true" />
</many-to-many>
It's like it didn't find the file Application.hbm.xml in User.hmb.xml ... I forget a flag or something ?
thanks for you help guys !
EDIT :
Database script :
Code:
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);