Hibernate configuration - session factory scanning?

前端 未结 5 1067
粉色の甜心
粉色の甜心 2020-12-22 01:06

We have this hibernate.cfg.xml file. Is there a way to tell Hibernate to just scan a directory instead of having to add an entry here for each class?



        
5条回答
  •  醉酒成梦
    2020-12-22 01:45

    A NHibernate best practice is to add only an assembly to the configuration before building your ISessionFactory API.

    Robert did point it out with the element.

    Another way would be to perform runtime configuration as follows:

    Configuration cfg = new Configuration();
    cfg.AddAssembly(typeof(OneOfYourDomainType).Assembly.Name); // Or something like that by memory.
    cfg.Configure();
    static ISessionFactory sessionFactory = cfg.BuildSessionFactory();
    

    Making the ISessionFactory static is important as it is very expensive to instantiate.

    typeof(OneOfYourDomainType).Assembly.Name returns the name of your assembly containing all of your domain objects, with the proper mappings. Then, adding this, you add the assembly, and you do not need to repeat the process again and again for your domain types.

提交回复
热议问题