jpa with mysql db connection was not working in lagom - java

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>

Related

How to run fix error about persistence.xml

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

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.

PersistenceProvider createEntityManagerFactory returns null in Eclipse Plugin

I created a standalone app to access my database (either mssql or h2)
i created a helper class which connects to my server by passing the connection details.
all fine and works as it is supposed to be.
now i move my code to an Eclipse Plugin (->OSGi)
first i ran into a Driver Not Found Error, but i fixed this with passing my ClassLoader to the PersistenceProvider.
now i got the next problem, which i am not able to resolve:
the PersistenceProvider returns null, when i call createEntityManagerFactory passing the persistence unit name and my map.
it does not throw any exceptions...simply returns null
so i guess the driver is found...
as the standalone application is working, it can't be the persistence.xml either...
any ideas?
here my persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="EclipseLinkTest" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- <mapping-file>META-INF/contact-orm.xml</mapping-file> -->
<properties>
<!-- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> -->
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:h2:D:/JAVA/Final/lumo.osgi.service.eclipselink.impl/db/embedded.db" /> -->
<!-- <property name="javax.persistence.jdbc.user" value="admin" /> -->
<!-- <property name="javax.persistence.jdbc.password" value="admin" /> -->
<!-- <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> -->
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://dbserverIP:1433;DatabaseName=testsystem" /> -->
<!-- <property name="javax.persistence.jdbc.user" value="editor" /> -->
<!-- <property name="javax.persistence.jdbc.password" value="editorpwd" /> -->
<!-- EclipseLink should create the database schema automatically -->
<!-- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> -->
<!-- <property name="eclipselink.ddl-generation" value="create-tables" /> -->
<!-- <property name="eclipselink.ddl-generation.output-mode" value="database" /> -->
<!-- <property name="eclipselink.weaving" value="true"/> -->
<property name="eclipselink.logging.level" value="WARNING"/>
</properties>
</persistence-unit>
</persistence>
and this is the code to get the configuration (application is the main class - so its class loader got information about everything in my target definition)
public Properties getEmbeddedConfiguration(String path, String database,
String login, String password) {
Properties props = new Properties();
// this one is for getting started in OSGi & JPA
// No suitable driver found for jdbc:h2:file:C:\temp\Thumbs.db;IFEXISTS=TRUE
// props.put(PersistenceUnitProperties.CLASSLOADER,
// EclipseLinkServiceImpl.class.getClassLoader());
// this fixes the driver not found but leaves a nullpointer
props.put(PersistenceUnitProperties.CLASSLOADER,
Application.class.getClassLoader());
String connectionString = String.format(
"jdbc:h2:file:%s%s;IFEXISTS=TRUE", path, database);
props.put("javax.persistence.jdbc.driver", "org.h2.Driver");
props.put("javax.persistence.jdbc.url", connectionString);
props.put("javax.persistence.jdbc.user", login);
props.put("javax.persistence.jdbc.password", password);
props.put("eclipselink.target-database",
"org.eclipse.persistence.platform.database.H2Platform");
props.put("eclipselink.ddl-generation", "drop-and-create-tables");
props.put("eclipselink.ddl-generation.output-mode", "database");
return props;
}
and last but not least the create part
PersistenceProvider p = new PersistenceProvider();
factory = p.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,
getEmbeddedConfiguration(folder, database, username, pwd));
System.out.println("factory: " + factory);
em = factory.createEntityManager();
To get JPA running in OSGi take a look at the Gemini project

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