You have first a "Caused by: java.lang.ClassNotFoundException: com.sun.gjc.spi.ResourceAdapter " error in your output. This should be the first thing you should fix (something missing). On the other hand i see so many annotations in you Book class, which are in the majority not needed. In particular those for the column names. BTW: Why are you annotating the attributes instead of the getters? You have overwritten equals() but not really implemented it (only for Id field, what about the other fields?). And if you need to overwrite equals you have to overwrite hashCode as well. Let the IDE create them it's much more simpler.
A:
khmarbaise
2010-05-06 05:57:21
Annotating attributes in OK (you can even mix attributes and getter annotations with JPA 2.0). Also, the current `equals` implementation is fine for JPA (if two Books have the same PK, they should be the same from a JPA point of view) and the OP does implement `hashCode`. Using the PK instead of a business key is debatable but that's another story.
Pascal Thivent
2010-05-06 06:42:59
+3
A:
First, you are missing some JARs on your classpath and my suggestion is to add $GF_HOME/modules/gf-client.jar
(don't copy the JAR to your project, point directly on it).
Second, your test won't pass in it current state, it's missing some parts:
persist
needs to be called inside a transaction- you need to
flush
the changes to get an ID assigned.
Here is a improved version that will run each test method with its own EntityManager
and inside a transaction (that is rolled back at the end of the test method):
public class BookTest {
private static EntityManager em;
private static EntityManagerFactory emf;
@BeforeClass
public static void createEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("E01R01PU");
}
@AfterClass
public static void closeEntityManagerFactory() throws Exception {
emf.close();
}
@Before
public void beginTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
@After
public void rollbackTransaction() {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
}
}
@Test
public void createBook() {
Book book = new Book();
book.setId(1);
book.setDescription("Mastering the Behavior Driven Development with " +
"Ruby on Rails");
book.setTitle("Mastering the BDD");
book.setPrice(25.9f);
book.setNumberofpage(1029);
em.persist(book);
em.flush(); // sync the in-memory changes with the db
// now this will pass
assertNotNull("ID should not be null", book.getId());
}
}
Bonus: this test will pass :)
Note that people usually prefer being able to run tests without having to start a container i.e. without relying on JNDI. Here is a persistence.xml
showing how to configure EclipseLink to connect to a Derby database running in server mode that you could use in a testing context:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="TestPu" transaction-type="RESOURCE_LOCAL">
<class>com.acme.Foo</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!--property name="eclipselink.target-database" value="DERBY"/-->
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527//path/to/derbyDBs/TestDB;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.debug" value="ALL"/>
<property name="eclipselink.logging.level.sql" value="FINEST" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.cache" value="FINEST" />
</properties>
</persistence-unit>
</persistence>
Pascal Thivent
2010-05-06 06:38:50
I'm following your instruction and it worked. But when i create REST web service i can't built my project. Here is the message:app_path\build-impl.xml:583: The module has not been deployed.BUILD FAILED. So what shall i do now?
Zeck
2010-05-06 08:06:23
@Zeck you should accept this answer and create another question :) Please, don't mix different topics in the same question.
Pascal Thivent
2010-05-06 08:38:03