3. Testing the JPA project
4. Update database when entity changed.
5. Setting up load balance database connection.
6. Setting Cache Coordination in Eclipselink
5. Setting up load balance database connection.
6. Setting Cache Coordination in Eclipselink
The purpose of this post is to show how to test a JPA project whether it is properly setup.
Besides, this solution can be used to test a newly created/generated entity as well.
The concept is quite simple, simply create a java class with main(String[] args), in the main method, create database connection and then execute a select statement.
If successfully select records from database, that's mean the JPA project is properly setup.
Steps:
1. Create a new package called test in the project.
2. Create a new class called JpaTest in the test package.
3. Write the following codes in the main(String[] args) to test.
factory = Persistence.createEntityManagerFactory("HelloJPA");
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select a from Ofuser a");
List<Ofuser> users = q.getResultList();
System.out.println("Number of users: " + users.size());
if(users.size() > 0) {
for (Ofuser user : users) {
System.out.println(user);
}
}
4. Change the entity (Ofuser) in the codes above to any entity in the JPA project.
5. R-click > Run As... > Java application.
6. If any number of records return, that's meaning the JPA project is properly setup.
otherwise, there could be showing exceptions in the console.
Done!!
Thanks for the great set of JPA tutorials. Found it very useful.
ReplyDeleteBut I ran into a small problem when attempting to test the newly setup JPA project. I was wondering if you will be able to help. Searched on Stack Overflow and similar forums but could overcome the problem though by following the steps suggested for similar problems.
I keep on getting this error....
javax.persistence.PersistenceException: No Persistence provider for EntityManager named HelloJPA
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at test.JpaTest.main(JpaTest.java:21)
Gehan
//Persistence.xml
org.eclipse.persistence.jpa.PersistenceProvider
model.User
//JpaTest main method
import javax.persistence.Persistence;
import javax.persistence.Query;
import model.User;
public class JpaTest
{
public JpaTest()
{
}
public static void main(String[] args)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloJPA");
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("SELECT u FROM User u");
List users = q.getResultList();
System.out.println("Number of users: " + users.size());
if(users.size() > 0)
{
for (User user : users)
{
System.out.println(user);
}
}
em.close();
emf.close();
}
}
Sorry the Persistence.xml and user Entity Class related to the previous question are as follows.(I have published them as images as blogger blocks some syntax)
ReplyDeleteUser.java
http://goo.gl/PpyS0Z
any leads on how to slove this error ?
Persistence.xml
http://goo.gl/Hd8yDE
Files Included
http://goo.gl/thIaSl
Hi,
DeleteI couldn't find the root cause as well. as it is almost identical with my files.
here is my project, you can download and try in your workspace.
https://drive.google.com/file/d/0B-Sk6VWnJnd5b3RnU3NjQlNOSlU/edit?usp=sharing
instead, you can upload your project so that I can debug in my machine.
Thank you. I tried the same code for Hibernate and it worked fine.
ReplyDeleteActually I tried both, and both are worked.
Deleteanyway, it's great that your test is working for you.