简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM