Generating metamodel classes with JPA 2.1 - java

I have problems using hibernate metamodel generator:
java: Error unmarshalling /META-INF/persistence.xml with exception :
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 103; cvc-elt.1: Cannot find the declaration of element 'persistence'.]
Here is my persistence.xml:
<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://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="wespital" transaction-type="JTA">
<jta-data-source>jdbc/wespital</jta-data-source>
</persistence-unit>
</persistence>
It seems that current version (1.2.Final) does not support JPA 2.1 and I didn't find any beta-versions of this generator in the git repository. What other options could be to generate metamodel classes? I use wildfly as app server.

A new version (1.3.0.Final) has been released on August 9, 2013 that should solve your problems.

DataNucleus JPA metamodel generator generates classes suitable for JPA 2.0/2.1, see the DataNucleus docs.

I used metamodel generator from the eclipselink project, it seems to work fine.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>

Related

Register additional persistence unit in Keycloak Quarkus

We have a custom keyloak user storage provide to use our proprietary user database for authentication.
This works perfectly fine with the WildFly based Keycloak. Since Keycloak will drop WildFly support in june 2022 according to their release notes, I am currently trying to get it running in the new, Quarkus based Keycloak distribution.
First I had to remove all dependencies provided by Quarkus since those lead to class loader issues. But now I am stuck with this error when I try to start Keycloak with the command ./kc.sh start-dev --log-level=ERROR:
Updating the configuration and installing your custom providers, if any. Please wait.
ERROR: Unexpected error when starting the server in (development) mode
ERROR: Failed to start quarkus
ERROR: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
ERROR: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
For more details run the same command passing the '--verbose' option. Also you can use '--help' to see the details about the usage of the particular command.
And 'hibernate.dialect' is set in persistence.xml and configuring a persistance unit in persistence.xml should be supported according to quarkus documentation :
<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="example-pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.example.keycloak.provider.db.entity.Benutzer</class>
<class>com.example.keycloak.provider.db.entity.BenutzerWithSource</class>
<class>com.example.keycloak.provider.db.entity.Rolle</class>
<class>com.example.keycloak.provider.db.entity.Parameter</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
</properties>
</persistence-unit>
</persistence>
Configuring the dialect in an application.properties (according to https://quarkus.io/guides/hibernate-orm#multiple-persistence-units) file in the delivery didn't change the behavior as well:
quarkus.hibernate-orm."example-pu".hibernate-dialect=org.hibernate.dialect.OracleDialect
What am I missing?
Additional Persistence Units are not supported in Keycloak 17.0.0 with Quarkus.
This should be fixed in Keycloak 18.0.0: https://github.com/keycloak/keycloak/pull/10581.

How to create local database using JPA?

I am just starting to use JPA. I am trying to create database based on schema using Persistence.generateSchema("DataLayer", null) method, but i get an exception. As JPA implementation i am using OpenJPA.
My solution is divided into two Eclipse projects. Both use Maven and module-info.java.
1st project is clearly just main method where i call the method above.
2nd one is JPA entities and data access objects.
In 1st project i included dependency to 2nd project. In 2nd project i included dependencies to jpa implementor (openjpa) and database driver (derby). See dependencies part from pom.xml's bellow:
1st project dependency (there is just my 2nd project):
<dependency>
<groupId>registry</groupId>
<artifactId>datalayer</artifactId>
<version>0.0.1</version>
</dependency>
1st project module-info:
module justtestingTEMP
{
exports justtestingTEMP;
requires registry.datalayer;
requires javax.persistence;
}
2nd project dependencies:
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.15.2.0</version>
</dependency>
</dependencies>
2nd project module-info:
module registry.datalayer
{
exports datalayer.other;
exports datalayer.dto;
exports datalayer.dao;
requires javax.persistence;
}
This is persistence.xml (it is in 2nd project):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
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">
<persistence-unit name="DataLayer"
transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>myNonJtaDataSource</non-jta-data-source>
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="create" />
</properties>
</persistence-unit>
This is the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/sql/DataSource
at org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory.newInstance(JDBCBrokerFacto
ry.java:72)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Metho
d)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodA
ccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Delegatin
gMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.apache.openjpa.kernel.Bootstrap.invokeFactory(Bootstrap.java:131)
at org.apache.openjpa.kernel.Bootstrap.newBrokerFactory(Bootstrap.java:66)
at org.apache.openjpa.persistence.PersistenceProviderImpl.getBrokerFactory(Pers
istenceProviderImpl.java:152)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFa
ctory(PersistenceProviderImpl.java:95)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFa
ctory(PersistenceProviderImpl.java:159)
at org.apache.openjpa.persistence.PersistenceProviderImpl.generateSchema(Persis
tenceProviderImpl.java:244)
at javax.persistence#1.1/javax.persistence.Persistence.generateSchema(Persisten
ce.java:188)
at justtestingTEMP/justtestingTEMP.Main.main(Main.java:15)
Caused by: java.lang.ClassNotFoundException: javax.sql.DataSource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoade
r.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoa
ders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
Where can i get that javax.sql.DataSource class from? I guess i am missing some dependency or i have configured something wrong.
What i tried: Switch JPA implementation (other exceptions were raised - but about missing classes), add requires java.sql; to module-info.
After adding requires java.sql; to module-info:
Exception in thread "main" java.lang.NoClassDefFoundError: java/lang /instrument/
ClassFileTransformer
at org.apache.openjpa.persistence.PersistenceProviderImpl.loadAgent(Persistence
ProviderImpl.java:365)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFa
ctory(PersistenceProviderImpl.java:102)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFa
ctory(PersistenceProviderImpl.java:159)
at org.apache.openjpa.persistence.PersistenceProviderImpl.generateSchema(Persis
tenceProviderImpl.java:244)
at javax.persistence#1.1/javax.persistence.Persistence.generateSchema(Persisten
ce.java:188)
at justtestingTEMP/justtestingTEMP.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: java.lang.instrument.ClassFileTrans
former
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoade
r.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoa
ders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 6 more
Found out that required classes were in java.sql and and java.instrument module, so adding these to module-info solve my issue. module-info.java should look like this:
module justtestingTEMP
{
exports justtestingTEMP;
requires java.instrument;
requires java.sql;
requires javax.persistence;
requires registry.datalayer;
}
Also it is possible to remove module-info.java from 1st project. That resulted in a "correct" error saying that i have not defined jdbc driver and connection properties - that is good. No class not found errors. I can create and connect to database now. That is probably because all required modules are available implicitly. With module-info.java i have to define all required modules.

I can't find SqlServerDriver on java project

I'm trying to connect to SQL Server and insert the data into the database. I have a class person, main and DataBaseInfo. I have download maven library for all the errors I had and it didn't work. I don't know why.
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id='data' class='com.microsoft.sqlserver.jdbc.SQLServerDriver'>
<property name='driverClassName' value ='java.sql.DriverManager'></property>
<property name='dataSource' value ='jdbc:sqlserver://10.222.115.11:1433;databaseName=Test;integratedSecurity=true'></property>
<property name='username' value='HmgDbUser'></property>
<property name='password' value='123456'></property>
</bean>
<bean id='template' class='org.springframework.jdbc.core.JdbcTemplate'>
<property name='dataSource' ref ='data'></property>
</bean>
<bean id='dbi' class='test1.DataBaseInfo'>
<property name='template' ref ='template'></property>
</bean>
</beans>
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>test1</artifactId>
<name> Spring boots test</name>
<version>0.0.2-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssqljdbc6</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
</project>
My Main
package test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
//jdbc:sqlserver:10.200.105.11;databaseName=test;integratedSecurity=true
public static void main(String[] args) {
ApplicationContext conx = new ClassPathXmlApplicationContext("bean.xml");
DataBaseInfo di = (DataBaseInfo)conx.getBean("dbi");
int ins=di.insertPerson(new Person("abdulaziz","riyadh","Saudi","Developer"));
System.out.println(ins);
}
}
When I run it, this error occurs:
Oct 28, 2018 4:35:01 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.microsoft.sqlserver.jdbc.SQLServerDriver] for bean with name 'data' defined in class path resource [bean.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.microsoft.sqlserver.jdbc.SQLServerDriver] for bean with name 'data' defined in class path resource [bean.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:663)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:630)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1491)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1014)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:826)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at test1.Main.main(Main.java:8)
Caused by: java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1015)
at java.lang.ClassLoader.loadClass(ClassLoader.java:413)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:275)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:437)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1430)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1374)
... 10 more
My java version
You have two options.
Use JDK version 10 while compiling your maven module. Version 10 since the exception you have supplied indicates so. Class file format 54 corresponds to Java 10. See here.
Use a version of the JDBC driver compiled for JDK version 8 or matching the JDK version you want to use. But do check the documentation of your database as well, to ensure you pick up the correct driver implementation. Here's one for example:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
Try to use low version jdbc driver from maven

Open ObjectDB explorer whilst running the tomcat server / instance

I'm new to ObjectDB and not sure if I've done something wrong here but I'm getting a couple of issues I'm hoping the community can help me with.
First is this annoying inability to view the db in the explorer when the server is running. If the explorer is open first then the tomcat/server/idea will crash and burn.
I get this:
If on the other hand Explorer is open and I try to restart the server :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.objectdb.o._PersistenceException: Attempt to open a database file '/Users/SteveGreen/Development/Dev Workspace/RatedPlate_Java/RatedPlate/target/RatedPlate/WEB-INF/db/ratedplate.odb' that is currently in use by another process
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
I then need to close connection in explorer and restart the server, however the saga continues as now I receive:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.objectdb.o._PersistenceException: Attempt to open a database file '/Users/SteveGreen/Development/Dev Workspace/RatedPlate_Java/RatedPlate/target/RatedPlate/WEB-INF/db/ratedplate.odb' with an old unsupported format
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
and I need to clean install the mV build before continuing again.
I'm using version 2.2.5 as per the pom, the explorer is from version 2.6.3 as the earlier jar that I downloaded that mapped to my version was not displaying the query option. If I suggest to the IDE that I now use a version other than 2.2.5 then all hell breaks loose and I'm not sure what the best course of action is. As you can imagine, it makes for a pretty shitty debug environment.
Any help is hugely appreciated, thanks!
From my pom:
com.objectdb
objectdb
2.2.5
Persistence:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="RatedPlateDB" transaction-type="RESOURCE_LOCAL">
<provider>com.objectdb.jpa.Provider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="$objectdb/db/ratedplate.odb"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
EDIT: I believe I've found and fixed the upgrade issue - 2.6.3 does not come with Jta or persistence
details: http://m2.objectdb.com
Updated pom:
<dependency>
<groupId>com.objectdb</groupId>
<artifactId>objectdb</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
In embedded mode the database file is locked by one process, so you cannot access the same database (in embedded mode) concurrently in the Explorer and in your application.
To access a database by more than one process at the same time use either client-server mode or embedded-server mode.

While adding <provider> to Persistance.xml it' showing "Invalid content was found starting with element 'provider'."

I am trying to add to my persistance.xml file.
Like Below:
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
When I added to my persistance.xml it's showing
Invalid content was found starting with element 'provider'
How can I overcome this
I have added 2 jars
1.eclipselink.jar
2.javax.persistence_2.0.4.v201112200901.jar
Try to use
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
With this pom.xml
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
I think you're looking at older version of persistence.xml definition. The <provider> element is no longer valid on the java ee 6, check the persistence.xml xsd.
So try either using the latest persistence.xml schema or downgrading your java ee environment.

Categories