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?
Related
When I am running my unit test I these two errors
org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve
INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
and
javax.persistence.PersistenceException: No Persistence provider for EntityManager named dbContext
I don't get them both because as you can see in the image en persistence.xml file below this text everything is there that he is complaining about.
and here you have my persistance.xml
<?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="dbContext">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="{urltodb}"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="{myusername}"/>
<property name="hibernate.connection.password" value="{mypassword}"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Your folder structure is completely mixed up:
Your src-Folder is marked as source-folder (blue), so the subfolder main is interpreted as Java-Package
Your resources-folder is within your sources/main-folder That's why Intellij interprets it as package main.resources.META-INF.
(All your real packages start with upper-case. That's not an error, but unusual nevertheless)
How your folder structure should look like:
src/main/java => Mark this as source folder and put all your java packages there
src/main/resources => Mark this as resources folder an put your META-INF there
See also: How to create a test directory in Intellij 13?
I need to fill my database on ear startup, so I add javax.persistence.sql-load-script-source tag with reference to the file.
<persistence-unit name="MyPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/jboss/datasource/mydatasource</jta-data-source>
<jar-file>Entities.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.level.sql" value="ALL"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/defaultdata.sql"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/insertnations.sql"/>
</properties>
</persistence-unit>
It works, but now i want to use two file because i want to separate "most static" data (like nations and regions of the world) from "per-deploy" data (like some configurations).
Using the tag twice not works.
Is there something wrong?
It is possible to do what i want?
The persistence unit is used in a EAR. I use Wildfly and eclipselink 2.5.2 as persistence provider
I am involved in writing a project which requires servlet->database connection. I am collaborating with another person who have designed a database using HyperSQL (hsqldb), I am now trying to merge my project with his by adding his code to mine.
Further to my issue though. When I copy the code, it works, usually. I have few methods that use the data from the database and compare them with user input.
When attempting to connect to the database, I would randomly succeed or fail, getting the following error ;
Unable to acquire a connection from driver [null]
I of course initialise a driver ;
Class.forName("org.hsqldb.jdbcDriver").newInstance();
Now, when running my method, it sometimes succeeds, and sometimes fails, here is the XML file for the DB;
<?xml version="1.0" encoding="UTF-8"?>
<!--
Creates both the HyperSQL databases using hibernate. No password or username is set.
-->
<persistence version="1.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_1_0.xsd">
<persistence-unit name="monsters" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.Monster</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:monsters"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="users" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:users"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Probably an attempt is made to open the database in a readonly directory.
You need to specify a username for both persistence units. The default username is "SA".
The file path of the database that you specify in the URL is relative. It resolves to the execution directory. As you are developing a web application, you need to specify a directory that can be written to.
One way of doing this is by including a variable in the path, such as "jdbc:hsqldb:file:{$directorypath}/monsters" where the directorypath is the name of the web application's data dirctory, as specified in your web.xml file.
I have a standalone java application, which uses JPA for its persistence.
Right now I have a persistence.xml in META-INF.My application is currently in development.
My question is that if I move from development to the next envirnoment, say QA. I have to modify the persistence.xml and rebuild the jar. Is this the right way to go about it ?
If not,if I move the connection properties to a different file, where should this file be placed?
<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_2_0.xsd" version="2.0">
<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ClassA</class>
<class>ClassB</class>
<class>ClassC</class>
<class>ClassD</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url"
value="url" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
Thanks in advance !
That's a good question. Normally, you put all these environment settings in an external file, say application.properties, and pass the location to it to the JVM when you start your application (e.g. -Dconfig.location=/conf/)
Then you should find a way to get the externalized properties into your EntityManagerFactory. You can't do that in persistence.xml, you can only hard-code things there. But you can do it when creating the entity manager factory by passing vendor properties.
If using a framework like spring, for example, this is easier to do, as spring provides a factory bean for the entity manager. Otherwise you should handle it yourself. Here's the relevant bit from spring:
provider.createEntityManagerFactory(persistenceUnitInfo, getJpaPropertyMap())
I have an application which manages 3 databases. I use hibernate with JPA on seam framework.
So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and db3):
<persistence-unit name="db1" transaction-type="JTA" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>db1source</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.default_schema" value="SI_TEC" />
<property name="hibernate.validator.apply_to_ddl" value="false" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
</properties>
</persistence-unit>
<persistence-unit name="db2" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d2source</jta-data-source>
</persistence-unit>
<persistence-unit name="db3" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d3source</jta-data-source>
</persistence-unit>
In my seam components.xml file, I create 3 managed-persistence-context to map seam with my hibernate configuration.
Finally, I have several entities and my problem is here. I need to persist some entities in db2 and other in db3. So database schema are different and when I deploy my application, I get this error:
org.hibernate.HibernateException: Missing table: PORTAILPERMISSION
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1113)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
Truncated. see log file for complete stacktrace
Because, the table PORTAILPERMISSION doesn't exist in db2.
My question is:
How to specify in entity class what database (or persitence-unit) must be used to validate entity in startup ?
Thanks for your help.
You try to explicitly list classes (<class>..</class>) in each persistence unit. And use
<exclude-unlisted-classes>true</exclude-unlisted-classes>