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: Replacing *.hbm.xml files with @Annotations.
PostPosted: Fri Feb 05, 2010 8:53 am 
Newbie

Joined: Fri Feb 05, 2010 8:46 am
Posts: 1
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:

Code:
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:

Code:
<?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:

Code:
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 appreciated.


Top
 Profile  
 
 Post subject: Re: Replacing *.hbm.xml files with @Annotations.
PostPosted: Fri Feb 05, 2010 12:07 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi Jan Erik,

looks like a duplicate...
http://www.coderanch.com/t/481688/Object-Relational-Mapping/java/Replacing-hbm-xml-files-with

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.