1

Is it possible to add mapping in code rather in hibernate.cfg.xml file??

I have one hibernate.cfg.xml file for several different servers and I would like to load the mapping dynamically and not from the cfg file.

Thank you!

2
  • For example: If my hibernate.cfg.xml has this line: <mapping class="com.test.User"/> Then, I would like to remove it from cfg file and instantiate it using code. How to do that?? some code example would be appreciated. Commented Sep 12, 2014 at 14:18
  • Just have a look at the link I posted in my answer. The first examle answers exactly this question. Commented Sep 13, 2014 at 11:29

2 Answers 2

3

You can configure your entities using the JPA annotations. There are a lot of tutorials for this on the net, an example is Hibernate Annotations.

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

Comments

1

The answer that worked for me:

AnnotationConfiguration config = new AnnotationConfiguration().configure(hibernateCfgFileName); config.addAnnotatedClass(Myclass.class);

1 Comment

Yes, the same information is available in the link shared by @BetaRide

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.