Posting Sample Code
To demonstrate a technical question or an issue - you may have to provide a complete runnable sample program.
If an unexpected exception (e.g. NullPointerException
or ClassCastException
) is thrown by ObjectDB - posting the full stack trace with no sample program (using the Issue Tracking system) may be sufficient to locate the problem. In most other cases - a sample program may be required.
Whenever possible:
- Use a single Java file with one main class + static inner classes for entity / embeddable classes.
- Avoid dependency on external libraries.
- Use a console application with a main method (preferred over JUnit).
- Use embedded mode to connect to the ObjectDB database directly (no persistence unit).
- Keep the test as simple as possible - remove unnecessary code (but keep it complete and runnable).
You may use the following example as an initial template for your test case:
package test; import java.util.*; import javax.persistence.*; public final class MyTestCase { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory( "objectdb:$objectdb/db/test.tmp;drop"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); MyEntity e = new MyEntity("test"); em.persist(e); em.getTransaction().commit(); Query query = em.createQuery("SELECT e FROM MyEntity e"); List resultList = query.getResultList(); System.out.println(resultList); em.close(); emf.close(); } @Entity public static class MyEntity { private String name; MyEntity(String name) { this.name = name; } @Override public String toString() { return name; } } }
Note: If you are using NetBeans please ignore its strict JPA warnings (There is no ID defined for this entity hierarchy, The class should have a no-arg public or protected constructor, An entity class must be a top level class, The project does not contain a persistence unit, An entity or IdClass class should implement the java.io.Serializable interface).