I am trying to add JPA to my project, but always getting those errors:
keycloak_1 | 17:28:43,494 INFO [org.hibernate.jpa.boot.internal.PersistenceXmlParser] (default task-2) HHH000318: Could not find any META-INF/persistence.xml file in the classpath
keycloak_1 | 17:28:43,495 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-2) Uncaught server error: javax.persistence.PersistenceException: No Persistence provider for EntityManager named SQL
But when i try to initialise my entity manager:
HashMap<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.driver", configMap.getFirst(CONFIG_KEY_JDBC_URL));
properties.put("javax.persistence.jdbc.url", configMap.getFirst(CONFIG_KEY_JDBC_URL));
properties.put("javax.persistence.jdbc.user", configMap.getFirst(CONFIG_KEY_DB_USERNAME));
properties.put("javax.persistence.jdbc.password", configMap.getFirst(CONFIG_KEY_DB_PASSWORD));
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(
"SQL",
properties
);
EntityManager entityManager = entityManagerFactory.createEntityManager();
I am receiving those errors, but i can't understand how it's possible then i am clearly added that file to my .jar.
Persistence.xml content is :
<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_2.xsd"
version="2.2">
<persistence-unit name="SQL" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value=""/>
<property name="javax.persistence.jdbc.url" value=""/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
Structure of project and compiled jar is visible here (persistence content is changed to 2.2 version to mentioned before):
What am i doing wrong?
Update: Even find command returns that file exists in a correct directory:
➜ sql-sync-spi find . -type f -name "persistence.xml"
./build/resources/main/META-INF/persistence.xml
./src/main/resources/META-INF/persistence.xml
More updates: I've tried to copy persistence.xml from another project:
<?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="user-storage-jpa-example">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>org.keycloak.quickstart.storage.user.UserEntity</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
And on service startup i've received fatal error:
07:03:02,188 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "sql-sync-spi-1.0-SNAPSHOT.jar")]) - failure description: {
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jboss.datasources.ExampleDS1"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.persistenceunit.\"sql-sync-spi-1.0-SNAPSHOT.jar#sql\".__FIRST_PHASE__ is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]",
"jboss.persistenceunit.\"sql-sync-spi-1.0-SNAPSHOT.jar#sql\" is missing [jboss.naming.context.java.jboss.datasources.ExampleDS1]"
]
}
So at least part of the project sure sees persistence.xml file and tries to read it. Then i've changed xml back to my settings, and again i have no error on startup but again no file exists in classpath on entity manager init attempt.