I have a different *persistence.xml* in *src/test/resources/META-INF* in my
maven project for unit tests but it seems that the *persistence.xml* in
*src/main/resources/META-INF* gets precedence.
Is it possible to specify different *persistence.xml* for tests?
For more information:
http://code.google.com/p/google-guice/wiki/JPA
Is there a way to load the properties for a persistence unit from a
properties file? I can do this with standard JPA, but I don't see a way to
inject custom properties into the JpaPersistService where it creates the
EntityManagerFactory.
I found it. You can pass properties to the JpaPersistModule. For example,
when creating JpaPeristModule, you can invoke:
new JpaPersistModule("myapp-db").properties(hibernateJpaProperties)
It looks that injecting EntityManager this way:
@Inject
EntityManager em;
breaks persist() and merge() functionality. I tested it on Jboss
5/Hibernate. Everyting else works, but objects just won't be saved. We had
to change it to:
@Inject
Provider<EntityManager> em;
There are great warp-persist examples here:
http://code.google.com/p/warp-persist-sample/. With minor modifications it
will work with guice-persist engine.
I quote zyskow, the EntityManager injection does not work also on
Tomcat7.0.19/Hibernate3.6.7.
Hello,
I'd like to ask you if it's possible to make the PersistFilter class not
final.
Cause I need to start the PersistService when the servlet context is
initialized. But when the filter starts it throws an error.
If it wasn't final or if it could check if the PersistService is already
initialized would solve my problem.
There is the hint "Note that if you make MyService a @Singleton, then you
should inject Provider<EntityManager> instead." IMHO this not only is true
if MyService is a @Singleton but also if MyOtherService is a @Singleton and
is using MyService which itself is NOT @Singleton.
Would look to add something for newcomers like me. Don't know how obvious
or not this is (it wasn't for me), but if you instantiate MyService
MANUALLY your EntityManager will be null and calls will throw
NullPointerException. Since we are using Guice already, just create a new
module which you can add after MyModule when you call createInjector
public class ServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(MyService.class);
http://google-guice.googlecode.com/git/javadoc/com/google/inject/persist/PersistFilter.html
For multiple providers, you should register this filter once per
provider, inside a private module for each persist module installed (this
must be the same private module where the specific persist module is itself
installed).
Is there an example somewhere? I tried several things but was not able to
use multiple persistence units with the PersistFilter.
https://groups.google.com/forum/#!topic/google-guice/2VK-bdsnjZc/discussion
The module file for this sample may also be kindly furnished for the
benefit of users new to Guice but have used Jpa earlier.
I'm using an environment with guice-servlet running on a tomcat and
hibernate under guice-persist. The problem I've encountered is that when I
use em.getReference() in one request the loaded proxy object stays in the
entitymanager cache and may appear in another request where I expect to
have an object completely loaded from the DB.
I'm used to using hibernate in EJB3 environment where it is a default
behavior. The entity manager cache is clear for each new request. Isn't it
a more safe behavior for guice-persist to clear the session for each
request? Or at least to give it as a setting for JpaPersistModule?
There is a special flag in hibernate SessionImpl "autoClear" which is
responsible for EJB3-behavior. Is there any way I could enable the flag
when the new entity manager is being created by JpaPersistModule?
"Note that if you make MyService a @Singleton, then you should inject
Provider<EntityManager> instead."
When should MyService be a singleton? If I am using the DAO without state,
i.e. injecting it into my Servlets for processing through a Provider,
should it be a one and done created object? I am creating a new one each
time, but have noticed that on message boards people say to make the DAO
Services singletons, is that right?
I am attempting to disable flushing in my entityManager.
So I have: <property name="org.hibernate.FlushMode" value="NEVER" />
in my persistence.xml file.
However, this seems to be getting completely ignored when the EntityManager
is created.
Is there something I need to be doing when I create my JpaPersistModule to
get this to work properly?