0

Assume we have a Parent class in some library:

public abstract class Parent {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Also assume that we have a Child class in some other library:

public class Child extends Parent {

}

I want to use Child class as embedded in one of my entities, like

@Entity
public class SomeEntity {
    // id and other fields
    @Embedded
    private Child child;

    // getters, setters etc.
}

The problem is, the fields defined in Parent are not embedded, i.e. there are no columns for them in the corresponding table of SomeEntity.

Just to make sure that it is because of inheritance, I first added some fields to Child and observed that they are embedded as expected. I also tried embedding Parent directly, and the columns for them are generated this time too.

The problem is, how can I embed the fields defined in a super class?

I tried to mark child field of SomeEntity with @Access(AccessType.PROPERTY), but it did not work. I also tried to change those fields in Parent class (i.e. name in our example) to public but this didn't work either.

In case it is related, my project is a Spring Boot project, with Hibernate as the JPA provider. Any help is appreciated.

EDIT: The attempted orm.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings 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_2_0.xsd"
                 version="2.0">

    <embeddable class="...Child" access="PROPERTY">
        <attributes>
            <basic name="name">
                <column nullable="false"/>
            </basic>
        </attributes>
    </embeddable>
</entity-mappings>

I tried with access="FIELD" and also without any access parameter, but no luck.

To make sure that orm.xml is loaded, I gave an invalid class to embeddable and observed ClassNotFoundException, hence it is loaded.

ram
  • 1,099
  • 1
  • 7
  • 22
  • 1
    Is your `Child` class annotated with `@Embeddable` ? – Luay Abdulraheem Jun 06 '17 at 00:34
  • or do you have an `orm.xml` for the third party class defining it as `` ? (and that way you don't have to edit the actual class to add annotations). – Neil Stockton Jun 06 '17 at 06:26
  • @LuayAbdulraheem no it's not, because it's in some other library. – ram Jun 06 '17 at 14:32
  • @NeilStockton If I try to embed `Parent` without `orm.xml`, it works. So I thought it is not mandatory. However, I tried with `orm.xml` too but it didn't work again. – ram Jun 06 '17 at 14:34
  • You use `orm.xml` when you don't want/have annotations, or when you want to override annotations. Since you don't share with us whether you have annotations (on the third party class) then people have to guess. Any JPA provider that is compliant supports `orm.xml` – Neil Stockton Jun 06 '17 at 14:53
  • @NeilStockton I have already shared my third party classes: `Parent` and `Child`. They do not have annotations, so I have to go with `orm.xml`. – ram Jun 06 '17 at 14:55
  • so go with it ... placed under `META-INF`. As said, all compliant JPA providers support it. http://www.datanucleus.org/products/accessplatform_5_1/jpa/metadata_xml.html – Neil Stockton Jun 06 '17 at 14:56

1 Answers1

0

Quoting from here,

The JPA spec does not support embeddable objects having inheritance, although some JPA providers may allow this.

I guess this as the cause of my problem. Shame on JPA.

In an answer to a similar question, it is stated that:

Hibernate doesn't support inheritance of @Embeddable component.

ram
  • 1,099
  • 1
  • 7
  • 22