Different object models in same JPA persistence.xml - java

I'm using JPA to work with a couple of databases with different object model in them.
persistence.xml
<persistence-unit name="unit_1" transaction-type="RESOURCE_LOCAL">
<description>pu-pu-pi-du</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>my.package.items1.Class1_1</class>
<class>my.package.items1.Class1_2</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- uncomment to show sql queries in System.out -->
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<!-- Envers -->
</properties>
</persistence-unit>
<persistence-unit name="unit_2" transaction-type="RESOURCE_LOCAL">
<description>Persistance config</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>my.package.items2.Class2_1</class>
<class>my.package.items2.Class2_2</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- uncomment to show sql queries in System.out -->
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<!-- Envers -->
</properties>
</persistence-unit>
When I create factory this way:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("unit_1", properties);
It works well, but updates the databast of unit_1 with all of the classes from unit_2. factory.metamodel.entities will contain all 4 classes:
my.package.items1.Class1_1
my.package.items1.Class1_2
my.package.items2.Class2_1
my.package.items2.Class2_2
And two more tables from model are added to database from unit_1.
How to explain JPA, that database of unit_1 has nothing common with unit_2 and I don't need its mappings?

Just add the following param after ''class'' tags in both of ''persistence-unit''
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Related

Creating schema from sql file looks OK, but tables are not found

In persistence.xml I am creating a database (in-memory database for test) like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jar-file>pathToMyJar.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
That works as expected but is slow. So I saved a sql-script for schema generation like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jar-file>pathToMyJar.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="pathToSchemaGeneratonScript.sql"/>
</properties>
</persistence-unit>
And then tried to use it like this:
<persistence-unit name="TestDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="pathToSchemaGeneratonScript.sql"/>
</properties>
</persistence-unit>
Since show_sql is set to true I can see that the script runs, e.g "create table xxx (...".
But no tables seems to exist, e.g:
entityManagerFactory = Persistence.createEntityManagerFactory("TestDatabase");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
Query query = entityManager.createNativeQuery("insert into xxx (someColumns) values (someValues)");
query.executeUpdate();
Will result in "org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table "xxx" not found".
I have connected manually and verified that the tables are not there. I also applied the generated sql file manually and then the tables are created as expected.

Hibernate errors : No Persistence provider for EntityManager named my-unit

i've tried to put my persistence.xml in various parts of my Java project and i call it with this command:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("my-unit");
but it returns me an error:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at main.MainClass.main(MainClass.java:81)
Process finished with exit code 1
my persistence.xml file:
<?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="my-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<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"/>
<!-- use encache provider-->
<!--<property name="hibernate.cache.region.factory_class"-->
<!--value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>-->
<!--<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>-->
<!--<!–use second level cache–>-->
<!--<property name="hibernate.cache.use_second_level_cache" value="true"/>-->
<!--<!–use 3rd level cache–>-->
<!--<property name="hibernate.cache.use_query_cache" value="true"/>-->
</properties>
</persistence-unit>
I use IntelliJ as IDE for Java and the persistence.xml file is under META-INF folder in resources folder, i have tried to check the project structure and all looks like nice.
I use hibernate as maven repository.
Thanks for helping
You need to define the persistence provider name
<provider>org.hibernate.ejb.HibernatePersistence</provider>
That will solve the problem.

JPA / Hibernate : schema generation with multiple persistence units

I have an application that uses a set of JPA entities that are located in 2 different databases. I configured it with multiple persistence units.
The problem is that I want to auto-generate the schema using schema-generation, and all the entities are created in both databases.
I have in both PUs:
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
And, yes, I want to use the metadata to get the entities automatically. I do not want to provide a manual script, because I would need to keep it up to date with the entities.
Is there a way to mark which entity to be generated by which PU?
-edit: please be aware that adding "schema" property on #Table does not resolve the problem, because each PU will try to create the same entity in the right schema, and there will be errors because the tables will already exist.
Yes, you can do that. You need to list the entities under each persistant unit, and also DISABLE the auto discovery of the unlisted entities explicitly with <exclude-unlisted-classes>true</exclude-unlisted-classes>.
<!-- Unit 1 -->
<persistence-unit name="Unit1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.your.class.A</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.username" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
</properties>
</persistence-unit>
<!-- Unit 2 -->
<persistence-unit name="Unit2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.your.class.B</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.username" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
</properties>
</persistence-unit>
Edit
If you are using annotations configuration, then
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setPackagesToScan("com.A");
And another factory for another entity manager with a different package name.
I found a way: I will use "schema" property in #Table annotation for each entity and then I would enable only one PU to auto generate the tables.

Hibernate/JPA not validating against DB schema on startup

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"/>

Persistence unit is not persistent

I need persistence unit that creates embedded database which stays persistent after closing EntityManager.
This is my PU:
<persistence-unit name="hello-jpa" transaction-type="RESOURCE_LOCAL">
<class>hello.jpa.User</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:target/hsql.db"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
And it deletes data after closing application.
My understanding of the documentation is that the "old" URL jdbc:hsqldb:. creates or connects to the same database as the new form for the URL jdbc:hsqldb:mem:. (i.e. in memory).
So try with the following URL instead: jdbc:hsqldb:file:target/hsql.db.

Categories