-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Einfach ein Wert aus der DB mittels Hibernate lesen.
PostPosted: Thu Nov 30, 2006 9:54 am 
Newbie

Joined: Tue Nov 28, 2006 12:46 pm
Posts: 6
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.xml


Code:
<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.xml


Code:
<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.xml


Code:
<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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 5:13 am 
Newbie

Joined: Tue Nov 28, 2006 12:46 pm
Posts: 6
Vielleicht ist die Frage zu bloed, aber ich komme einfach nicht weiter. Ganz simple gefragt.

    Ist es moeglich eine "Select" Anweisung an die Datenbank zu schicken, ohne das diese Tabelle in meiner hibernate.cfg.xml Datei besteht???



Bin froh ueber jeden Hinweis!

Danke & Gruss,
Dariusz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 5:48 am 
Newbie

Joined: Tue Nov 28, 2006 12:46 pm
Posts: 6
OK, hat sich erledigt. Ich haette einfach diese Methode (session.createSQLQuery())benutzen muessen, damit ich als Result keine Objekte, sondern einfach nur ein String/Int zurueck bekomme:

Code:
String sqlQuery = "SELECT roleID FROM user_project WHERE userID = "+user.getUserID()+" AND projectID = "+pID;
Query query = session.createSQLQuery( sqlQuery );
Object role = ( Object ) query.uniqueResult();
System.out.println("======================");
System.out.println( "Result: "+role.toString() );


Dariusz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.