PGK
PGK

Reputation: 141

eclipselink inheritance implementation

I am trying to implement ElcipseLink JPA2.0 for inheritance in my project. Can't use annotation. only xml mappings.

Here is my code. public class DefaultEntity {

}

public class SpecialEntity extends DefaultEntity {
public String name; 
public int age; 
}

public class AnotherSplEntity extends DefaultEntity {
long ts;
String pkey; 

}

public class MyPersistableEntity {

public DefaultEntity de; 

public void setMyPersistableEntity(DefaultEntity de) {
  // any subclass can be assigned here. 
  this.de = de
}

here is my ORM.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entity-mappings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"      version="2.3">

<persistence-unit-metadata>
    <exclude-default-mappings />
</persistence-unit-metadata>
<entity  class="MyPersistableEntity">
<attributes>
<one-to-one name="de">
<cascade>
<cascade-all />
</cascade>
</one-to-one>
</attributes>
</entity>
<mapped-superclass class="DefaultEntity">
<attributes>
<id name="id" attribute-type="long">
<generated-value strategy="SEQUENCE" />
</id>

</attributes>
</mapped-superclass>
<entity class="SpecialEntity" >
    <attributes>
        <id name="id" attribute-type="long">
            <generated-value strategy="SEQUENCE" />
        </id>
        <basic name="name" attribute-type="String" />
        <basic name="age" attribute-type="int" />
    </attributes>
</entity>
</entity-mappings>

I keep getting " uses a non-entity [class DefaultEntity] as target entity in the relationship attribute [field de]"

how to make EclipseLink recognize the actual class assigned and use that mapping?

any ideas? foremost, can it be done using EcliseLink?

thanks Gopi

Upvotes: 0

Views: 673

Answers (2)

PGK
PGK

Reputation: 141

finally found a way to do it. So I replaced with and created one for Abstract class too. Not sure if it is a bug with EclipseLink. And another issue is using generated-value strategy "SEQUENCE" (may be others) is not generating sequences properly.

My orm looks like below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entity-mappings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm" version="2.3">

<persistence-unit-metadata>
    <exclude-default-mappings />
</persistence-unit-metadata>

<entity  class="ABC">
    <table name="" />
    <attributes>
        <id .......>
        </id>
        <basic name="ts" attribute-type="long" />
        <one-to-one name="field-referring-to-abstract-class" >
            <join-column name="ABSTRACT_ID"/>
            <cascade>
                <cascade-all />
            </cascade>
        </one-to-one>
    </attributes>
</entity>


<entity class="ABSTRACT-CLASS" >
    <table name="ABSTRACT-TABLE"/>  
    <inheritance strategy="TABLE_PER_CLASS" />
    <attributes>
        <id name="ABSTRACT_ID" attribute-type="String" >
            <column name="ABSTRACT_ID" />
        </id>
    </attributes>
</entity>


<entity class="SUB-CLASS1-TO-ABSTRACT" access="FIELD">
     <table name="SUBCLASS1"/>
    <attributes>
        <basic name="name" attribute-type="String" />
        <basic name="age" attribute-type="int" />
    </attributes>
</entity>

<entity class="SUB-CLASS2-TO-ABSTRACT" access="FIELD">
     <table name="SUBCLASS2"/>
    <attributes>
        <basic name="city" attribute-type="String" />
        <basic name="zipcode" attribute-type="int" />
    </attributes>
</entity>
</entity-mappings>

so, this is storing to appropriate tables as we as reading provided PK in subclasses I provide holds uniqueness.

hope this helps thanks Gopi

Upvotes: 0

James
James

Reputation: 18379

If you want the reference to be SpecialEntity you need to set the target-entity,

See, http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Target_Entity

Or better, just change the type of your field to SpecialEntity.

If it can be either, then you cannot use a MappedSuperclass, you need to make the DefaultEntity and Entity and map the inheritance.

Upvotes: 1

Related Questions