-->
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: mapping association tables with annotations
PostPosted: Tue Aug 03, 2010 5:05 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
Greetings All,

I've created a Eclipse/Java/Hibernate/MySQL/annotations/DAOs/HibernateUtil application roughly patterend after the canonical CaveatEmptor app. Up till now I've done 1:1 table:class mappings where each class:table has one pk of type Long, which works with the default DAO/DAOHibernate model.

So far so good.

Now I need to weave an ManyToMany association table into this fabric. This table consists of two columns, each corresponding to the pk of the corresponding table. The values of the two columns form the concatenated key of the association table.

googling turned this up:
[url]
http://boris.kirzner.info/blog/archives ... osite-key/
[/url]
I've built my @Entity and @Embeddable classes based on this model.

But now I'm stuck. What else must I do?
- must the @Embeddable class be listed in the hibernate.cfg.xml file ?
- assuming the @Entity class must be listed in the hibernate.cfg.xml file,
I don't think it will work with the HibernateUtil/DAO method because it doesn't have a key of Long.
If this is a 'special case' must I revert to creating a *.hbm.xml for it?

All suggestions/advice/hints/examples/constructive criticisms are welcome.

TIA,

Still-learning Steve


Top
 Profile  
 
 Post subject: Re: mapping association tables with annotations
PostPosted: Thu Aug 05, 2010 3:40 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
I assume you want to add other attributes to the association table? The blog uses an example where that is not the case. Thus it's not such a good example.

If you just want to map a ManyToMany relation you do not need to map the association entity explicitly as in the example.

Otherwise:
Quote:
But now I'm stuck. What else must I do?
- must the @Embeddable class be listed in the hibernate.cfg.xml file ?

yes
Quote:
- assuming the @Entity class must be listed in the hibernate.cfg.xml file,

yes
Quote:
I don't think it will work with the HibernateUtil/DAO method because it doesn't have a key of Long.
If this is a 'special case' must I revert to creating a *.hbm.xml for it?

what method are you referring to? Long as Id should be fine AFAIK.


Top
 Profile  
 
 Post subject: Re: mapping association tables with annotations
PostPosted: Thu Aug 19, 2010 8:29 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
Thank you for your reply!

My problem turned out to be my incomplete knowledge of Hibernate and to use correct terminology.

Rewording my problem statement:

I have numerous 1:1 class:data-table mappings.
In my database I have numerous association-tables linking the various data-tables.
I need to incorporate these association tables into my app somehow

Solution was easy ( after RT*Ming a bit more ):

Association tables are commonly refered to as join tables
for a given association table:
in each class referenced by the association table, add a field corresponding to the OTHER class
and map using @JoinTable

Example:

given an association table District_User_Relationship containing two fields
id_user bigint,
id_district bigint
with a many:many relationship

and classes User and District mapped to data tables Users and Districts respectively

Code:
@Entity
@Table(name="Districts")
public class District
{
    @Id
    private Long id;
    .....

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="District_User_Relationship",
          joinColumns={ @JoinColumn(name="id_district") },
          inverseJoinColumns={ @JoinColumn(name="id_user") }
    )
    private Set<User> users;


and

Code:
@Entity
@Table(name="Users")
public class User
{
    @Id
    private Long id;
    .....

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="District_User_Relationship",
          joinColumns={ @JoinColumn(name="id_user") },
          inverseJoinColumns={ @JoinColumn(name="id_district") }
    )
    private Set<District> districts;


Important note: you do NOT add any references to the association table in any Hibernate stuff ( hibernate.cfg.xml, HibernateUtils, create any DAO stuff, etc)

Hope this helps others.

Thx,

Still-learning Steve


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.