Hi,
I am new to JPA and OpenJPA. I am trying to work with interfaces in JPA and
I am getting some exceptions. Can someone please help me out.
Summary of the problem
---------------------------
I have an interface called Endpoint and an implementation of that called
EndpointObject. When I commit the EndpointObject to a database, I get the
exception that the 'relation endpoint does not exist'. I see a table called
'endpointobject' that is created in the database but not 'endpoint'. I have
provided all my code and all other details below.
1. Endpoint.java
import javax.persistence.Basic;
import javax.persistence.Id;
import org.apache.openjpa.persistence.ManagedInterface;
@ManagedInterface
public interface Endpoint {
@Id
public long getId();
@Basic
public String getSite_name();
public void setSite_name(String siteName);
}
2. EndpointObject.java
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class EndpointObject implements Endpoint {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Basic
private String site_name;
public long getId() {
return id;
}
public String getSite_name() {
return site_name;
}
public void setSite_name(String siteName) {
site_name = siteName;
}
public EndpointObject() {
}
}
3. Main.java.
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.openjpa.jdbc.kernel.TableJDBCSeq;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAPersistence;
public class Main {
public static void main(String[] args) {
EntityManagerFactory factory =
Persistence.createEntityManagerFactory("TestJPA1");
EntityManager em = factory.createEntityManager();
OpenJPAEntityManager em1 = OpenJPAPersistence.cast(em);
try {
Class c = Class.forName("Endpoint");
OpenJPAEntityManagerFactory factory1 =
em1.getEntityManagerFactory();
OpenJPAEntityManagerFactorySPI spi =
(OpenJPAEntityManagerFactorySPI)
factory1;
spi.getConfiguration().getMetaDataRepositoryInstance().register(c);
ClassMetaData cmd =
spi.getConfiguration().getMetaDataRepositoryInstance().addMetaData(c,
ClassMetaData.ACCESS_PROPERTY);
cmd.setManagedInterface(true);
String[] str = new String[2];
str[0] = "-action";
str[1] = "add";
TableJDBCSeq.main(str);
Endpoint ep = (Endpoint) em1.createInstance(c);
ep.setSite_name("site_name1");
em1.getTransaction().begin();
em1.persist(ep);
em1.getTransaction().commit();
Query q = em1.createQuery("SELECT a FROM Endpoint a");
for (int i = 0; i < q.getResultList().size(); ++i) {
Endpoint ep1 = (Endpoint)
q.getResultList().get(i);
System.out.println(ep1.getId() + " " +
ep.getSite_name());
}
em1.close();
factory1.close();
em.close();
factory.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
</entity-mappings>
5. persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="TestJPA1">
<provider> org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>EndpointObject</class>
<properties>
<property name="openjpa.ConnectionURL"
value="jdbc:postgresql://localhost:5432/datakoa"/>
<property name="openjpa.ConnectionDriverName"
value="org.postgresql.Driver"/>
<property name="openjpa.ConnectionUserName" value="gopi"/>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema"/>
<property name="openjpa.ConnectionPassword" value="gopi"/>
</properties>
</persistence-unit>
</persistence>
6. I am running this on a Mac OSX 10.5.8
7. JVM is
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-226)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-92, mixed mode)
8. Postgres jdbc driver version is postgresql-8.4-701.jdbc3.jar
--
View this message in context:
http://n2.nabble.com/Problems-with-Interfaces-tp3894934p3894934.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.