I have the following problem:
The application is deployed successfully, but whenever i try to execute a query it ends up like this:
Caused by: java.lang.IllegalArgumentException: Not an entity: class javaeetutorial.addressbook.entity.Contact
So here are my configs:
Persistence.xml
<persistence-unit name="address-bookPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/PostgreSQLDataSource</jta-data-source>
<class>javaeetutorial.addressbook.entity.Contact</class>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="show_sql" value="true"/>
</properties>
</persistence-unit>
The entity class is annotated with #Entity of course.
Can anyone help me solve this issue?
I spent two days and found the following solution.
1. Use at least Hibernate 4.3.10:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
</dependency>
2. Change persistence.xml as follows (schema 2.1, provider
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>, two properties - a MUST):
<?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="your_pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/your_src</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm, jar"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
Related
I am new in jpa. I am using eclipselink for that. So i am trying to a simple maven project. But it doesnt work. I am getting this error (full stack trace):
Internal Exception: java.lang.NullPointerException: Cannot invoke "org.eclipse.persistence.internal.sessions.AbstractSession.getName()" because "this.session" is null
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:115)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at main.Main.main(Main.java:16)
Caused by: java.lang.NullPointerException: Cannot invoke "org.eclipse.persistence.internal.sessions.AbstractSession.getName()" because "this.session" is null
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:2027)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:100)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:104)
My main class:
package main;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import dao.PersonDao;
import model.JpaPerson;
public class Main {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("JpaPersonUnit");// block 16. Exception coming from there
EntityManager manager = factory.createEntityManager();
EntityTransaction transaction=manager.getTransaction();
PersonDao pd=new PersonDao(manager);
transaction.begin();
JpaPerson person1=new JpaPerson(0, "Efe", "Yanıkkollu", "Büyük Efe İmparatorluğu");
JpaPerson person2=new JpaPerson(1, "Yavuz Selim", "Yanıkkollu", "Türkiye");
pd.create(person3);
pd.create(person2);
pd.create(person1);
transaction.commit();
System.out.println(pd.getPersonById(2).getName());
transaction.begin();
pd.delete(person3);
transaction.commit();
int count=0;
for(JpaPerson p : pd.read()) {
System.out.println("Name: " + p.getName());
count ++;
}
System.out.println(count);
}
}
my persistence.xml (in META-INF):
<?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="JpaPersonUnit">
<class>model.JpaPerson</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa.schema?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
my pom.xml:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EclipseLink01</groupId>
<artifactId>EclipseLink01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<eclipselink.version>2.6.4</eclipselink.version>
<mysql.version>6.0.4</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</project>
there is nothing wrong with the structure of my entity class. And jpa.schema is exist on mysql.
I have also received this error before when I wanted to run another jpa project. What should I do to fix this?
Your persistence.xml is missing a few points, specifically the transaction type (resource local if outside a container) and the provider. See the oracle docs for a good example, but you might try:
<?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="JpaPersonUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa.schema?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
Netbeans 14 with java 8 or 17.
With a single persistent-unit using the Users.java and UsersJpaController.java classes to access a single table Users, everything works fine.
When I configure a second persistence-unit to access another database,
I get this error (without adding code or creating tables; only add persistence-unit)
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
(default-compile) on project management:
Fatal error compiling: java.lang.RuntimeException:
javax.annotation.processing.FilerException:
**Attempt to recreate a file for type it.lsc.management.db.Users_**
-> [Help 1]
I need to connect to some databases at the same time. Do you have any suggestions to solve the problem?
The file persistence.xml is:
<?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="db_management" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://192.168.0.25\SQLEXPRESS:1433;databaseName=CRM_LSC;encrypt=false"/>
<property name="javax.persistence.jdbc.user" value="uuu"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.password" value="uuu!"/>
</properties>
</persistence-unit>
<persistence-unit name="db_agen" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://192.168.0.195\SQLEXPRESS:1433;databaseName=db_agen"/>
<property name="javax.persistence.jdbc.user" value="uu"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.password" value="uuu!"/>
</properties>
</persistence-unit>
</persistence>
I tried to get Spring-MVC, Hibernate, JTA with a Postgres Server to work.
I got most of the stuff working (Read from Db through EntityManager without JTA), but I can't get the transactions to work with JTA.
<?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="defaultPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>???</jta-data-source>
<class>net.test.test.database.UsersEntity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="validpw"/>
</properties>
</persistence-unit>
If I provide my datasource from my servlet I get an Exception like
DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'dataSource'
I also found resources saying the datasource should be defined in the application-server (but I am not sure about this one). If it is important I use a Tomcat-server.
I have embedded derby database and i work with jpa. This is my 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="pers">
<class>entities.Leverancier</class>
<class>entities.Prijsproduct</class>
<class>entities.Product</class>
</persistence-unit>
</persistence>
What should i change or add to get this working. When I run my code now I get the following:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at test.Test.main(Test.java:19)
Your persistence.xml is not correct. Look at a sample below:
<persistence-unit name="MyAppPU" transaction-type="RESOURCE_LOCAL">
<!-- This is where you mention your JPA runtime provider e.g. it's EclipseLink here -->
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>mypkg.MyEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/my_schema"/>
<property name="javax.persistence.jdbc.password" value="pass"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
</properties>
</persistence-unit>
You also have to make sure that you put your JPA provider jar files (along with the derby-client jar) in the classpath.
For some reason hibernate is not catching issues like mapping entities to tables that do not exist. My persistence.xml file looks like this...
<?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_1_0.xsd" version="1.0">
<!-- A JPA Persistence Unit -->
<persistence-unit name="printLogixJpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.printlogix.rp.server.domain.User</class>
<class>com.printlogix.rp.server.domain.UserImage</class>
<class>com.printlogix.rp.server.domain.TemplateInstance</class>
<class>com.printlogix.rp.server.domain.UserOrder</class>
<class>com.printlogix.rp.server.domain.OrderLineItem</class>
<class>com.printlogix.rp.server.domain.OrderStatus</class>
<class>com.printlogix.rp.server.domain.AgencyImage</class>
<class>com.printlogix.rp.server.domain.Agency</class>
<class>com.printlogix.rp.server.domain.Brokerage</class>
<class>com.printlogix.rp.server.domain.SystemTemplate</class>
<class>com.printlogix.rp.server.domain.TemplateType</class>
<class>com.printlogix.rp.server.domain.Product</class>
<class>com.printlogix.rp.server.domain.ProductDefinition</class>
<class>com.printlogix.rp.server.domain.ExpeditedAddress</class>
<class>com.printlogix.rp.server.domain.BrokerageUser</class>
<class>com.printlogix.rp.server.domain.StateProvince</class>
<class>com.printlogix.rp.server.domain.Country</class>
<class>com.printlogix.rp.server.domain.AwardClub</class>
<class>com.printlogix.rp.server.domain.SuperUser</class>
<class>com.printlogix.rp.server.domain.AgencyUser</class>
<class>com.printlogix.rp.server.domain.RememberMe</class>
<class>com.printlogix.rp.server.domain.PasswordResetRequest</class>
<class>com.printlogix.rp.server.domain.SystemTheme</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true"/>
<!--Begin Credentials -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/realtorprint_dev?autoReconnect=true"/>
<property name="hibernate.connection.username" value="test"/>
<property name="hibernate.connection.password" value="test"/>
<!--End Credentials -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>
Try adding the property "hibernate.hbm2ddl.auto" with value of "validate":
<property name="hibernate.hbm2ddl.auto" value="validate"/>