Hallo!
Ich habe folgende SQL Struktur:
Code:
--------------
| project
--------------
| projectID
| ...
--------------
-------------------
| user_project
-----------------
| userID
| projectID
| roleID
-------------------
--------------------
| user
--------------------
| userID
| userName
| ....
--------------------
------------------
| role
------------------
| roleID
| roleName
-----------------
Hier die das
hibernate.cfg.xml file:
Code:
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="connection.username">root</property>
<property name="connection.password">*******</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/mySQL</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- mapping files -->
<mapping resource="com/**/***/persistent/Project.hbm.xml"/>
<mapping resource="com/**/***/persistent/Role.hbm.xml"/>
<mapping resource="com/**/***/persistent/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
und hier die hbm.xml files:
Project.hbm.xmlCode:
<hibernate-mapping
>
<class
name="com.**.***.persistent.Project"
table="project"
>
<cache usage="read-write" />
<id
name="projectID"
column="projectID"
type="java.lang.Long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Project.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="projectName"
type="string"
update="true"
insert="true"
column="projectName"
/>
<property
name="date"
type="timestamp"
update="true"
insert="true"
column="date"
/>
<many-to-one
name="author"
class="com.**.***.persistent.User"
cascade="save-update"
outer-join="true"
update="true"
insert="true"
column="userID"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Project.xml
containing the additional properties and place it in your merge dir.
-->
</class>
Role.hbm.xmlCode:
<class
name="com.**.***.persistent.Role"
table="role"
>
<cache usage="read-write" />
<id
name="roleID"
column="roleID"
type="java.lang.Long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Role.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="roleName"
type="string"
update="true"
insert="true"
column="roleName"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Role.xml
containing the additional properties and place it in your merge dir.
-->
</class>
User.hbm.xmlCode:
<class
name="com.**.***.persistent.User"
table="user"
>
<cache usage="read-write" />
<id
name="userID"
column="userID"
type="java.lang.Long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="userName"
type="string"
update="true"
insert="true"
column="userName"
/>
<bag
name="projectList"
table="user_project"
lazy="false"
cascade="save-update"
order-by="projectID"
>
<key
column="userID"
>
</key>
<many-to-many
class="com.**.***.persistent.Project"
column="projectID"
outer-join="true"
/>
</bag>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->
</class>
Hier der Java Code, wo ich versuche einfach einen Wert aus der Datenbank zu bekommen:
Code:
....
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String sqlQuery = "SELECT roleID FROM user_project WHERE userID = "+user.getUserID()+" AND projectID = "+pID;
Query query = session.createQuery( sqlQuery );
System.out.println( "Query: "+query );
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
...
Hier die Exception:
Code:
org.hibernate.hql.ast.QuerySyntaxException: user_project is not mapped. [SELECT roleID FROM user_project WHERE userID = 1 AND projectID = 1]
...
Caused by: user_project is not mapped.
...64 more!
Die Frage ist, ob ich ueberhaupt die Tabelle '
user_project' in der
hibernate.cfg.xml mappen muss oder nicht? Ich meine ja,
ich versuche doch nur auf Werte von der Tabelle drauf zu zugreifen, die fuer andere Klassen nicht relevant sind.
Ist das ueberhaupt moeglich? Wenn jemand aber eine bessere Idee hat, dann bin ich fuer jeden Vorschlag sehr dankbar.
Gruss,
Dariusz