-->
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.  [ 4 posts ] 
Author Message
 Post subject: General question & Bidirectional with JoinTable
PostPosted: Tue Jun 01, 2010 10:37 am 
Newbie

Joined: Tue Jun 01, 2010 10:18 am
Posts: 15
Hello!

I am a newbie with Hibernate and I am using the latest version of Hibernate in my Flex/BlazeDS/J2EE project.
Because I use BlazeDS, which does not support Hibernate's Lazy Loading I have to join a table.

I have this example:
There is a ContactGroup which is in a 1:n relation to Contact, So one ContactGroup has many Contacts and one Contact is in one ContactGroup.
In class Contact.java there is not an annotation like "mappedBy", "ManyToOne", ...
In class ContactGroup.java, there is

Code:
   @OneToMany(fetch=FetchType.EAGER)
   @Cascade({org.hibernate.annotations.CascadeType.ALL})
   @JoinTable(name="Group_Contact",joinColumns=
   {
      @JoinColumn(name="groupId")
   }
   )
   @Fetch(FetchMode.SUBSELECT)
   private List<Contact> m_contacts;
   public List<Contact> getContacts()
   {
      if (m_contacts == null)
         m_contacts = new ArrayList<Contact>();
      return m_contacts;
   }
   public void setContacts(List<Contact> contacts) { m_contacts = contacts; }


Here I do not understand what is done exactly. A table between Contact and ContactGroup called "Group_Contact" will be created (I only need this table,
because of the non supported Lazy Loading of BlazeDS (I know that I can solve this, using Frameworks like Cairngorm, or Spring, but I do not want to use them, because I am a newbie and
I have problems to understand Hibernate, and I do not want to use x other Frameworks, which I will also a newbie in and there will be other problems)).
In this table, there must be the ID of the Group and the ID of the Contact, right?

Now it is possible to get all Contacts from the class ContactGroup. But how can I get from a Contact the linked ContactGroup? I do not see that this is bidirectional, because in Contact.java, there is no attribute like
private ContactGroup theContactGroup;
How can I get this bidirection in my case?

Best Regards and thanks a lot in advance, PHANTOMIAS


Top
 Profile  
 
 Post subject: Re: General question & Bidirectional with JoinTable
PostPosted: Wed Jun 02, 2010 3:03 am 
Newbie

Joined: Tue Jun 01, 2010 10:18 am
Posts: 15
Hm, nobody an idea?


Top
 Profile  
 
 Post subject: Re: General question & Bidirectional with JoinTable
PostPosted: Wed Jun 02, 2010 11:56 pm 
Newbie

Joined: Mon Apr 06, 2009 9:55 pm
Posts: 12
Hi,

Why don't you add the @ManyToOne association to the Contact class and make it bidirectional?

Something like this:

Code:
@Entity
@Table(name = "Group")
class ContactGroup {
    @Id
    Long groupId;

   @OneToMany(fetch=FetchType.EAGER, mappedBy = "contactGroup")
   @Cascade({org.hibernate.annotations.CascadeType.ALL})
   private List<Contact> m_contacts;     
}

@Entity
@Table(name = "Group_Contact")
class Contact {
    @Id
    Long contactId;

    @JoinColumn(name = "groupId", referencedColumnName = "groupId")
    @ManyToOne(fetch = FetchType.EAGER)   
    ContactGroup contactGroup;
}


Everything is eager, as you required.

You don't need @Fetch(FetchMode.SUBSELECT) as you don't use LAZY. Right?

Hope it helps,
Anton


Top
 Profile  
 
 Post subject: Re: General question & Bidirectional with JoinTable
PostPosted: Thu Jun 03, 2010 8:39 am 
Newbie

Joined: Tue Jun 01, 2010 10:18 am
Posts: 15
In my "Java <- BlazeDS -> Flex" it tells me, that I need to use a table in between the two entities (with @jointable) otherwise there will be errors because of BlazeDS. So I need in a 1:n relation 3 tables (normally I would only use two tables).
Not a very nice behavior...

Best Regards.


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