I am following a tutorial on setting up Hibernate JPA at the following site:
http://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Startedeverything works and compiles. however, when I run the JUnit test CompanyTest,
I lose all of my information in the Person table. If I then run the JUnit test on
PersonTest I loose all of my information in the Person table. Any idea how this is happening?
Here are my classes:
PersonTest.javaCode:
package entity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class PersonTest extends TestBase {
@SuppressWarnings("unchecked")
@Test
public void insertAndRetrieve() {
final List<Person> people = generatePersonObjects();
em.getTransaction().begin();
for (Person p : people) {
em.persist(p);
}
em.getTransaction().commit();
final List<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());
for (Person current : list) {
final String firstName = current.getFirstName();
final String streetAddress1 = current.getAddress()
.getStreetAddress1();
assertTrue(firstName.equals("Brett")
|| firstName.equals("FirstName"));
assertTrue(streetAddress1.equals("A Rd.")
|| streetAddress1.equals("B Rd."));
}
}
public static List<Person> generatePersonObjects() {
final List<Person> people = new ArrayList<Person>();
final Address a1 = new Address("A Rd.", "", "Dallas", "TX", "75001");
final Person p1 = new Person("Brett", 'L', "Schuchert", a1);
final Address a2 = new Address("B Rd.", "S2", "OkC", "OK", "73116");
final Person p2 = new Person("FirstName", 'K', "LastName", a2);
people.add(p1);
people.add(p2);
return people;
}
}
TestBase.javaCode:
package entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
public class TestBase {
protected EntityManagerFactory emf;
protected EntityManager em;
public TestBase() {
super();
}
@Before
public void initEmfAndEm() {
BasicConfigurator.configure();
Logger.getLogger("org").setLevel(Level.ERROR);
emf = Persistence.createEntityManagerFactory("examplePersistenceUnit");
em = emf.createEntityManager();
}
@After
public void cleanup() {
em.close();
}
}
Person.javaCode:
package entity;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
int id;
private String firstName;
private char middleInitial;
private String lastName;
@Embedded
private Address address;
public Person() {
}
public Person(final String fn, final char mi, final String ln,
final Address address) {
setFirstName(fn);
setMiddleInitial(mi);
setLastName(ln);
setAddress(address);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
public char getMiddleInitial() {
return middleInitial;
}
public void setMiddleInitial(final char middleInitial) {
this.middleInitial = middleInitial;
}
public final Address getAddress() {
return address;
}
public final void setAddress(final Address address) {
this.address = address;
}
}
And finally the CompanyTest & Company:
CompanyTest.javaCode:
package entity;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CompanyTest extends TestBase {
@Test
public void createCompany() {
final Company c1 = new Company();
c1.setName("The Company");
c1.setAddress(new Address("D Rd.", "", "Paris", "TX", "77382"));
em.getTransaction().begin();
em.persist(c1);
em.getTransaction().commit();
final Company foundCompany = (Company) em.createQuery(
"select c from Company c where c.name=?1").setParameter(1,
"The Company").getSingleResult();
assertEquals("D Rd.", foundCompany.getAddress().getStreetAddress1());
}
}
Company.javaCode:
package entity;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Company {
@Id
@GeneratedValue
int id;
private String name;
@Embedded
private Address address;
@OneToMany
private Collection<Person> employees;
public Company() {
}
public Company(final String name, final Address address,
final Collection<Person> employees) {
setName(name);
setAddress(address);
setEmployees(employees);
}
public Address getAddress() {
return address;
}
public void setAddress(final Address address) {
this.address = address;
}
public Collection<Person> getEmployees() {
if (employees == null) {
employees = new ArrayList<Person>();
}
return employees;
}
public void setEmployees(final Collection<Person> employees) {
this.employees = employees;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public void hire(final Person p) {
getEmployees().add(p);
}
public void fire(final Person p) {
getEmployees().remove(p);
}
}
persistence.xmlCode:
<persistence>
<persistence-unit name="examplePersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/database" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value ="password" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
Any ideas? Thanks.