-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-many
PostPosted: Thu May 20, 2004 7:46 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hi,

I have the followings tables:

-------------------------------------------
Company:
-------------------------------------------
company_id varchar2(18) PK
name varchar2(50)
-------------------------------------------



-------------------------------------------
User:
-------------------------------------------
user_id varchar2(10) PK
name varchar2(50)
-------------------------------------------

A company can to have more than one contact user and
the user can be contact user of some Company.

So, the following table was created:

-------------------------------------------
company_x_contact_user:
-------------------------------------------
company_id varchar2(18) pk and fk
user_id varchar2(10) pk and FK
other_column varchar2(10)

-------------------------------------------

I can to create a many-to-many relationship, where
Company have a List of user and User have List of company,
but how can I retrieve the other_column????

thanks!!!

[]

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 9:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
your association table is not a simple one since it handle extra info (other column)

you may use composite element

http://www.hibernate.org/hib_docs/refer ... nents.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 9:58 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
OK.. thanks!!!

and if the other_column is PK too????

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 9:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can add what you want in a component, even many-to-one

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 21, 2004 11:50 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hi,

I

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 21, 2004 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
When asking questions, give at least:
the Hibernate version
your mapping documents
the Java code between sessionFactory.openSession() and session.close()
the full stack trace of any exception that occurs
the name and version of the database you are using
a debug level Hibernate log excerpt is also appreciated

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 21, 2004 12:47 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
OPssss!!!! Ok, ok...

Version Hibernate: 2.0.2
SGDB: Oracle 8.1.7

The mapping files are:

Code:

<!-- User -->
<hibernate-mapping>
    <class name="User" table="user">
      <id name="cpf" column="cpf">
         <generator class="assigned"/>
      </id>       
        <property name="name" column="nome"/>   
        <bag name="contactCompanies" table="empresa_x_contato" inverse="true" lazy="true">
         <key column="cpf_contato"/>         
         <many-to-many class="Company" column="cnpj"/>
        </bag>       
        <bag name="templates" table="perfil_x_usuario" inverse="true" lazy="false" cascade="save-update">
         <key column="cpf"/>
         <many-to-many class="Template" column="id_perfil"/>
        </bag>       
    </class>
</hibernate-mapping>

<!-- Company -->

<hibernate-mapping>
    <class name="Company" table="company">
      <id name="cnpj" column="cnpj">
         <generator class="assigned"/>
      </id>
      <property name="name" column="nome"/>
        <bag name="templates" inverse="true" lazy="false">
         <key column="cnpj"/>
         <one-to-many class="Template"/>
        </bag>
      <bag name="companies" table="empresa_x_empresa" inverse="true" lazy="true">
         <key column="cnpj"/>
         <many-to-many class="Company" column="cnpj"/>
        </bag>   
      <bag name="companyParents" table="empresa_x_empresa" inverse="true" lazy="false" cascade="save-update">
         <key column="cnpj_pai"/>
         <many-to-many class="Company" column="cnpj"/>
        </bag>
      <bag name="contactUsers" table="empresa_x_contato" inverse="true" lazy="false" cascade="save-update">
         <key column="cnpj"/>
         <many-to-many class="User" column="cpf_contato"/>
        </bag>               
    </class>

</hibernate-mapping>


<!-- Template -->

<hibernate-mapping>
    <class name="Template" table="template">
      <id name="idTemplate" column="id_template">
         <generator class="native">
            <param name="sequence">seq_template</param>
         </generator>
      </id>       
        <property name="name" column="nome"/>
      <many-to-one name="company" column="cnpj"
         class="Company"/>         
        <bag name="users" table="perfil_x_usuario" inverse="true" lazy="true">
         <key column="id_perfil"/>
         <many-to-many class="User" column="cpf"/>
        </bag>               
      <bag name="templates" inverse="true" lazy="true">
         <key column="id_perfil_pai"/>
         <one-to-many class="Template"/>
        </bag>       
    </class>

</hibernate-mapping>




The relationship must be:


-An User can to have some Templates and the templates can to have
some Users.
-An User can to have some contactCompany (Company) and Company
can to have some contactUser (Company have some contact users)
-A Company can to have some companyParents (Matrix Company)
an can to have some branch offices (Object Company too).

I try to do this:


Code:
String query = "from User u";
session.createQuery(query);


And It occurs the following (debug eclipse):

Code:

e=PropertyAccessException
   cause=NullPointerException
   cause=PropertyAccessException
   .
   .
   detailMessage="exception getting property value with CGLIB"
   persistentClass=Class(Company)
   propertyName="?"
   stackTrace=null


I didn

_________________
Tads


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.