-->
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.  [ 6 posts ] 
Author Message
 Post subject: need extrafields from many-to-many relation
PostPosted: Thu Mar 02, 2006 1:19 pm 
Newbie

Joined: Thu Mar 02, 2006 1:04 pm
Posts: 4
Hi all!

I am a new hibernate user, and i am trying to get inside of the hibernate complex world.
Currently i need to do the following. I have 3 tables defined: player, team and player_team_assoc.
The third table (player_team_assoc) was defined to satisfy the needing of have a many-to-many relationship between team and player tables.

Basically the tables structure is like this:

Player
--------
(PK)player_id
player_name

Team
------
(PK)team_id
team_name

player_team_assoc
-----------------------
(PK)team_id
(PK)player_id
is_team_manager


My problem arrives when i try to set the 'is_team_manager' field of the 'player_team_assoc' table to a Player java object in the player hiberante definition.
I have tried everything, i tried to put a composite-element, element, keys, etc, but i cannot find the way to say that i want to load the 'is_team_manager' field to a player object.

In the next lines i attach the bag xml definition of Player and Team elements:

Player definition
-------------------

<bag name="collectionTeams" table="player_team_assoc" inverse="true" cascade="all" lazy="true">
<key column="player_id" />
<many-to-many column="team_id" class="slm.persistence.dao.team.Team" />
</bag>


I guess that in the previous definition i should put something inside the bag tags indicating that i want to set the 'is_team_manager' field to a property of the Player object.


Team definition
------------------

<bag name="collectionPlayers" table="player_team_assoc" cascade="all" lazy="true">
<key column="team_id" />
<many-to-many column="player_id" class="slm.persistence.dao.player.Player" />
</bag>


The hibernate version iam using is 3.1 and the database is MySql-4.0.16



Since now, THX for any help!! :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 2:34 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
What you can do is use a bag of composite elements in your team object :

Code:
<bag name="collectionPlayers" table="player_team_assoc" cascade="all" lazy="true">
  <key column="team_id" />
  <composite-element class="TeamMember">
    <many-to-many column="player_id" class="slm.persistence.dao.player.Player" />
    <property name="isTeamManager"/>
  </composite-element>
</bag>


Where TeamMember is a class that has a Player and a isTeamManager attributes.

An other (better ?) way to to this would be to add a teamManager attribute to your Team object (If a team has exactly one manager)


Top
 Profile  
 
 Post subject: rel
PostPosted: Thu Mar 02, 2006 3:04 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
It is called ternary relationships, couple of llinks:
http://www.hibernate.org/hib_docs/v3/re ... ns-ternary
http://ndpsoftware.com/HibernateMappingCheatSheet.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 3:30 pm 
Newbie

Joined: Thu Mar 02, 2006 1:04 pm
Posts: 4
the-gtm wrote:
What you can do is use a bag of composite elements in your team object :

Code:
<bag name="collectionPlayers" table="player_team_assoc" cascade="all" lazy="true">
  <key column="team_id" />
  <composite-element class="TeamMember">
    <many-to-many column="player_id" class="slm.persistence.dao.player.Player" />
    <property name="isTeamManager"/>
  </composite-element>
</bag>


Where TeamMember is a class that has a Player and a isTeamManager attributes.

An other (better ?) way to to this would be to add a teamManager attribute to your Team object (If a team has exactly one manager)


I tried the solution of put that code on the Player xml definition, and add an attribute called 'isTeamManager' to it, but when i try to execute a simple query to load a player it throws me this exception:

Code:
Caused by: org.xml.sax.SAXParseException:
The "composite-element" dont allow "many-to-many" in this place.
   at org.apache.crimson.parser.Parser2.error(Parser2.java:3317)



And the Player definition looks like this now:

Code:
        <bag name="collectionTeams" table="player_team_assoc" inverse="true" cascade="all" lazy="true">
         <key column="player_id" />
         <composite-element class="slm.persistence.dao.player.Player">
            <many-to-many column="team_id" class="slm.persistence.dao.team.Team" />
            <property name="is_team_manager"/>
         </composite-element>
      </bag>


I guess that bag doesnt allow that order or something like that... i change that declaration through all the bag tag :) but it didnt work.

MORE HELPPPPPPPPP d:

Bytes!

Pablo


Top
 Profile  
 
 Post subject: should be
PostPosted: Thu Mar 02, 2006 6:42 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
should be
<bag name="collectionPlayers" table="player_team_assoc" cascade="all" lazy="true">
<key column="team_id" />
<composite-element class="TeamMember">
<many-to-one column="player_id" />
<property name="teamManager"/>
</composite-element>
</bag>

Provided that class TeamMember has property teamManager with setter 'setTeamManager(boolean v )' and getter 'boolean isTeamManager()';

Player itself should not have 'teamManager' property.

Sorting is supported via order-by attribute
http://www.hibernate.org/hib_docs/v3/re ... tions.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: should be
PostPosted: Thu Mar 02, 2006 8:17 pm 
Newbie

Joined: Thu Mar 02, 2006 1:04 pm
Posts: 4
kgignatyev wrote:
should be
<bag name="collectionPlayers" table="player_team_assoc" cascade="all" lazy="true">
<key column="team_id" />
<composite-element class="TeamMember">
<many-to-one column="player_id" />
<property name="teamManager"/>
</composite-element>
</bag>

Provided that class TeamMember has property teamManager with setter 'setTeamManager(boolean v )' and getter 'boolean isTeamManager()';

Player itself should not have 'teamManager' property.

Sorting is supported via order-by attribute
http://www.hibernate.org/hib_docs/v3/re ... tions.html


Thanks for the help... finally the error was found. It was a severe error, because it was a conceptual error. It was impossible to have that property on the player object itself, because if that happen it means that the player is the team manager of all the elements of the collection of teams.
Thanks for everything!!!

Hope to help you sometime :D

bytes

Pablo


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