hibernate with JPA to generate entity in other schema - java

I have created two entities in my JAVA code one is Account entity, another is AccountLog entity. These two entities mapped to the corresponding table in same schema named testdb. We use hibernate and JPA to handle the insert/update and table generation.
Since the performance issue, I would like to separate the AccountLog into other schema named testdb_log. So that the AccountLog table will be generated in schema testdb_log and the next insert/update event will be stored in the schema testdb_log.
What is the best solution to handle the above cases? Add #table annotation with schema name? or others?
How to generate the entity to other schema?
For this entity, how to save/update this entity information to other schema?

I will probably use different persistence units in the persistence xml, something like this:
<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="oneschema" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${datasource.baseurl}/SCHEMAONE" />
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</properties>
</persistence-unit>
<persistence-unit name="anotherchema" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.company.AccountLog</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${datasource.baseurl}/SCHEMANOTHER" />
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</properties>
</persistence-unit>
</persistence>
In the second persistence unit you declare explicitly what classes belong to it, so the entity manager will know how to deal with them.
Schema, connections, connection pools are usually transparent to the java code so you shouldn't change anything in java.

Related

Can it lead to problems while using the same Hibernate managed database for multiple projects?

I'm using Wildfly with Hibernate and I will have two different projects accessing the same database. Each project has its own persistence.xml, but the datasources within the persistence.xml are the same. Currently I have one project with these datasources. This looks like this:
Project A:
<?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="MyProjectPersistenceUnit" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myprojectDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
<persistence-unit name="MyProjectLoggingUnit" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:jboss/datasources/myprojectDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>
The additional project's persistence.xml will look like this:
Project B:
<?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="MyProjectLoggingUnit" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myprojectDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
</persistence>
The first project uses both JTA and RESOURCE_LOCAL transactions, so I can handle logging "manually". The second project uses JTA only since I only do logging operation there. Project A does CRUD operations, Project B does create operations only. Both projects run within the same Wildfly server. Is it possible that any problem, maybe with locking in database, transactions in Wildfly or whatever, could occur between project A and project B while accessing the same database with the same datasources from different projects as I do it?
I don't think so, but I'm afraid that some 'side effects' could occur I don't know yet.
There should not be any problem, as Datasource is managed by jboss, it will allocate connection as per your configuration, locking can occure if you are using same row for processing from different projects but let database take care of that.
And there are different entities involved for not creating any problem like tho TCP connection is same/ shared sessions and transactions are diffrent for operation, spring and hibernate both are mature and best what they do, unless you messed with configurations ;), your looks good.
I agree that in principal you should not worry! BUT, since we are not dealing with magic and there is always an explanation when something does not work, the only case you should worry is a potential business coupling between the 2 applications and of course ends up in the data base. What I am trying to say, is that if Application1 which uses the same DB with Application2, performs things on entities that eventually are expected to be visible/ update for the logic on the other application, then yes there might be a chance that you will face some technical deadlocks (eventually pretty fine for a DB) but could be a problem on the business code level.
It is a matter of design and higher coupling, which happens to result in a technical coupling on the DB. I am not sure if I describe it correct, as a high level concern :)

How Persistence Unit works in JPA

I am trying to learn JPA with Hibernate implementation. There are lot of blog of net about this but still i am struggling to implement it. I have written one Dao class which creates the EntityManagerFactory. But while looking this PERSISTENCE_UNIT it says
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
private final String PERSISTENCE_UNIT = "test";
private EntityManager entityManager;
public GenericDao() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = factory.createEntityManager();
}
I also created one persistence.xml file which is in META-INF folder of web application. It looks like this.
<?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="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:1532/test" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
For deployment Tomcat8 is being used. Please let me know why application not able to lookup PERSISTENCE_UNIT.
As per comment - the problem is your persistence.xml location. It needs to be exactly where your application expects it to be:
WEB-INF/classes/META-INF/persistence.xml
Now, why did you encounter this problem is another matter, connected with how you create your .war file. Solution that should work however, is trusting Maven to take care of that. If you set packaging to war, and put your META-INF folder into resources (src/main/resources/META-INF/persistence.xml) it should take care of things.

how to automatic create table in jpa persistence xml file?

I am using eclipse IDE.I also try following two .but failed..when i manually create table in my mysql database then my complete program run fine... I want create table automatic with respect to entity class.
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.hbm2ddl.auto">update</property>
here my persistence 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="JpaTest2" transaction-type="RESOURCE_LOCAL">
<class>com.jpa.Employee</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate"/>
<property name="javax.persistence.jdbc.user" value="umar"/>
<property name="javax.persistence.jdbc.password" value="umar"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
Dont use Hibernate specific options. JPA 2.1 provides
javax.persistence.schema-generation.database.action
that can be set to "create", "drop", "drop-and-create", "none".
That way you keep your code JPA implementation independent
Check your entity. Did you miss #Table annotation? The exception clearly says that the table is missing 'hibernate.employee':
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.employee' doesn't exist at ...
If you defined a naming strategy that prepends all tables with hibernate., then make sure that the tables are created in MySql.

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.

OpenJpa in Apache Geronimo 3.0

I have a newbie question, so I hope you can help.
Currently as ORM tool I am using OpenJPA 2.2.1 .
MY CUrrent persistence UNit:
<?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" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
<persistence-unit name="FleaCircusLocal" transaction-type="RESOURCE_LOCAL">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>FleaCircusOracleDS</non-jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
</persistence>
For the sake of simplicity, persistence-unit "FleaCircus" is what I use ( Container Managed Persistence ). FleaCircusLocal is another test pu I created , where I've created a sample stateless bean that basically starts a new transaction, persist an element and commit it.
However it doesn't sinchronize immediatly and customer complains about it.
Is there any way to make OpenJPA makes whats in context immediatly visible to the DB or is it impossible due to the nature as OpenJPA/JPA was thought ?
For me it is not a big deal since data is visible inside the container ( Apache Geronimo ) however for the customer it is important since he wants to connet using 3rd party tool ( eg. SQL Developer to and see if data was inserted or not .)

Categories