Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

java - Mapping same class relation

Hi I’m trying to map some classes in hibernate there and have general problem how such mapping can be done. There is User class and Facebook user class which has the following structure User Class :

public class User{
 public User(){}
 Long Id;
 String FirstName;
 String LastName;
 ....
 FbUser fbuser;
 //// all requred 
 getters and setters...
}

Facebook class FbUser can have list of Friends which are objects of the same class FbUser.

public class FbUser{
 public FbUser(){}
 Long fbId;
 String FirstName;
 String LastName;
 List<FbUser> friends;
 //// all requred 
 getters and setters...
}

Till now I made many to one relation between User And FbUser.

<hibernate-mapping>
    <class
        name="User"
        table="User"
    >

        <id
            name="Id"
            column="ID"
            type="java.lang.Long"
            unsaved-value="null"
        >
         <generator class="increment"/>
        </id>

        <property
            name="FirstName"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="FirstName" />
        </property>
 <property
            name="LastName"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="LastName" />
        </property>
        <many-to-one
            name="fbUser"
            class="FbUser"
            cascade="all"
            column="fbId"
            unique="true" 
        />

    </class>
</hibernate-mapping>

And now the FbUser Mapping:

<hibernate-mapping>
    <class
        name="FbUser"
        table="FbUser"
    >

        <id
            name="fbId"
            column="fbId"
            type="java.lang.Long"
            unsaved-value="null"
        >
          <generator class="increment"/>
        </id>

        <property
            name="FirstName"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="FirstName" />
        </property>

        <property
            name="LastName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="LastName"
            not-null="true"
            unique="false"
        />
    </class>
</hibernate-mapping>

Chow can I map FbUser List inside the FbUser Map file? I got lost :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, first: User has a one-to-one relation with FbUser, correct? second: Map FbUser to FbUser as a many to many relation using a list or a set. I have an Set example here:

    <set 
        name="friends"
        table="FbUser" <!-You may use other table here if you want->
        access="field">
        <key 
            column="fbId"/>
        <many-to-many 
            class="bla.bla.bla.FbUser"
            column="friend_id" />
    </set>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...