ClassCastException while using find() for javax.persistence.EntityManager - java

While attempting to retrieve an Entity from a Database, using JPA's EntityManager, I am getting a ClassCastException that references two identical Classes. After reading other posts/articles, I suspect that the application using 2 different Class Loaders on the same Class type may be the culprit. Unfortunately, I have no idea how to verify nor correct this. Could someone help shine some light on this situation and its solution?
Entity Member.java
package com.example.mysql_flyway_database.entities;
import javax.persistence.*;
#Entity
#Table(name = "_member_")
public class Member {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer memberId;
#Column(name = "member_name")
private String memberName;
public Integer getMemberId() {
return memberId;
}
... // other getter/setter methods
}
persistence.xml - stored in /resources/META-INF/, in case this matters
<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="Member" transaction-type="RESOURCE_LOCAL">
<class>com.example.mysql_flyway_database.entities.Member</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/JDND_C3"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="Keep1Moving2Forward3!"/>
</properties>
</persistence-unit>
</persistence>
MyApplication
...
private static void readMembersAsEntities(EntityManagerFactory factory) {
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
Member member = manager.find(Member.class, 1);
System.out.println(String.format("Member %s was found", member.getMemberName()));
}
...
Here is the log:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.ClassCastException: com.example.mysql_flyway_database.entities.Member cannot be cast to com.example.mysql_flyway_database.entities.Member
at com.example.mysql_flyway_database.MysqlFlywayDatabaseApplication.readMembersAsEntities(MysqlFlywayDatabaseApplication.java:51)
at com.example.mysql_flyway_database.MysqlFlywayDatabaseApplication.main(MysqlFlywayDatabaseApplication.java:42)
... 5 more
Note:
This is quite similar to the issue underlined here:
JPA class loading issue with Hibernate.
Disclosure: I am fairly new to Spring and its plugins/dependencies (currently learning), so I am a bit lost on the provided solution described in the post mentioned. Regarding the solution mentioned in the provided post: In my project, I do not see /WEB-INF/lib.
Any help would be greatly appreciated.

Related

Can't connect to JDBC From persistence.xml

I have plain java class book annotated as Entity for jpa.
I configure persistence xml, but i have big stack trace with exception which i don't understand. Also, i can't find jdbc package in javax.persistence. What do me?
This is my Entity
package Java.JPA;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
public class Book
{
#Id #GeneratedValue
private long id;
private String title;
public Book()
{
}
public Book(String title) {
this.title = title;
}
public String getTitle()
{
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
this is my main class
package Java;
import Java.JPA.Book;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class main
{
public static void main(String[] args) throws SQLException, ClassNotFoundException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");
EntityManager em = emf.createEntityManager();
Book book = new Book("TestJPA");
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(book);
tx.commit();
em.close();
emf.close();
}
}
and this is my persistence.xml
<?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="example">
<!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>-->
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Java.JPA.Book</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/studyJPA"/>
<property name="javax.persistence." value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
</persistence>
stack trace:
"C:\Program Files\Java\jdk1.8.0_92\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.2\lib\idea_rt.jar=55859:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_92\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_92\jre\lib\rt.jar;C:\Tomcat\apache-tomcat-9.0.7\lib\servlet-api.jar;C:\Users\Anton\Desktop\study\target\classes;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\javaee-api-7.0-1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jsp-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\servlet-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\el-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jasper.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\catalina.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\ecj-4.6.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jasper-el.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jaspic-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-jni.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\catalina-ha.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\serp-1.15.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-dbcp.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-jdbc.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-util.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xalan-2.7.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\catalina-ant.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\hawtbuf-1.11.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\howl-1.0.1-1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\hsqldb-2.3.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jasypt-1.9.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\mimepull-1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\neethi-3.0.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wsdl4j-1.6.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xmlsec-2.0.6.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\joda-time-2.7.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openjpa-2.4.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\stax-ex-1.7.4.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-coyote.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\websocket-api.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activation-1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\bval-jsr-1.1.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jaxb-api-2.3.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\stax-api-1.0-2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-i18n-es.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-i18n-fr.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-i18n-ja.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\bval-core-1.1.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\catalina-tribes.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-cli-1.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cryptacular-1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-core-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jaxb-core-2.3.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\jaxb-impl-2.3.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\stax2-api-3.1.4.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\sxc-runtime-0.8.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-lang-2.6.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\saaj-impl-1.3.23.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\serializer-2.7.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\slf4j-api-1.7.21.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-util-scan.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomcat-websocket.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-jdbc-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xbean-naming-4.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xml-resolver-1.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-dbcp2-2.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-lang3-3.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-pool2-2.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-api-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-cxf-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-javaagent.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-jee-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\sxc-jaxb-core-0.8.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-jaxrs-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xbean-reflect-4.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-ra-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-codec-1.10.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-wsdl-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\java-support-7.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\johnzon-core-1.0.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\myfaces-api-2.2.12.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-core-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-ejbd-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-hsql-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-http-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-rest-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\slf4j-jdk14-1.7.21.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-common-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-loader-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-webapp-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-policy-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\bcprov-jdk15on-1.60.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-logging-1.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\johnzon-jaxrs-1.0.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\johnzon-jsonb-1.0.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\myfaces-impl-2.2.12.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-core-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-mojarra-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-myfaces-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\catalina-storeconfig.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-digester-1.8.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-jcs-core-2.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\johnzon-mapper-1.0.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-client-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-cxf-rs-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-loader-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-server-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\swizzle-stream-1.6.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-catalina-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-bindings-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xmlschema-core-2.2.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-protobuf-1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-ws-addr-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-ee-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xbean-asm6-shaded-4.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xbean-bundleutils-4.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-broker-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-client-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-jcs-jcache-2.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-security-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-ejb-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-jsf-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-spi-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-web-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-beanutils-1.9.3.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-client-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-ws-policy-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-saml-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-soap-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-el22-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-impl-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\tomee-webservices-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\woodstox-core-asl-4.4.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\xbean-finder-shaded-4.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-management-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\geronimo-connector-3.1.4.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-saml-impl-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xacml-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\commons-collections-3.2.2.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-ws-security-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-webservices-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xacml-impl-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xmlsec-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-jdbc-store-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-bindings-xml-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\geronimo-transaction-3.1.4.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\mbean-annotation-api-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-profile-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xmlsec-impl-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\quartz-openejb-shade-2.2.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-bindings-soap-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-json-basic-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-security-saml-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\geronimo-jsonb_1.0_spec-1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-cxf-transport-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-jee-accessors-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-security-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\taglibs-standard-impl-1.2.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\taglibs-standard-spec-1.2.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-ws-security-dom-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-kahadb-store-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-frontend-jaxrs-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-frontend-jaxws-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-security-impl-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openwebbeans-ee-common-1.7.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-ws-security-stax-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\batchee-jbatch-0.4-incubating.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-frontend-simple-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-transports-http-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\openejb-jpa-integration-7.1.0.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xacml-saml-api-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\taglibs-standard-jstlel-1.2.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-databinding-jaxb-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-security-cors-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-security-jose-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\opensaml-xacml-saml-impl-3.1.1.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-ws-security-common-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\activemq-openwire-legacy-5.14.5.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-security-oauth2-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-extension-search-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\wss4j-ws-security-policy-stax-2.1.9.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-extension-providers-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-security-jose-jaxrs-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\cxf-rt-rs-service-description-3.1.15.jar;D:\Program\TomEE+\apache-tomee-plus-7.1.0\lib\geronimo-javamail_1.4_mail-1.9.0-alpha-2.jar;C:\Users\Anton\.m2\repository\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar;C:\Users\Anton\.m2\repository\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;C:\Users\Anton\.m2\repository\org\hibernate\hibernate-core\5.3.6.Final\hibernate-core-5.3.6.Final.jar;C:\Users\Anton\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\Anton\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\Anton\.m2\repository\org\javassist\javassist\3.23.1-GA\javassist-3.23.1-GA.jar;C:\Users\Anton\.m2\repository\net\bytebuddy\byte-buddy\1.8.17\byte-buddy-1.8.17.jar;C:\Users\Anton\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\Anton\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\Anton\.m2\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\Users\Anton\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\Anton\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\Anton\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\Anton\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\javax.persistence\2.2.1\javax.persistence-2.2.1.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.jpa\2.7.3\org.eclipse.persistence.jpa-2.7.3.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.asm\2.7.3\org.eclipse.persistence.asm-2.7.3.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.antlr\2.7.3\org.eclipse.persistence.antlr-2.7.3.jar;C:\Users\Anton\.m2\repository\javax\json\javax.json-api\1.1.2\javax.json-api-1.1.2.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.jpa.jpql\2.7.3\org.eclipse.persistence.jpa.jpql-2.7.3.jar;C:\Users\Anton\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.core\2.7.3\org.eclipse.persistence.core-2.7.3.jar" Java.main
91 INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when they are loaded by the JVM.
121 INFO [main] openjpa.Runtime - Starting OpenJPA 2.4.3
Exception in thread "main" <openjpa-2.4.3-r422266:1833086 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#2655aabb".
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:71)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:850)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:603)
at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1520)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:535)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:460)
at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:121)
at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:973)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:964)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:642)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:202)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:154)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
at Java.main.main(main.java:15)
Process finished with exit code 1
in dependencies pom.xml there are hibernate and mysql driver. Also, i import javaee-api-7.0-1.jar, servlet-api.jar and jsp-api.jar from tomcat's directory. Please help. More than several hours i trying to solve it. I will be grateful!
this is my java ee jar file. There is not jdbc package or another files with name "jdbc"
enter image description here
maybe, i downloaded wrong tomcat ee. If this is true, give me a link for download correct version.
I don't know what happened, but i have new exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/geronimo/osgi/locator/ProviderLocator
at javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver.getPersistenceProviders(PersistenceProviderResolverHolder.java:108)
at javax.persistence.Persistence.getProviders(Persistence.java:275)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:88)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:72)
at Java.main.main(main.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.geronimo.osgi.locator.ProviderLocator
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
It's magic:) Now i have a question. How to fix that?
<persistence>
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<class>org.hibernate.ejb.test.Apple</class>
<class>org.hibernate.ejb.test.Distributor</class>
<class>org.hibernate.ejb.test.Fruit</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.ejb.classcache.org.hibernate.ejb.test.Item"
value="read-write"/>
<property name="hibernate.ejb.collectioncache.org.hibernate.ejb.test.Item.distributors"
value="read-write, RegionName"/>
</properties>
</persistence-unit>
</persistence>

Error with persistence using EclipseLink and UCanAccess

I am trying to develop an app for exercise reasons. I am using MSAccess 2010 as the database with UCanAccess (3.06) as the driver and the EclipseLink 2.1 as the entity framework.
I am stuck in adding new records to the database. Here the error code:
Internal Exception: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 user lacks privilege or object not found: IDENTITY_VAL_LOCAL
Error Code: -5501
Call: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
Query: ValueReadQuery(name="SEQ_GEN_IDENTITY" sql="SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1")
It seems to me that the autogenerate of the id fails. The entity class was generated vie Netbeans and looks like this:
#Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "ID")
private Integer id;
By default, EclipseLink tries to automatically detect the underlying database and generate SQL statements using the appropriate SQL dialect. That apparently isn't working for you because the SQL statement to retrieve the last created identity value is not recognized by UCanAccess .
You could try adding a target-database directive to your EclipseLink configuration specifying SQLServer in an attempt to get a working SQL statement (SELECT ##IDENTITY) to retrieve the last created ID value. However, bear in mind that there are significant differences between T-SQL and Access SQL so you will probably continue to encounter other compatibility issues between EclipseLink and UCanAccess.
before knowing above answer i was also facing same problem for inserting new record in access Database ,
Thanks to Mr. Gord Thompson to give a great Solution for me ,
and it is working too.
i have just added one line in my persistence.xml file..
property name="eclipselink.target-database" value="HSQL"
<?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="OnePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>design_frames.One</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:ucanaccess://C:\One\One.accdb"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.driver" value="net.ucanaccess.jdbc.UcanaccessDriver"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="HSQL"/>
</properties>
</persistence-unit>
</persistence>

JPA Entity not mapped exception

I am using JPA and java as technology and writing very simple web application.
There is one class called Employee, look like below code
#Entity
#Table(name="Employee")
public class Employee{
#Id
private int id;
#Column(name="name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
one table called Employee is exist into database.
Its persistence.xml file looks like below code.
<?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="JPAService" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- Entity classes -->
<class>com.solution.domain.Employee</class>
<properties>
<!-- The JDBC driver of your database -->
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<!-- The database username -->
<property name="javax.persistence.jdbc.user" value="postgres" />
<!-- The database password -->
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="javax.persistence.jdbc.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
</properties>
</persistence-unit>
</persistence>
Employee class entry exist in this xml file.
There are not issue while startup of application. Every thing working fine.
But when query is executed then it throw and exception.
"Employee is not mapped [select e from Employee e]"
EntityManager em = getEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("select e from Employee e");
List<Employee> employees = query.getResultList();
if(employees != null){
return employees.get(0);
}
em.getTransaction().commit();
I am unable to understand where mapping is missing. As i already mentioned its a web project. So for more clarification i am attaching one screen shot of my project too.
Thanks in advance.
I got the problem. It was an silly mistake. I was using JPA but in Employee class I used the Entity annotation from hibernate implementation instead of JPA package.
Side note: It's a bad idea to model the database with JPA, e.g., by implementing the integer surrogate key in the data model. JPA is supposed to help persist an object model, which would use the natural "key" to distinguish objects. A JPA entity should only expose attributes of the object model. This has the benefit of greatly simplifying the expression of relationships because you relate objects, not ID fields. Don't use JPA for database translation, use it for object persistence.

OpenJPA L2 Cache not caching related objects

I have a simple data model which consists of a parent object with a one-to-many relationship to a list of child objects.
#Entity
public class Client implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(cascade = CascadeType.ALL)
private List<Session> sessions;
...
}
#Entity
public class Session implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(length = 200)
private String location;
...
}
I have enabled L2 caching using OpenJpa in the persistence.xml:
<?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="puOpenJPA_Gym" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>java:jboss/datasources/GymTracker</jta-data-source>
<class>za.co.blacklemon.entities.Client</class>
<class>za.co.blacklemon.entities.Session</class>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="jboss.as.jpa.providerModule" value="org.apache.openjpa"/>
<property name="openjpa.DynamicEnhancementAgent" value="false"/>
<property name="openjpa.Log" value="File=stdout, DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.QueryCache" value="true(CacheSize=10000, SoftReferenceSize=1000)"/>
<property name="openjpa.DataCache" value="true(CacheSize=20000, SoftReferenceSize=1000)"/>
</properties>
</persistence-unit>
</persistence>
I then get individual clients by calling find and then accessing the list of sessions. The second line here triggers OpenJPA to populate the child objects from the DB:
Client client = dao.find(Client.class, id);
System.out.println(client.getSessions().size());
The first time this runs there appears two queries in the log, one for the parent object and one each for any children:
executing prepstmnt 782097742 SELECT t0.contactNumber, t0.description, t0.firstName, t0.photo, t0.surname FROM Client t0 WHERE t0.id = ? [params=?]
executing prepstmnt 626498798 SELECT t1.id, t1.location FROM Client_Session t0 INNER JOIN Session t1 ON t0.SESSIONS_ID = t1.id WHERE t0.CLIENT_ID = ? [params=?]
The second time this runs the initial query is gone, as the object is retrieved from the cache, but the second (and possibly more) query is still executed.
Why does OpenJPA not store the child objects in a one-to-many relationship in the cache?
I'm using OpenJPA 2.3.0 on Java 7.
Why does OpenJPA not store the child objects in a one-to-many relationship in the cache?
OpenJPA stores the child objects, but it doesn't store the FK back into the parent. I fixed this problem in OpenJPA trunk(2.4.x) via OPENJPA-2285.

Can one entity be persisted to multiple persistence units using OpenJPA?

I'm trying to save an entity to two separate persistence units one after the other. I can successfully save the entity to the first unit, I then detach it from that unit, reset the #Id value and persist to the second, but it appears that the object still has an associated id that is possibly not set-able? I think it's called the oid? The error:
Caused by: <openjpa-2.2.0-r422266:1244990 nonfatal store error>
org.apache.openjpa.persistence.EntityNotFoundException: The instance
of type "class za.co.core.ejb.entities.Address" with oid "4" no longer
exists in the data store. This may mean that you deleted the instance
in a separate transaction, but this context still has a cached version.
I know I can create a brand new object and copy the values I want across, but I want to do this generically without knowing too much about the object itself.
My code looks like this:
#PersistenceContext(unitName = "puOpenJPA_MSSQL",
type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
#PersistenceContext(unitName = "puOpenJPA_MSSQLaudit",
type = PersistenceContextType.TRANSACTION)
private EntityManager auditManager;
...
entityManager.persist(entity);
entityManager.detach(entity);
entity.setId(null); //this sets the #id property of the entity to null
auditManager.persist(entity); //exception thrown
And here is the persistence.xml
<?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="puOpenJPA_MSSQL" transaction-type="JTA">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<jta-data-source>
java:jboss/datasources/mySqlSandbox
</jta-data-source>
<class>
za.co.core.ejb.entities.AuditableEntity
</class>
<class>za.co.core.ejb.entities.Address</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
<property name="jboss.as.jpa.providerModule"
value="org.apache.openjpa" />
<property name="openjpa.DynamicEnhancementAgent"
value="false"/>
</properties>
</persistence-unit>
<persistence-unit name="puOpenJPA_MSSQLaudit" transaction-type="JTA">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<jta-data-source>
java:jboss/datasources/mySqlSandboxAudit
</jta-data-source>
<class>za.co.core.ejb.entities.AuditableEntity</class>
<class>za.co.core.ejb.entities.Address</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
<property name="jboss.as.jpa.providerModule"
value="org.apache.openjpa" />
<property name="openjpa.DynamicEnhancementAgent"
value="false" />
</properties>
</persistence-unit>
</persistence>
Thanks,
Sean
In theory yes because entities are "plain old java objects", but in practice to make all that magic work the persistence provider proxies parts of it, like collection members. As soon as you persist it, its not 'your' entity anymore - its part of the provider's bookkeeping.
If you want to persist the same entity multiple times, clone it multiple times and persist each individual copy.

Categories