简体   繁体   中英

How to persist a recursive property in Hibernate?

I am struggling with a Hibernate mapping involving a class with a property of the same class type. Ie (simplified):

public class A {
    protected A next = null;
}

My mapping currently looks like:

<hibernate-mapping package="mypackage">
    <class name="A" table="tblA" lazy="false">

        <id name="id">
            <generator class="native"/>
        </id>

        <property name="next" type="A" />

    </class>
</hibernate-mapping>

However during Hibernate initialization I get a Exception in thread "main" org.hibernate.MappingException: Could not determine type for: A, at table: tblA, for columns: [org.hibernate.mapping.Column(next)] .

My understanding is that I am trying to use a mapping which hasn't been declared yet (because I am doing it right now) and I would probably need something like a forward declaration, or so. Do you have any idea?

Thank you
Tunnuz

It is a reference:

<many-to-one name="next"/>

<Property> is used for primitive types. There is usually no need to declare the type, since it is determined by Hibernate using reflection.

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