I would like to implement a program in Java able to interact with a database. I have already done something like this using EJB, but this time I need it to be able to work without an application server. What I have done so far is (with Eclipse):
persistence.xml
(it is in META-INF
folder)MySQL JDBC Driver
library and persistence-api-1.0.2.jar
Here is my persistence.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<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="top" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>database.Match</class>
<class>database.Player</class>
<class>database.Tournament</class>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/top"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Here is how I try to use an EntityManager
:
private static EntityManagerFactory factory;
private EntityManager entityManager;
public DBManager() {
factory = Persistence.createEntityManagerFactory("top");
entityManager = factory.createEntityManager();
}
public Player createPlayer(String name, int age, Court favouriteCourt, int currentRanking) {
Player player = new Player();
player.setAge(age);
player.setCurrentRanking(currentRanking);
player.setFavouriteCourt(favouriteCourt);
player.setName(name);
entityManager.persist(player);
return player;
}
My Main
class just simply creates a new DBManager
and tries to use createPlayer
.
I get the following exception:
Exception in thread "main" javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpath.
at javax.persistence.Persistence.findAllProviders(Persistence.java:167)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)
Adding hibernate-entitymanager-3.3.2.GA.jar
I get:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named top: Provider named org.hibernate.ejb.HibernatePersistence threw unexpected exception at create EntityManagerFactory:
java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: org/hibernate/MappingException
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:124)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: org.hibernate.MappingException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at database.DBManager.<init>(DBManager.java:17)
at database.Main.main(Main.java:8)
What am I missing? Do I need to import other jars? Or to set something else in the descriptor?
What you're missing is that hibernate-entitymanager just contains the wrapper that wraps hibernate core to expose it as a JPA entity manager. So if you've only added that you won't get the actual hibernate classes, hence your class not found.
If you look at the picture of how the components tie together on the hibernate home page you'll see that entity manager builds on hibernate core and hibernate annotations. You'll need to add the required jars for both of these as well as all their dependencies.
The docs describe the various folders of jars in the hibernate distro and exactly which sets of jars you need for what kind of usage.