2

How can we add more entities to hibernate? Mine is a standalone application built on spring and hibernate.

I have the metadata about the class file of the new entities. How can I add them to hibernate session factory without restarting or making changes to the confugation XML files?

All the mappings are managed by hibernate itself. And only the configurations like connection parameters are maintained in XML.

In short, if I have the class file location of the entities, how can I instruct hibernate to pickup the entities from there, without restarting or making changes to the config files?

Thanks.

1
  • Hi, did you get it working? I'm trying it so far and no success :( Commented Nov 18, 2013 at 13:48

2 Answers 2

3

From the Hibernate docs:

The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.

What you could do is have a separate SessionFactory for each set of dynamically loaded entities. Create a Configuration and add your new classes to it. Use that to create a new SessionFactory or EntityManagerFactory.

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

2 Comments

If I go for creating separate session factories, how the connection and transaction management will be affected? Will this approach need restart of the application?
I expect that SessionFactories will have independent connections and transactions. As far as restarting your application, I don't think that would be necessary.
3

The way to do this is like the following:

Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); // base configuration

configuration.addAnnotatedClass(MyClass1.class); // @Entity classes
configuration.addAnnotatedClass(MyClass2.class);
configuration.addAnnotatedClass(MyClassN.class);

Session session = configuration.buildSessionFactory().openSession();
// Work with session...

Comments

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.