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