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.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, mapping by annotations, check this link

5 Comments

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.
How is that similar to mapping by code? It's equivalent to NHibernate.Mapping.Attributes
@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.
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.
@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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.