How to run fix error about persistence.xml - java

hi all i got two different error when i run my java apps using hibernate persistence.xml
1. Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
2.Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
for the first error i have add my persistence.xml like this one :
here my persistence.xml
<?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="FastMatch" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.dxtr.hibernate.newOrderSingleEntity</class>
<properties>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://url/tables" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="eclipselink.logging.level.connection" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
for the second i have add in my depedency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
and
still got error like this one
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.dxtr.Transaction.FastMatchTransaction.FastmatchTransaction.main(FastmatchTransaction.java:12)
and here my pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
i have try add javax.persistence.2.2.jar and other solution from other link here but this problem cannot run well
for more information i use mysql5.7 and eclipse for IDE. and for running this code i use aws. i have try all of the code in stackoverflow but still got the problem so how to fix it ?
fuad

Related

Java Hibernate Issue: No Persistence provider for EntityManager named BookStoreWebsite

I know this question has been out there for a while but I tried about 10 different solutions and nothing worked. I'm new to Hibernate and Maven, so I'm a bit lost.
I've checked if persistence is inside Meta-inf,
added provider tag under persistence unit,
checked libraries
and so on...
import com.bookstore.entity.Users;
public class UsersTest {
public static void main(String[] args) {
Users user1 = new Users();
user1.setEmail("pablo.the.souza#gmail.com");
user1.setFullName("Pablo Souza");
user1.setPassword("123456");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BookStoreWebsite");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(user1);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
System.out.println("A users object was persisted");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="BookStoreWebsite">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bookstore" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
</persistence>
I changed the Hibernate core dependency from:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha5</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
to this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
And it works. Not sure why unfortunately.

jpa with mysql db connection was not working in lagom

I created the lagom project and connect to MySQL db with jpa persistence. My configurations files are below. when i run this code table was not created in db, and it displays the error is follow. Please any one help me.
This error diplays after some secons i dont know what i missing in this code.
Error
akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://rdapupdate-impl-application/user/jd
bcCreateTables-singleton/singleton/jdbcCreateTables#-898354221]] after [20000 ms]. Message of type [com.lightbend.lagom.internal.persistence.cluster.ClusterStartupTaskActor$Execute$] was sent by [Actor[akka://rdapupdate-impl-application/user/jdbcCreateTables-singleton/singleton/jdbcCreateTables#-898354221]]. A typical reason for `AskTimeoutException` is that the recipient actor didn't send a reply.
at akka.pattern.PromiseActorRef$.$anonfun$defaultOnTimeout$1(AskSupport.scala:635)
at akka.pattern.PromiseActorRef$.$anonfun$apply$1(AskSupport.scala:650)
at akka.actor.Scheduler$$anon$4.run(Scheduler.scala:205)
at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Future.scala:870)
at scala.concurrent.BatchingExecutor.execute(BatchingExecutor.scala:109)
at scala.concurrent.BatchingExecutor.execute$(BatchingExecutor.scala:103)
at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scala:868)
at akka.actor.LightArrayRevolverScheduler$TaskHolder.executeTask(LightArrayRevolverScheduler.scala:328)
at akka.actor.LightArrayRevolverScheduler$$anon$4.executeBucket$1(LightArrayRevolverScheduler.scala:279)
at akka.actor.LightArrayRevolverScheduler$$anon$4.nextTick(LightArrayRevolverScheduler.scala:283)
at akka.actor.LightArrayRevolverScheduler$$anon$4.run(LightArrayRevolverScheduler.scala:235)
at java.lang.Thread.run(Thread.java:748)
application.conf
#
# Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com>
#
play.modules.enabled += sample.rdapupdate.impl.RdapUpdateServiceModule
rdapupdate.cassandra.keyspace = stream
cassandra-journal.keyspace = ${rdapupdate.cassandra.keyspace}
cassandra-snapshot-store.keyspace = ${rdapupdate.cassandra.keyspace}
lagom.persistence.read-side.cassandra.keyspace =
${rdapupdate.cassandra.keyspace}
akka.cluster.sharding.state-store-mode = ddata
akka.actor.serialization-bindings {
"akka.Done" = akka-misc
"akka.actor.Address" = akka-misc
"akka.remote.UniqueAddress" = akka-misc
}
db.default {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/test"
}
lagom.persistence.jdbc.create-tables.auto = true
jdbc-defaults.slick.driver = "slick.driver.MySQLDriver$"
lagom.persistence.jpa {
persistence-unit = "default"
initialization-retry {
interval {
min = 5s
factor = 1.0
}
max-retries = 10
}
}
persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/>
<!-- Add any other standard or provider-specific properties -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/test" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<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" />
</properties>
</persistence-unit>
</persistence>
I'm using the following maven dependencies
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-persistence-jdbc_${scala.binary.version}</artifactId>
<version>${lagom.version}</version>
</dependency>
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-persistence-jpa_${scala.binary.version}</artifactId>
<version>${lagom.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>

java.lang.ClassNotFoundException org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider

Good morning, I 'm trying to implement a multi-tenancy Java project ( Primefaces , EJBs , Hibernate 5 / JPA - Postgres ).
pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.7.Final</version>
<scope>provided</scope>
</dependency>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="myDS" transaction-type="JTA">
<jta-data-source>java:/myDS</jta-data-source>
<class>com.arkin.erpmodel.general.entities.AccountingDocumentType</class>
<class>com.arkin.erpmodel.general.entities.AccountingOperation</class>
<class>com.arkin.erpmodel.general.entities.AccountingYear</class>
<class>com.arkin.erpmodel.general.entities.AccountingYearPeriod</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.multiTenancy" value="SCHEMA"/>
<property name="hibernate.tenant_identifier_resolver" value="com.arkin.erpmodel.multitenancyprovider.SchemaResolver"/>
<property name="hibernate.multi_tenant_connection_provider" value="com.arkin.erpmodel.multitenancyprovider.MultiTenantProvider"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="org.hibernate.type" value="debug" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.infinispan.statistics" value="false" />
<property name="hibernate.order_inserts" value="true"/>
<property name="hibernate.jdbc.batch_size" value="20"/>
<!-- Turn on entity and query cache statistics in the admin console -->
<property name="hibernate.generate_statistics" value="false" />
<!-- store entries in the cache in a more human friendly format - helps when interpreting logs -->
<property name="hibernate.cache.use_structured_entries" value="false" />
<!-- MultiTenancy -->
</properties>
</persistence-unit>
</persistence>
Maven Dependencies
I could help know why not recognize the MultiTenantConnectionProvider class?
If you check the readme in hibernate-entitymanager it says
Hibernate's JPA support has been merged into the hibernate-core
module, making this hibernate-entitymanager module obsolete. This
module will be removed in Hibernate ORM 6.0. It is only kept here for
various consumers that expect a static set of artifact names across a
number of Hibernate releases. See
https://hibernate.atlassian.net/browse/HHH-10823
Now, you need to use hibernate-core
The MultiTenantConnectionProvider is located in
org.hibernate.engine.jdbc.connections.spi package of `hibernate-core`
You can include this maven dependancy:-
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.0.7.Final</version>
</dependency>

Try to use C3P0 with Hibernate

I'm using JPA and have a persistence.xml with the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd ">
<persistence-unit name="odontonewPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
<!-- C3P0 configuration -->
<property name="hibernate.connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
</properties>
</persistence-unit>
</persistence>
The problem is that when i try to initialize my tomcat i got the following error:
691 [Thread-2] ERROR org.hibernate.connection.ConnectionProviderFactory - Could not instantiate connection provider
java.lang.ClassNotFoundException: org.hibernate.connection.C3P0ConnectionProvider
In my pom.xml (i use maven), i have the following dependency:
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
That class is part of hibernate-c3p0. You will need to add a dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.2.7.Final</version>
</dependency>
or whichever version you need.

How to skip the schema generation of a superclass table? Java/JPA/Hibernate/Annotations/Maven/hbm2ddl

Our application has a need to share an existing database table with another locally running application, and, so, I wish to refer to this table but skip generating the DDL for it. I also hoped to define the table in the application's schema as an alias or MySQL merge table, but I can add that to a manual script that will run before the generated schema script.
How can I specify that the create and alter table DDL for the shared_schema table are omitted from the myapp-schema-ddl.sql output?
From commons/src/main/java/com/company/shared/SharedSuperEntity.java:
#Entity
#Table(name="shared_entity", catalog = "shared_schema")
#Inheritance(strategy = InheritanceType.JOINED)
public class SharedSuperEntity {
// fields, etc...
}
From myapp/src/main/java/com/company/shared/SharedSuperEntity.java:
#Entity
#Table(name="local_entity", catalog = "local_schema")
public class LocalEntity extends SharedSuperEntity {
// fields, etc...
}
From myapp/src/main/resources/META_INF/persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/LocalDS</jta-data-source>
<class>com.company.shared.SharedSuperEntity</class>
<class>com.company.myapp.LocalEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>AUTO</validation-mode>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
<property name="hibernate.default_batch_fetch_size" value="4" />
<property name="hibernate.default_entity_mode" value="pojo" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.jdbc.fetch_size" value="0" />
<property name="hibernate.jdbc.use_get_generated_keys" value="false" />
<property name="hibernate.jdbc.use_scrollable_resultset" value="false" />
<property name="hibernate.jdbc.use_streams_for_binary" value="false" />
<property name="hibernate.hibernate.max_fetch_depth" value="3" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.query.substitutions" value="true 1, false 0, yes 'Y', no 'N'" />
<property name="hibernate.use_identifer_rollback" value="true" />
<property name="hibernate.use_outer_join" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>
From within myapp/pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>jpaconfiguration</implementation>
</component>
</components>
<componentProperties>
<persistenceunit>MyAppPU</persistenceunit>
<outputfilename>myapp-schema-ddl.sql</outputfilename>
<drop>false</drop>
<create>true</create>
<export>false</export>
<format>true</format>
<jdk5>true</jdk5>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
I don't think Hibernate supports that directly. You could make two config files: one that lists all the mapped classes for runtime and one that omits the offending class for schema generation. Otherwise, you might look at some kind of post-processing, like a sed script, to remove the DDL after the fact.

Categories