@Embeddable with hbm.xml

At work we are using hibernate with annotations. So in some places we use the @Embeddable & @Embedded annotation. What do they do? They are both standard annotations, @Embeddable means “this class will not have it’s own table, and it will be embedded in some other’s class table”, and @Embedded means “embed this field to my table”.

That’s all good, but I’ve returned to my old project (klopaj.com), and what do we have there? hbm.xml files. I confess, I’ve first started using hibernate annotations, and then hbm files, so I’m not a hbm guru (nor annotations, but I know a bit more :)). So, to do the same thing with hbm.xml:


<class name="Poi"...>
...
<component name="geoPoint" access="field" class="GeoPoint">
<property name="latitude" type="double" />
<property name="longitude" type="double"  />
...
</component>

Simple as that, now my Poi table has two additional columns “latitude” and “longitude”, but my Poi class has a reference to GeoPoint instance. :)
But still, I like the annotation solutions prettier, but this one is working as planned.

Advertisements

5 comments

  1. You know…I still have a dilemma should I blog in English or in Serbian… Mislim da ti makar koji radis Javu bi trebalo da pises na Srpskom ;) E da, i stavi neki syntax highlighting.

    • I have the same dilemma :)
      Actually I wanted to write in Serbian for javasvet, but we’ll see.

      A za syntax, stavio sam neki plugin koji zeza. Moraću izgleda sam da hostujem da bi sve radilo kako valja :)
      Takođe mislio sam i da imam i na srpskom i na engleskom, a onda sam shvatio da me mrzi da pišem toliko…

  2. Hello Vuk,

    I am stuck with same problem ….. Primary Key of my class Test.java (table name Test) is of Type Test_PK.java — Where Test_PK.java has some 3 variables (column) …

    Now using annotations it is very easy as I can define like this

    @Entity
    @Table (———)
    public class Test{
    @Id
    private Test_PK id;
    getter & setters
    private String l_var;
    getter & setters
    }

    @Embeddable
    public class Test_PK{
    private String var1;
    private String var2;
    private String var3;
    getters & setters
    }

    How can I achive this hbm.xml file ???
    Can you please Suggest as how to map component as primary key basically as id tag in hbm.xml file

    Thanks,
    Ritesh

  3. If I understood you correctly, here is how to map your Test class:

    
    <class name="Test"...>
    ...
    <component name="id" access="field" class="Test_PK">
    <property name="var1" type="string" />
    <property name="var2" type="string" />
    <property name="var3" type="string" />
    ...
    </component>
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s