Poni
Poni

Reputation: 11317

hibernate map member's field directly without setter/getter

I have these simplified types:

public class DataBean extends ZZZZZ {
    public String   name;
}

public class Member extends CCCCC {
    public DataBean m_data;
}

I'd like to map m_data.name directly within Member. I cannot use any sort of inheritance between the two (occupied already!).

This would be ideal:

<property name="m_data.name" column="name" type="string" access="field" />

Any idea? Is it possible with Hibernate?

Currently I'm getting this error:

org.hibernate.PropertyNotFoundException: field [m_data.name] not found on com.example.Member

The main reason is that I'm trying to (re)use a data bean taken from an http service, and I really don't feel like rewriting all the fields, nor writing a setter/getter.

Upvotes: 1

Views: 1369

Answers (1)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

The name property is actually in the DataBean class. So, the exception you are getting is obvious. Because the mapping is for Member.

The DataBean is embedded in Member and to map the field in the embedded class you have to use the <component> element.

Please refer to this answer to this question here in SO: Hibernate @embeddable annotation equivalent for XML mapping file?

The answer also has a link to the page that explains what embedded objects are how to map them.

Update:

<component
        name="m_data"
        class="com.example.DemoBean"
        access="field">
    <property name="name" column="NAME" type="string" access="field" />
</component>

Upvotes: 6

Related Questions