12

In Nhibernate we have Fluent Nhibernate and, now, the built-in "Mapping by code" feature in Nhibernate 3.2. Both allow you to programmatically construct the mappings for your Domain and we could either write some conventions to map all the domain or we could write individual classes for each corresponding domain object.

Anything similar for Hibernate?

2 Answers 2

1

You should be able to configure Hibernate without XML and Annotations by using the Hibernate Configuration API, see http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/cfg/Configuration.html

Table mappings can be created through the Mappings API: http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/cfg/Mappings.html

I've never used the latter one as I found annotations the best way to go - but maybe the pointer helps.

0

Yes, mapping by annotations, check this link

5
  • Correct me if wrong, there is no convention over configuration approach via annotations right? Meaning, I still need to litter all my classes with annotations one way or another.
    – Newbie
    Commented Apr 3, 2012 at 2:43
  • 1
    How is that similar to mapping by code? It's equivalent to NHibernate.Mapping.Attributes Commented Apr 4, 2012 at 16:15
  • @newbie you are wrong. Hibernate has a pretty strong convention over configuration. You need a few annotations, but I would gladly argue that an entity class with some annotations looks "better" then the examples I have seen mapping in code. Mostly because you have your mapping code precisely where I need it.. But I don't expect everyone to agree.
    – barsju
    Commented Apr 4, 2012 at 19:13
  • 1
    The problem with annotations is that your domain classes now need to be persistence-aware. It's not a capital sin, but it's bad separation of concerns. Also, it doesn't answer the question. Commented Feb 19, 2015 at 10:09
  • @joão-mendes If you have a good separation of concerns, you will have you entity classes and several domain classes, if you are using your Entity Tables as business models you do not have a good separation of concerns. A simple catalog when is added it just need a name, for edit also the id and maybe a status field, so, the entity is not suitable for both representations, the best is to have your repo layer and above them your business objects
    – rekiem87
    Commented Jul 4, 2018 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.