users@glassfish.java.net

JPA foreign composite key problems

From: <glassfish_at_javadesktop.org>
Date: Wed, 28 Nov 2007 16:40:48 PST

Hi,

I've been attempting to write some POJOs to work with an existing schema that our company uses. Basically it's perfectly fine for the creation and persistence of simple database tables, but when it comes to having a table which has a foreign key referring to the key of one of the basic tables, and no normal id key, I'm having trouble working out which method to use to work properly with our DB.

The entity details are similar in the finer detail to these below:

-------------------
BASE
-------------------
@Entity
@Table(name="BASE")
public class Base implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name="ID")
    private Long id;
    
    @Column(name="BASETEXT")
    private String baseText;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }
    
    public String getBaseText() {
        return baseText;
    }

    public void setBaseText(String baseText) {
        this.baseText = baseText;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Base)) {
            return false;
        }
        Base other = (Base) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "inheritanceDB.Base[id=" + id + "]";
    }
}

-------------------
DEP
-------------------
@Entity
@Table(name="DEP")
public class Dep implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @JoinColumn(name="BASEID")
    private Base baseId;
    
    @Column(name="DEPTEXT")
    private String depText;

    public Base getBaseId() {
        return baseId;
    }

    public void setBaseId(Base baseId) {
        this.baseId = baseId;
    }

    public String getDepText() {
        return depText;
    }

    public void setDepText(String inheritedText) {
        this.depText = inheritedText;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Dep)) {
            return false;
        }
        Dep other = (Dep) object;
        if ((this.baseId == null && other.baseId != null) || (this.baseId != null && !this.baseId.equals(other.baseId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "inheritanceDB.Inherited[id=" + baseId.toString() + "]";
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (baseId != null ? baseId.hashCode() : 0);
        return hash;
    }
}

The schema for this is:

BASE:
PK NUMERIC ID
              VARCHAR[255] BASETEXT

DEP:
FK PK NUMERIC BASEID
              VARCHAR[255] DEPTEXT

The leading method that I have been looking at is making Dep extend Base, but using the Inheritance strategy that seems to fit this schema best (InheritanceType.JOINED), attempts to insert a DiscrminatorColumn, DTYPE, into the BASE table, which is not possible in this situation.

I'm open to any method which would allow this to work properly.

Thanks in advance,
Tom
[Message sent by forum member 'tomsullivan' (tomsullivan)]

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