-->
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.  [ 1 post ] 
Author Message
 Post subject: Multiple ManyToMany-relationships. Composite key in join tab
PostPosted: Fri Sep 19, 2008 5:44 am 
Newbie

Joined: Thu Sep 11, 2008 10:19 am
Posts: 6
Hi,

it seems that I try something that anyone tried before. At least Google shows that there were nobody trying this ;)

I have an entity with three many-to-may-relationships. The join tables only contain the ids of the two joined tables.

Code:
COMPONENT {
    COMPONENT_ID(number),
    ...
}

LANGUAGE {
    LANGUAGE_ID(number),
    ...
}

PLATFORM {
    PLATFORM_ID(number),
    ...
}

T_RELEASE {
    RELEASE_ID(number),
    ...
}

T_RELEASE_COMPONENT {
    T_RELEASE_RELEASE_ID(number),
    COMPONENT_ID_COMPONENT_ID(number)
}

RELEASE_LANGUAGE {
    T_RELEASE_RELEASE_ID(number),
    LANGUAGE_ID_LANGUAGE_ID(number)
}

RELEASE_PLATFORM {
    T_RELEASE_RELEASE_ID(number),
    PLATFORM_ID_PLATFORM_ID(number)
}


I use Hibernate Annotations, so I tried to establish those relationships this way:

Code:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_COMPONENT",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="COMPONENT_ID_COMPONENT_ID")
)
public List<Component> getComponents() {
  return components;
}

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_LANGUAGE",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="LANGUAGE_ID_LANGUAGE_ID")
)
public List<Language> getLanguages() {
  return languages;
}

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_PLATFORM",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="PLATFORM_ID_PLATFORM_ID")
)
public List<Platform> getPlatforms() {
  return platforms;
}


Hibernate complained that it can't fetch multiple bags. I asked Google for help and found Eyal Lupus blog post on this. Since I don't want to use lazy fetching or introduce an index column I tried to use @CollectionId in order to switch from bag semantics to idbag.

So my class turned to:
Code:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_COMPONENT",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="COMPONENT_ID_COMPONENT_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
    columns = {
      @Column(name = "T_RELEASE_RELEASE_ID"),
      @Column(name = "COMPONENT_ID_COMPONENT_ID")
    })
public List<Component> getComponents() {
  return components;
}

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_LANGUAGE",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="LANGUAGE_ID_LANGUAGE_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
    columns = {
      @Column(name = "T_RELEASE_RELEASE_ID"),
      @Column(name = "LANGUAGE_ID_LANGUAGE_ID")
    })
public List<Language> getLanguages() {
  return languages;
}

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
    name="T_RELEASE_PLATFORM",
    joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
    inverseJoinColumns=@JoinColumn(name="PLATFORM_ID_PLATFORM_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
    columns = {
      @Column(name = "T_RELEASE_RELEASE_ID"),
      @Column(name = "PLATFORM_ID_PLATFORM_ID")
    })
public List<Platform> getPlatforms() {
  return platforms;
}


I thought the columns attribute of @ColumnId specifies the primary key of the join table. And since the primary key is a composite of both columns I included them both. But deploying this results in a complaint of Hibernate that "T_RELEASE_RELEASE_ID" is used multiple times. Sure it's used multiple times, but on different tables?!?

Since I use JSF for representation I really would like to use java.util.List and not a Set or whatever. And since this table structure is fixed I don't want to introduce an additional column. Is there any way to solve this?

Thanks in advance
Newlukai


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

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.