Hibernate sets up the way to create an object

Can I configure Hiberate / NHibernate not to use the default constructor to create objects when reading from the database?

When Hibernate reads 10 clients from a table, it creates 10 Customerobjects. He does it with

Customer c = new Customer();

Is it possible to do the following instead of Hibernate:

Customer c = ACertainStaticFactory.CreateNewCustomer();

or even to manage a factory instance:

ACertainFactory factory = .....;
Customer c = factory.CreateNewCustomer();

or even more complicated, pass the parameter that I set earlier:

// My client code
Query query = session.CreateQuery(...);
// either:
query.SetSomeParameter(someObject);
// or:
session.SetSomeParameter(someObject);

query.List();

// Hibernate should then behave like this:
Customer c = new Customer(someObject);
// or
Customer c = ACertainStaticFactory.CreateNewCustomer(someObject);
// etc.

Is this possible anyway? If yes: how? If not: is there an alternative?

+3
source share
2 answers

When Hibernate reads 10 customers from a table, it creates 10 Customer objects. He does this with the help of (...)

, Hibernate Class<T>#newInstance() , no-arg. , .

? : ? : ?

, Hibernate Factory? Hibernate, ( ):

. :

, ( , factory ).

+2

Source: https://habr.com/ru/post/1768629/


All Articles