0 Replies Latest reply on Feb 5, 2010 8:39 AM by Jan Erik Robertsen

    Replacing *.hbm.xml files with @Annotations.

    Jan Erik Robertsen Newbie

      I have three java classes that are persisted using hibernate. I want to switch from using *.hbm.xml files to annotations on these classes. However the db has additional tables that are created by tags in hbm.xml. Is there a way to create these extra tables using annotations without having to create additional java classes.

       


      One of the java classes that result in two tables in db:

       

      public class Attribute {
           private Long id;

       

           private String name;

       

           private String value;

       

           private VocabularyElement parent;

       

           // This map will result in its own table using the hbm.xml file listed below.
           // What annotations can I use instead of the hbm.xml file?
           private Map<String, String> extensionAttributes = new HashMap<String, String>();

       

           // Constructors, getters, setters
           // ...
      }

       

      hbm.xml file for the Attribute class:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC
           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

       

      <hibernate-mapping>
           <class name="com.tracetracker.mds.adm.domain.Attribute">
               
                <id name="id" access="field">
                     <generator class="native"/>
                </id>
               
                <natural-id>
                     <property name="name" access="field"/>
                     <many-to-one name="parent" class="com.tracetracker.mds.adm.domain.VocabularyElement"/>         
                </natural-id>
               
                <property name="value" type="text"/>
               
                <!-- This tag creates the extra table. -->
                <map name="extensionAttributes" table="Attribute_Attr"
                          order-by="attr_name asc" lazy="false" access="field">
                     <key column="id"/>
                     <map-key column="attr_name" type="string"/>
                     <element column="attr_value" type="string"/>
                </map>
                         
           </class>
      </hibernate-mapping>

       

      The resulting sql script:

       

      create table Attribute (

           id int8 not null,
           name varchar(255) not null,
           value text,
           parent int8 not null,
           primary key (id),
           unique (name, parent)
      );

       

      create table Attribute_Attr (
           id int8 not null,
           attr_name varchar(255) not null,
           attr_value varchar(255),
           primary key (attr_name, id)
      );

       

      alter table Attribute add constraint FK7839CA7C7FB02B5A foreign key (parent) references VocabularyElement;
      alter table Attribute_Attr add constraint FK4F4769F4A36A3D31 foreign key (id) references Attribute;

       

      Any help is much apprecated.