-->
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.  [ 2 posts ] 
Author Message
 Post subject: converting Map<Entity,Entity> to annotations
PostPosted: Tue Aug 25, 2009 9:28 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Hello,

am trying to convert an existing Hibernate mapping file to annotation. I did most of the stuff, but am blocked on a complex Map property.

First of all, here is the hibernate Mapping (working well)

Code:
<class name="Employee" ....>
....

      <map name="functionGroups" table="RH_EMP_FCT_GROUP_LINK" cascade="lock,merge">
          <key column="EMP_ID"/>
          <map-key-many-to-many column="FCT_GROUP_ID" class="be.rmi.intranet.db.users.FunctionGroup"/>
          <many-to-many column="FCT_ID" class="be.rmi.intranet.db.users.Function"/>
      </map>
</class>

<class name="FunctionGroupLink" table="RH_EMP_FCT_GROUP_LINK" lazy="true">
  <id name="linkId">
   <column name="LINK_ID" not-null="true" unique="true" />
      <generator class="sequence">
       <param name="sequence">RH_GENERIC_SEQ</param>
      </generator>
  </id>
   <many-to-one name="employee" column="emp_id" not-null="true"/>
   <many-to-one name="function" column="fct_id" not-null="true"/>
   <many-to-one name="functionGroup" column="fct_group_id" not-null="true"/>
</class>


The table behind this is supposed to be of the following schema
Code:
create table RH_EMP_FCT_GROUP_LINK (LINK_ID bigint not null, fct_id bigint not null, emp_id bigint not null, fct_group_id bigint not null, primary key (LINK_ID))

The idea behind this table is 3-sides relation-ship between employee, function and functionGroup. Exemple entries can be "John Smith", "manager", "Sales Manager", that says the Manager of Jhon Smith is the Sales Manager. The map serves this purpose, i provide as key the "Manager" FunctionGroup, and i get the SalesManager Function as value.

I translated the Employee part of the mapping as this:
Code:
    @ManyToMany()
    @JoinTable(name="RH_EMP_FCT_GROUP_LINK")
    @MapKey(columns=@Column(name="EMP_ID"))
    @MapKeyManyToMany(joinColumns=@JoinColumn(name="FCT_GROUP_ID"))
    public Map<FunctionGroup, Function> getFunctionGroups()

But for curious reason, the hbm2ddl creates the following table
Code:
create table RH_EMP_FCT_GROUP_LINK (LINK_ID bigint not null, fct_id bigint not null, emp_id bigint not null, fct_group_id bigint not null,

RH_EMPLOYEE_PERSON_ID bigint not null, functionGroups_FCT_ID bigint not null, primary key (RH_EMPLOYEE_PERSON_ID, FCT_GROUP_ID))


Am clearly missing how to tell hibernate, using annotation, what columns to use for the key and column, so if someone could correct me...


Top
 Profile  
 
 Post subject: Re: converting Map<Entity,Entity> to annotations
PostPosted: Tue Aug 25, 2009 10:31 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
This seems to work:
Code:
    @ManyToMany(fetch=FetchType.LAZY)
    @MapKeyManyToMany(joinColumns = {@JoinColumn(name = "FCT_GROUP_ID", nullable = false) })
    @JoinTable(name = "RH_EMP_FCT_GROUP_LINK", joinColumns = {@JoinColumn(name = "EMP_ID")}, inverseJoinColumns={@JoinColumn(name="FCT_ID") })


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