users@glassfish.java.net

How to use java interfaces as targetEntities in associations?

From: <glassfish_at_javadesktop.org>
Date: Tue, 05 Jun 2007 09:04:34 PDT

With JPA it is quite easy to persist java classes (POJOs) directly in a database.

But what if those java classes uses java interfaces (maybe they are called POJI -plain old java interfaces-?) to access their attributes and associations. I know that a targetEntity could be specified at the relationship annotation, but this only works if there is a one-to-one correlation between the interface and the implementing class.

Can JPA deal with situations where there are two classes implementing the same interface without having a common superclass?

[i]Example code for Container.class:[/i]
package com.innoq.cn.persistence;
import javax.persistence.*;
@Entity
public class ContainerClass {
        @Id
        @GeneratedValue
        private int id;
        @OneToOne(cascade=CascadeType.PERSIST, [b]targetEntity=ClassOne.class[/b])
        private Interface interfaceBase;
        public Interface getInterface() { return interfaceBase; }
        public void setInterface(Interface interfaceBase) { this.interfaceBase = interfaceBase; }
}

[i]Example code for Interface.java:[/i]
package com.innoq.cn.persistence;
public interface Interface {
        public String getName();
        public void setName(String name);
}
[Message sent by forum member 'neumann5' (neumann5)]

http://forums.java.net/jive/thread.jspa?messageID=220592