java - JPA with Hibernate 5: programmatically create EntityManagerFactory -
this question specifically programmatically creating jpa entitymanagerfactory
backed hibernate 5, meaning without configuration xml files , without using spring. also, question specifically creating entitymanagerfactory
with hibernate interceptor.
i know how create hibernate sessionfactory
way want, not want hibernate sessionfactory
, want jpa entitymanagerfactory
backed hibernate sessionfactory
. given entitymanagerfactory
there way obtain underlying sessionfactory
, if have sessionfactory
, want entitymanagerfactory
wrapper around it, appears out of luck.
with hibernate version 4.2.2 ejb3configuration
deprecated, there seemed no other way programmatically create entitymanagerfactory
, doing this:
@suppresswarnings( "deprecation" ) entitymanagerfactory buildentitymanagerfactory( unmodifiablemap<string,string> properties, unmodifiablecollection<class<?>> annotatedclasses, interceptor interceptor ) { ejb3configuration cfg = new ejb3configuration(); for( binding<string,string> binding : properties ) cfg.setproperty( binding.key, binding.value ); for( class<?> annotatedclass : annotatedclasses ) cfg.addannotatedclass( annotatedclass ); cfg.setinterceptor( interceptor ); return cfg.buildentitymanagerfactory(); }
with hibernate 4.3.0 ejb3configuration
removed, had make use of hack:
entitymanagerfactory buildentitymanagerfactory( unmodifiablemap<string,string> properties, unmodifiablecollection<class<?>> annotatedclasses, interceptor interceptor ) { configuration cfg = new configuration(); for( binding<string,string> binding : properties ) cfg.setproperty( binding.key, binding.value ); for( class<?> annotatedclass : annotatedclasses ) cfg.addannotatedclass( annotatedclass ); cfg.setinterceptor( interceptor ); standardserviceregistrybuilder ssrb = new standardserviceregistrybuilder(); ssrb.applysettings( cfg.getproperties() ); //??? why again? serviceregistry serviceregistry = ssrb.build(); return new entitymanagerfactoryimpl( persistenceunittransactiontype.resource_local, /**/ /*discardonclose=*/true, /*sessioninterceptorclass=*/null, /**/ cfg, serviceregistry, null ); }
(it hack because instantiating entitymanagerfactoryimpl
in package org.hibernate.jpa.internal
.)
now, hibernate 5 have changed constructor of entitymanagerfactoryimpl
, above code not work. can waste few hours trying figure out how set things can invoke constructor, sure after couple of hibernate versions, won't work anymore, either.
so, question:
does know of nice , clean way of implementing function
entitymanagerfactory buildentitymanagerfactory( unmodifiablemap<string,string> properties, unmodifiablecollection<class<?>> annotatedclasses, interceptor interceptor )
so create hibernate entitymanagerfactory
programmatically, meaning without configuration xml files , without using spring with hibernate interceptor ?
there old question: hibernate create jpa entitymanagerfactory out persistence.xml has answer older version of hibernate, has been anticipated in question. won't do, because want work hibernate 5, , ideally, in way not use deprecated or internal, have chances of working long time come.
the easiest way pass along org.hibernate.jpa.boot.spi.persistenceunitdescriptor
reference, abstraction on "persistence unit" information. in normal jpa bootstrapping hibernate build persistenceunitdescriptor
on persistence.xml (for jpa calls "se bootstrapping") or on javax.persistence.spi.persistenceunitinfo
(for jpa calls "ee bootstrapping).
but abstraction reason :) create own , pass want hibernate use. intended way works starting org.hibernate.jpa.boot.spi.bootstrap
, e.g.:
entitymanagerfactory emf = bootstrap.getentitymanagerfactorybuilder( new custompersistenceunitdescriptor(), collections.emptymap() ).build(); ... class custompersistenceunitdescriptor implements persistenceunitdescriptor { @override public properties getproperties() { final properties properties = new properties(); properties.put( availablesettngs.interceptor, new myinterceptor( ... ); return properties; } ... }
Comments
Post a Comment