Hello,
I am new to hibernate and JPA. I am trying to get a basic prototype working. There are two classes - Bid.java and Item.java (code below)
package com.Test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Bid {
private Long id;
private String name;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
@Override
public String toString() {
return "Item [id=" + id + ", name=" + name + "]";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
====
Item.java
Package com.Test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Item {
private Long id;
private String name;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
@Override
public String toString() {
return "Item [id=" + id + ", name=" + name + "]";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
=========================
This is my persistence.xml file
<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/ ... ce_2_0.xsd"
version="2.0">
<persistence-unit name="manager1">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- not necessary to specify each class
hibernate searches even in non EE environments.
<class>com.test.Bid</class>
-->
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://3.10.10.99:1433;databaseName=Test;"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value="sa">
<property name="hibernate.connection.pool_size" value="0"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!--
* validate: validate the schema, makes no changes to the database.
* update: update the schema.
* create: creates the schema, destroying previous data.
* create-drop: drop the schema at the end of the session.
-->
</properties>
</persistence-unit>
</persistence>
===========================
I am trying to run this Junit test:
ublic class AppTest extends TestCase {
public AppTest(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(AppTest.class);
}
public void testJPA() throws Exception {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("manager1");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Item item = new Item();
item.setName("this is a test name for the item using JPA");
Bid bid = new Bid();
bid.setName("somename");
em.persist(item);
em.persist(bid);
tx.commit();
List<Bid> bids = em.createQuery("from Bid").getResultList();
for(Bid currentBid : bids) {
System.out.println(currentBid);
}
em.close();
emf.close();
}
}
=========
I am getting this error: javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at AppTest.testJPA(AppTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Can you please tell me if I am missing something here? Does the persistence.xml have to be in a specific folder for this to work? Any pointers/guidance will be helpful.
Thanks.