-->
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: Component bean w/in java.util.Map w/property as key
PostPosted: Mon Jun 18, 2007 9:48 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 2:46 pm
Posts: 32
Hello,

I have a bean, MyBean, with a property, myMapProp that is represented in Java as a Map<String,MyComponent>. (See code below.) The key to the map is one of the properties of the component (the code property). However, I have not been able to configure Hibernate without forcing me to have two columns with the same data in the table, one representing the key in the map and one representing the property in the component. I'd like to eliminate this duplicate data if possible.

Code:
public class MyBean
{
    private Map<String,MyComponent> myMapProp =
        new TreeMap<String,MyComponent>();
    public Map<String,MyComponent>getMyMapProp()
    { return myMapProp; }
    public void setMyMapProp(Map<String,MyComponent> myMapProp)
    { this.myMapProp = myMapProp; }
}

public class MyComponent
{
    private String code;
    private Integer fee;
    public String getCode() { return code; }
    public void setCode(String code)
    { this.code = code; }
    public Integer getFee() { return fee; }
    public void setFee(Integer fee)
    { this.fee = fee; }
}


The database table:
Code:
CREATE TABLE my_map_table
(
  my_bean_id integer NOT NULL,
  code character varying(12) NOT NULL,
  fee integer,
  code_key character varying(12) NOT NULL,
  CONSTRAINT my_map_table_pkey PRIMARY KEY (my_bean_id, code_key),
  CONSTRAINT fk_mmt_my_bean FOREIGN KEY (my_bean_id)
      REFERENCES my_bean (my_bean_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)


You can see above that there are two columns that contain the code property of the component. If I try to use the same column in the map-key and property tags I get a Hibernate error for duplicate column.

Hibernate version: 3.2.0

Mapping documents:
Code:
<hibernate-mapping>
<class name="MyBean" table="process_server">
    <id name="id" type="java.lang.Integer" unsaved-value="null" >
        <column name="my_bean_id" sql-type="int" not-null="true" />
        <generator class="native"/>
    </id>
    <map name="myMapProp" table="my_map_table" sort="natural">
        <key column="my_bean_id" />
        <map-key type="string" column="code_key" length="12" />
        <composite-element class="MyComponent">
            <property name="code" type="string">
                <column name="code" length="12" not-null="true" />
            </property>
            <property name="fee" type="int">
                <column name="fee" />
            </property>
        </composite-element>
    </map>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close(): N/A

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: PostgreSQL 8.2

The generated SQL (show_sql=true): N/A

Debug level Hibernate log excerpt: N/A


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 11:09 am 
Newbie

Joined: Sun Jan 29, 2006 9:27 pm
Posts: 10
Well, I did something like that a few months ago, it's currently working:

Code:
public class MDocType extends MBase {
  private String name;
  private Map<String, MDocTypeMetaData> docTypeMetaDataMap =
  new HashMap<String, MDocTypeMetaData>();

  public String getName()  {return name;}
  public void setName(String name) {this.name = name;}

  public Map<String, MDocTypeMetaData> getDocTypeMetaDataMap() {return docTypeMetaDataMap;}

  public void setDocTypeMetaDataMap(Map<String, MDocTypeMetaData> docTypeMetaDataMap) {this.docTypeMetaDataMap = docTypeMetaDataMap;}
}

public class MDocTypeMetaData extends MMetaData {
  private MDocType docTypeRef;

  public MDocType getDocTypeRef() {return docTypeRef;}
  public void setDocTypeRef(MDocType docTypeRef) {this.docTypeRef = docTypeRef;}
}


Code:
<class name="MDocType" table="t_wrk_doc_type" lazy="false">
<id name="id" type="int" unsaved-value="0">
  <generator class="increment" />
</id>
<map name="docTypeMetaDataMap" inverse="true" lazy="false" cascade="all-delete-orphan">
  <key column="doc_type_id" />
  <map-key column="okey" type="string" />
    <one-to-many class="MDocTypeMetaData" />
  </map>
</class>

<class name = "MDocTypeMetaData" table ="t_wrk_doc_type_meta_data" lazy = "false">
  <id name = "id" type = "int" unsaved-value = "0">
    <generator class = "increment"/>
  </id>
  <property name="key" column = "okey"/>
  <property  name="val" column = "oval"/>
  <many-to-one name="docTypeRef" class  = "MDocType" column = "doc_type_id"/>
</class>


I have the okey column only one time in the database. The main difference is that I'm using two mapped classes and not composite-element, may be it works for you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 11:32 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 2:46 pm
Posts: 32
piojosnos wrote:
Well, I did something like that a few months ago, it's currently working:

I have the okey column only one time in the database. The main difference is that I'm using two mapped classes and not composite-element, may be it works for you.


Thanks, I considered doing something like that. The main thing that discourages me from doing it is that there is the extra id column in this case. Perhaps that is better than repeating the string column. But I would like try to find a solution without any extra data; i.e. one that would match the schema I desired if it were created first.


Top
 Profile  
 
 Post subject: Composite mapping with key mapped in composite element too.
PostPosted: Tue Jun 19, 2007 11:33 am 
Newbie

Joined: Fri Apr 29, 2005 5:38 am
Posts: 3
I had the same problem with composite mapping. The only solution was, adding a seperate mapping for the composite element. But I don´t really understand, why composite mapping isn´t possible for this case. Is this a bug in Hibernate, or a feature for the next version or is there a Best-Practice?

Would be happy about any suggestions.

Regards,
Sam


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.