JPA 2.0 / Application managed / EntityManager null - java

I am trying to build a sample application in pure jee7. I'm stuck with the DAO part:
When i use:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPu");
Emf is not null and i can persist/find etc etc...
When i try to use dependency injection like:
#Singleton
public class MyDAOClass{
#PersistenceUnit(unitName = "myPu")
EntityManagerFactory emf;
}
emf is null. I use Wildfly 8. I am using the jee7 doc from oracle and i dont find where i did my mistake.
my persistence.xml is well placed. Wildfly detects it when it starts...
PU looks like:
<persistence-unit name="plcmanagerPU">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>fr.digitalautomation.entity.ui.PLCBackgroundView</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/plcmanager"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root135"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
Some advise would be very appreciated.
Thx you !

try
<persistence-unit name="myPu">

Related

SingletonEhCacheRegionFactory not available with Hibernate 6.1.7

We are using hibernate 5.2.5, where we were overriding start method of org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory todo some init actions. This class
we configure in persistence.xml for key "hibernate.cache.region.factory_class"
public class AppSingletonEhCacheRegionFactory extends SingletonEhCacheRegionFactory {
#Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
//init_actions
super.start(settings, properties);
}
}
Now we are upgrading to 6.1.7.FINAL
In docs, EHcache is not mentioned in hibernate 6.1.7 https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#caching-config-provider
persistence.xml
<persistence-unit name="db" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>app.jdbc.APPDS</jta-data-source>
<properties>
<property name="jboss.as.jpa.providerModule" value="org.hibernate:main"/>
<property name="hibernate.cache.region.factory_class"
value="com.app.AppSingletonEhCacheRegionFactory"/>
<property name="hibernate.connection.release_mode" value="after_transaction"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.jdbc.batch_size" value="30"/>
<property name="net.sf.ehcache.cacheManagerName" value="app-cache"/>
<property name="hibernate.integration.envers.enabled" value="false"/>
<property name="hibernate.cache.region_prefix" value=""/>
</properties>
</persistence-unit>
EHcache can be configure by using the JCache integration. Take a look into the JCache integration tests to see how we test this.

Hibernate errors : No Persistence provider for EntityManager named my-unit

i've tried to put my persistence.xml in various parts of my Java project and i call it with this command:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("my-unit");
but it returns me an error:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at main.MainClass.main(MainClass.java:81)
Process finished with exit code 1
my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- use encache provider-->
<!--<property name="hibernate.cache.region.factory_class"-->
<!--value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>-->
<!--<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>-->
<!--<!–use second level cache–>-->
<!--<property name="hibernate.cache.use_second_level_cache" value="true"/>-->
<!--<!–use 3rd level cache–>-->
<!--<property name="hibernate.cache.use_query_cache" value="true"/>-->
</properties>
</persistence-unit>
I use IntelliJ as IDE for Java and the persistence.xml file is under META-INF folder in resources folder, i have tried to check the project structure and all looks like nice.
I use hibernate as maven repository.
Thanks for helping
You need to define the persistence provider name
<provider>org.hibernate.ejb.HibernatePersistence</provider>
That will solve the problem.

I can't find resolve PersistenceException: No Persistence provider for EntityManager

I found some solutions in the forum, but they not working for me.
The problem is that when I run main method I receive this exception -
PersistenceException: No Persistence provider for EntityManager
On the imane is my persistance.xml file pat but it's not working. I try to change file path to web/WEB-INF/classes/META-INF, but its's still not working.
Here my full persistance.xml source
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="StudentPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.classes.StudentBean</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/Students"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.username" value="doncho"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
</properties>
</persistence-unit>
</persistence>
And one more thing? Why HibernatePersistence is stricken?
I resolve "stricken" mistake like that -
Is it correct?
But anyway the PersistenceException: No Persistence provider for EntityManager still there.
I tryed - this and this - nothings happened
I would be grateful for any ideas to resolve this error because #Entity method is important to me.
Best regards,
D. Balamjiev
The workig method for me is to create this path in my project tree:
web/WEB-INF/classes/META-INF/persistence.xml
This is working path when We create Web Applications with IntelliJ.

C3P0 Configurations! Where and How?

We are implementing a Web App using JPA2.0 and Hibernate3.0. Connection pool configurations are set in persistence.xml located in META-INF folder.
persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
<!-- Entity Classes-->
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="bytecode.provider" value="org.hibernate.bytecode.javassist.BytecodeProviderImpl"/>
<property name="hibernate.connection.username" value="{username}"/>
<property name="hibernate.connection.password" value="{password}"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.url" value="{jdbc url}"/>
<property name="hibernate.c3p0.min_size" value="1"/>
<property name="hibernate.c3p0.timeout" value="1000"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<property name="hibernate.c3p0.idle_test_periods" value="600"/>
<property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>
<property name="hibernate.c3p0.preferredTestQuery" value="SELECT 1;"/>
</properties>
</persistence-unit>
</persistence>
We have a problem with connection pool configurations. It seems the configurations have no effect and the connection will be broken after 8 hours.
Do we need another configuration file like hibernate.cfg.xml or hibernate.properties?
I had this same problem with the proprieties that I put in persistence.xml didn't affect c3p0.
Looking into http://www.mchange.com/projects/c3p0/index.html#configuration_files I tried to put an xml file named c3p0-config.xml and put it in WEB-INF/classes and it work perfectly.
Here is an example of a c3p0-config.xml file:
<c3p0-config>
<default-config>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
<user-overrides user="test-user">
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
<property name="maxStatements">0</property>
</user-overrides>
</default-config>
</c3p0-config>
Good question, bad title. :) I think I answered this question on your re-post: Best configuration of c3p0
I had the same exact issue, my problem was that my web application container (Tomcat) was managing my database connections. I had to move the c3p0 configuration from my persistence.xml file to Tomcat's context.xml. The link Domenic D provided is a great place to start if that's your problem.
There is a typo in your settings, it should be idle_test_period not idle_test_periods.
See this post for information about the setting: The use of c3p0.idle_test_period.

Persistence unit is not persistent

I need persistence unit that creates embedded database which stays persistent after closing EntityManager.
This is my PU:
<persistence-unit name="hello-jpa" transaction-type="RESOURCE_LOCAL">
<class>hello.jpa.User</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:target/hsql.db"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
And it deletes data after closing application.
My understanding of the documentation is that the "old" URL jdbc:hsqldb:. creates or connects to the same database as the new form for the URL jdbc:hsqldb:mem:. (i.e. in memory).
So try with the following URL instead: jdbc:hsqldb:file:target/hsql.db.

Categories