I havea java maven3 project in Eclipse IDE with jboss tools installed. I'm using hibenrate4
I'm triing to setup the hibenrate configuratoin in hibernate view to test hql queries, the problem is that is says that it couldnt find the persistence unit. I have the persistence.xml placed in src/main/resources/META-INF/persistence.xml
is there some maven config that I nead to set?
Persistence.xml
<persistence-unit name="ypay">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ypay</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<!-- <property name="hibernate.hbm2ddl.auto" value="validate" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
</properties>
hibernate.properties
hibernate.connection.password=
hibernate.connection.username=root
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.url=jdbc:mysql://****************:3306/ypay?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
You need a META-INF/persistence.xml with
<persistence-unit name="ypay">
(usually put it in WEB-INF/classes/META-INF. When using maven you can try placing it in src/main/resources/META-INF)
Please refer to this doc for details.
Related
My project started with JPA only and it doesn't have Spring. Later, I added Liquibase and I had some issues with the persistence unit name since it is necessary to have one to be able to use EntityManager.
entityManagerFactory = Persistence.createEntityManagerFactory("MyPU");
So, to be able to continue with the tables creation with Liquibase and persisting into the database with JPA, I kept both persistence.xml and liquibase.properties files, despite contaning the same database configuration.
<?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_2.xsd"
version="2.2">
<persistence-unit name="MyPU">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
changeLogFile=src/main/resources/META-INF/database/dbchangelog.xml
url=jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC
username=root
password=root
I've taken a look at liquibase-hibernate and I didn't understand it very well but it seems to be used to generate the diff files, which is not my need at the moment.
Are both files necessary? Is there something I can do to have only one of them?
Liquibase doesn't have a direct way to read the url/username/password information from the presistence.xml file. The liquibase-hibernate extension does add support for diff'ing a database with your java file mapping files, but doesn't change how Liquibase gets the url/username/password.
You said you were not using Spring, but if you are still in a web application, you can use the Liquibase servlet listener to run Liquibase which pulls the connection from a pre-configured datasource. JPA can pull from that same pre-configured datasource instead of re-defining the configuration as well.
Otherwise, unless you want to do a bit of custom Java coding to parse the persistence.xml file and pass it into Liquibase, you do need both files.
To avoid the repetition you could do something like defining build properties in your maven/gradle/whatever setup and have <property name="javax.persistence.jdbc.url" value="${database.url}"/>in your persistence.xml source file, and url: ${database.url} in your liquibase.properties file.
I have created a Maven project with some services that use JPA to connect to my database, this project contains the file persistence.xml with all the config needed.
Now I want to import the jar of that project into a new Spring Boot project in order to use all those services, but when I run the Spring Boot project I get an error.
I have tried to import my services jar in another (non Spring Boot) project and that works, so I think that some config of Spring Boot may be overriding mine.
I have my services project dependency on my pom as follows:
<dependency>
<groupId>org.test.services</groupId>
<artifactId>ServicesProject</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
My persistence.xml file has this config:
<?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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.logging.level" value="INFO"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin://localhost:l521/orcl"/>
<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.globally_quoted_identifiers" value="true" />
</properties>
</persistence-unit>
</persistence>
I get this error when I run the Spring Boot app:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
Hibernate is able to create database structure. Below is example how configuration looks using JPA2 with <property name="hibernate.hbm2ddl.auto" value="create"/>
( Use Hibernate Entity Manager in the case)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
How to make Hibernate to create database SQL script for initialization and update? (that could be run manually or via batch execution) and what tools to use for that.
Yes, there are SQL statements in log (Q Getting SQL script from Hibernate update ), but that is not clean solution.
The goal is to prepare solution for automated database upgrade with every version release.
For automated database upgrades, look at another tool specifically designed for the job. I would suggest Liqiubase.
I did follow exactly as per spec in one of search 8.2.2 Persistence Unit Scope,
but it failed in throwing exception like
15:08:09,956 WARNING [FileZippedJarVisitor] Unable to find file (ignored): file:entities1.jar
java.io.FileNotFoundException: entities1.jar (The system cannot find the file specified)
...similarly other one too
Here is the structure of ear:
|-ear--
|-lib--|... some libs ...
| |--my-persistence-xml.jar
|--entities1.jar
|--entities2.jar
|-ejb-1.jar
|-web-1.war
Here is the persistence:
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/mypu</jta-data-source>
<jar-file>entities1.jar</jar-file>
<jar-file>entities2.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
persistence.xml should be placed in META-INF folder of your ejb-jar module. In this case you can use persistence.xml without <jar-file>. All entites will be registered automaticaly.
If you want use <jar-file> then path should be
<jar-file>../../entities1.jar</jar-file>
<jar-file>../../entities2.jar</jar-file>
what means my-persistence-xml.jar. It's jar or xml?
I have a hibernate project, which uses JPA.
my persistence.xml contents is as follows:
<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="Demo-PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/DemoDS</non-jta-data-source>
<class>com.demo.framework.entity.ReportDefinitionEntity</class>
<properties>
<!-- Database connection -->
<property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
<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" />
<!-- Hibernate dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Output goodies
-->
<property name="hibernate.query.jpaql_strict_compliance" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="false" />
<!-- Cache
-->
<property name="hibernate.jdbc.batch_versioned_data" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Now when I run it using eclipse I don't have a problem, but when I deploy it in Jboss, I get the below error:
ERROR [AbstractKernelController] Error
installing to Start:
name=persistence.unit:unitName=#Demo-PU
state=Create
java.lang.ClassCastException:
org.hibernate.ejb.HibernatePersistence
cannot be cast to
javax.persistence.spi.PersistenceProvider
And here is the list of Jar that I have
activation.jar
antlr-2.7.6.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-tools.jar
hibernate3.jar
javassist.jar
javax.persistence.jar
jdbc2_0-stdext.jar
jta.jar
mysql-connector-java-5.0.5-bin.jar
xml-writer.jar
How can I resolve this issue?
The ClassCastException is caused by having two copies of the javax.persistence APIs in your system (one in the common classloader provided by JBoss and the one in your app). When running on JBoss, you are just not supposed to provide this API in your application, don't package it.
By the way, it seems you're using a JPA 2.0 persistence.xml but I'm not convinced you're using the JPA 2.0 implemenation of Hibernate (actually, you seem to be using a pretty old version since I can see commons-logging.jar). You should probably fix that i.e. use the 1.0 version of persistence.xml.
Actually, you should very likely use a different persistence.xml when running on JBoss (using a JTA entity manager and a jta-data-source). And it seems weird to mix data source usage and Hibernate built-in connection pool.