How Persistence Unit works in JPA - java

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.

Related

How to solve "Using Hibernate built-in connection pool (not for production use!)" using JPA i.e. Hibernate EntityManager

I'm new to Hibernate and JPA in general.
I read a lot about this warning, but I still can't solve it.
The answers I read so far, said that it is necessary to have hibernate.cfg.xml in the project.
But I also read that:
If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml. So you generally don't need both as you use either Hibernate proprietary API or JPA.
(what is the purpose of two config files for Hibernate?)
Using persistence.xml I have this warning every time I use Hibernate.
This is my persistence.xml:
<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="integration"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db-name?autoReconnect=true"/>
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.id.new_generator_mappings"
value="true" />
</properties>
</persistence-unit>
</persistence>
I can't figure out what I'm doing wrong.
Thanks in advances
It is just a warning stating that you are using a built_in connection pool which is not a suitable solution in the production environment, you should use the application server connection pool in the production environment. depending on your application server you can setup database connection inside your application server then configure hibernate to use that connection.
But if you want to solve this problem without configuring the application server you can see this.

javax.persistence.PersistenceException: No Persistence provider for EntityManager named em1

I am creating a server in Glassfish and I want to have a JPA persistence layer to a remote MySQL database.
When I attempt to use the persistence layer, I get this exception: javax.persistence.PersistenceException: No Persistence provider for EntityManager named em1
Based on other StackOverflow posts, it seems this is an indication the persistence.xml is invalid. My persistence.xml is shown. Can someone point me in the right direction to figuring out how to get this to work?
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name ="em1">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>data.entry.Movie</class>
<properties>
<property name="eclipselink.target-database"
value="MySQL4"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="mysql3.cs.myschool.edu/mydb"/>
<property name="javax.persistence.jdbc.user" value="mydb" />
<property name="javax.persistence.jdbc.password" value="mypass" />
</properties>
</persistence-unit>
</persistence>

EclipseLink SessionCustomizer not getting called

I am using JPA with EclipseLink for PostGreSQL. Due to some product requirement I want to switch the schema runtime. I am planning to do it in "customize" method of SessionCustomizer as shown below
#Override
public void customize(Session session) throws Exception {
System.out.println("calling customize ###################");
if (this.schemaName != null || !this.schemaName.isEmpty()) {
session.getLogin().setTableQualifier(schemaName);
}
I set MySessionCustomizer in peristence.xml.
But don't know when and who will call MySessionCustomizer's customize method.
Please help me to understand this.
EDIT:
Here is my persistence.xml
<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" >
<class>com.demo.Student</class>
<properties>
<property name="eclipselink.session.customizer" value="com.demo.MySessionCustomizer"/>
<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="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
I am able to connect to the database using this persistence.xml and read/write from public schema of the database. However, I want to read and write from different schema and I also want to be able to change the schema at runtime.
I tried using "?currentSchema=MYSCHEMA" in DB URL, but it didn't work.

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 :)

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