本文介绍了映射相同的类关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我试图在hibernate中映射一些类,并且存在一般问题,这种映射可以如何完成。
User类和Facebook用户类具有以下结构
用户类:

  public class用户{
public User(){}
长ID;
字符串名字;
字符串姓氏;
....
FbUser fbuser;
////全部请求
获取者和设置者...
}



Facebook课程FbUser可以有朋友列表,它们是同一个班级FbUser的对象。


  public class FbUser {
public FbUser(){}
Long fbId;
字符串名字;
字符串姓氏;
列表< FbUser>朋友;
////全部请求
获取者和设置者...
}


$ b $到目前为止,我已经在User和FbUser之间建立了多对一的关系。

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

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

name =FirstName
update =true
insert =true
not-null =false
unique =false
type =java.lang.String
>
< column name =FirstName/>
< / property>
name =LastName
update =true
insert =true
not-null =false
独特=false
type =java.lang.String
>
< column name =LastName/>
< / property>
名称=fbUser
class =FbUser
cascade =全部
列=fbId
unique =true
/>

< / class>
< / hibernate-mapping>

现在FbUser映射:

 <休眠映射> 
< class
name =FbUser
table =FbUser
>

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

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

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

我可以在FbUser映射文件中映射FbUser List吗?我迷路了:(


解决方案

您可以创建一个名为MyFriends的附加类

  public class FbUser {

List< MyFriends> friends = new ArrayList< MyFriends>();

}

相关部分

如果您有索引列

 < hibernate-mapping> 
< class name =FbUser>
< list name =myFriends>
< key column =ME_IDinsert =falseupdate =false/>
< ; list-index column =哪个列应该用作索引/>
<一对多class =MyFriends/>
< / list>
< / class>
< / hibernate-mapping>

没有索引列



将您的列表重新写入

  public class FbUser {

收藏< MyFriends> friends = new ArrayList< MyFriends>();


$ / code $ / pre
$ b $


 < hibernate-mapping> 
< class name =FbUser>
< bag name =columns>
< key column =ME_IDinsert =falseupdate =false/>
<一对多课程=MyFriends/>
< / bag>
< / class>
< / hibernate-mapping>

以及您的MyFriends映射。 请注意,您需要一个组合主键(作为静态内部类实现)

 < class name = MyFriends > 
< composite-id name =myFriendsIdclass =MyFriends $ MyFriendsId>
< key-property name =meId/>
< key-property name =myFriendId/>
< / composite-id>
< / class>

您的MyFriends显示如下:

  public class MyFriends {

private MyFriendsId myFrinedId;

私人FbUser me;
私人FbUser myFriend;

public static class MyFriendsId implements Serializable {

private Integer meId;
私人整数myFriendId;

// getter's和setter's
$ b $ public MyFriendsId(){} $ b $ public MyFriendsId(Integer meId,Integer myFriendId){
this.meId = meId ;
this.myFriendId = myFriendId;

$ b $ // getter's和setter's

public boolean equals(Object o){
if(!(o instanceof MyFriendsId))
返回false;

MyFriendsId other =(MyFriendsId)o;
返回新的EqualsBuilder()
.append(getMeId(),other.getMeId())
.append(getMyFriendId(),other.getMyFriendId())
.isEquals );

$ b public int hashcode(){
return new HashCodeBuilder()
.append(getMeId())
.append(getMyFriendId())
.hashCode();
}
}
}


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 structureUser 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 :(

解决方案

You can create an additional class named, for instance, MyFriends

public class FbUser {

    List<MyFriends> friends = new ArrayList<MyFriends>();

}

Just relevant part

If you have a index-column

<hibernate-mapping>
    <class name="FbUser">
        <list name="myFriends">
            <key column="ME_ID" insert="false" update="false"/>
            <list-index column="WHICH COLUMN SHOULD BE USED AS INDEX"/>
            <one-to-many class="MyFriends"/>
        </list>
    </class>
</hibernate-mapping>

If you do not have a index-column

re-write your list as

public class FbUser {

    Collection<MyFriends> friends = new ArrayList<MyFriends>();

}

And

<hibernate-mapping>
    <class name="FbUser">
        <bag name="columns">
            <key column="ME_ID" insert="false" update="false"/>
            <one-to-many class="MyFriends"/>
        </bag>
    </class>
</hibernate-mapping>

And your MyFriends mapping. Notice you need a composite primary key (implemented as a static inner class)

<class name="MyFriends">
    <composite-id name="myFriendsId" class="MyFriends$MyFriendsId">
        <key-property name="meId"/>
        <key-property name="myFriendId"/>
    </composite-id>
    <many-to-one name="me" class="FbUser" insert="false" update="false"/>
    <many-to-one name="myFriend" class="FbUser" insert="false" update="false"/>
</class>

Your MyFriends is shown as follows

public class MyFriends {

    private MyFriendsId myFrinedId;

    private FbUser me;
    private FbUser myFriend;

    public static class MyFriendsId implements Serializable {

        private Integer meId;
        private Integer myFriendId;

        // getter's and setter's

        public MyFriendsId() {}
        public MyFriendsId(Integer meId, Integer myFriendId) {
            this.meId = meId;
            this.myFriendId = myFriendId;
        }

        // getter's and setter's

        public boolean equals(Object o) {
            if(!(o instanceof MyFriendsId))
                return false;

            MyFriendsId other = (MyFriendsId) o;
            return new EqualsBuilder()
                       .append(getMeId(), other.getMeId())
                       .append(getMyFriendId(), other.getMyFriendId())
                       .isEquals();
        }

        public int hashcode() {
            return new HashCodeBuilder()
                       .append(getMeId())
                       .append(getMyFriendId())
                       .hashCode();
        }
    }
}

这篇关于映射相同的类关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 16:42