Mapping same class relation - continuation

前端 未结 1 874
暗喜
暗喜 2020-12-07 05:25

This post is an continuation of this post

I have DlUser Class each object of this class may have DLFaceBook class and each object of DlFaceBook can have Friends whic

相关标签:
1条回答
  • 2020-12-07 06:20

    Just Facebook and MyFriends

    Facebook Notice add convenience method and MutableLong (later, i tell you why to use MutableLong)

    public class Facebook {
    
        private MutableLong id = new MutableLong();
        public Long getId() { return id.longValue(); }
        public void setId(Long id) { this.id.setValue(id); }
    
        public MutableLong getIdAsMutableLong() {
            return id;
        }
    
        private Collection<MyFriends> myFriends = new ArrayList<MyFriends>();
        public Collection<MyFriends> getMyFriends() { return myFriends; }
        public void setMyFriends(Collection<MyFriends> myFriends) { this.myFriends = myFriends; }
    
        /**
         * add convenience method
         */
        public void addFriend(Facebook myFriendFacebook) {
            myFriends.add(new MyFriends(this, myFriendFacebook));
        }
    
    }
    

    MyFriends

    public class MyFriends {
    
        private MyFriendsId myFriendId;
        public MyFriendsId getmyFriendId(){ return this.myFriendId; }
        public void setmyFriendId(MyFriendsId myFriendId){ this.myFriendId = myFriendId; }
    
        private Facebook me;
        public Facebook getme() { return this.me; }
        public void setme(Facebook me){ this.me = me; }
    
        private Facebook myFriend;
        public Facebook getmyFriend() { return this.myFriend; }
        public void setmyFriend(Facebook friend) { this.myFriend = friend; }
    
        public MyFriends() {}
        public MyFriends(Facebook meFacebook, Facebook myFriendFacebook){
            this.me = meFacebook ;
            this.myFriend = myFriendFacebook;
    
            this.myFriendId = new MyFriendsId(meFacebook.getIdAsMutableLong(), myFriendFacebook.getIdAsMutableLong());
        }
    
        public static class MyFriendsId implements Serializable {
    
            private MutableLong meId = new MutableLong();
            public Long getMeId() { return this.meId.longValue(); }
            public void setMeId(Long id) { this.meId.setValue(id); }
    
            private MutableLong myFriendId = new MutableLong();
            public Long getMyFriendId(){ return this.myFriendId.longValue(); }
            public void setMyFriendId(Long id) { this.myFriendId.setValue(id); }
    
            public MyFriendsId() {}
            public MyFriendsId(MutableLong meId, MutableLong myFriendId) {
                this.meId = meId;
                this.myFriendId = myFriendId;
            }
    
            @Override
            public boolean equals(Object o) {
                if (!(o instanceof MyFriendsId))
                    return false;
    
                MyFriendsId other = (MyFriendsId) o;
                return new EqualsBuilder()
                           .append(getMeId(), other.getMeId())
                           .append(getMyFriendId(), getMyFriendId())
                           .isEquals();
            }
    
            @Override
            public int hashCode() {
                return new HashCodeBuilder()
                           .append(getMeId())
                           .append(getMyFriendId())
                           .hashCode();
            }
    
        }
    }
    

    Mapping

    <hibernate-mapping package="br.com._3845772.model.domain">
        <class name="User">
            <id name="id">
                <generator class="native"/>
            </id>
            <many-to-one cascade="all" class="Facebook" name="facebook"/>
        </class>
        <class name="Facebook">
            <id name="id">
                <generator class="native"/>
            </id>
            <bag cascade="all" name="myFriends">
                <key column="ME_FACEBOOK_ID" update="false"/>
                <one-to-many class="MyFriends"/>
            </bag>
        </class>
        <class name="MyFriends">
            <composite-id class="MyFriends$MyFriendsId" name="myFriendId">
                <key-property column="ME_FACEBOOK_ID" name="meId"/>
                <key-property column="MY_FRIEND_FACEBOOK_ID" name="myFriendId"/>
            </composite-id>
            <many-to-one class="Facebook" column="ME_FACEBOOK_ID" insert="false" name="me" update="false"/>
            <many-to-one class="Facebook" column="MY_FRIEND_FACEBOOK_ID" insert="false" name="myFriend" update="false"/>
        </class>
    </hibernate-mapping>
    

    And this sample

    Facebook meFacebook = new Facebook();
    Facebook myFriendFacebook = new Facebook();
    
    meFacebook.addFriend(myFriendFacebook);
    
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    
    session.save(myFriendFacebook);
    session.save(meFacebook);
    
    session.getTransaction().commit();
    session.close();
    

    Which gives me

    Hibernate: insert into Facebook values ( )
    Hibernate: insert into Facebook values ( )
    Hibernate: select myfriends_.ME_FACEBOOK_ID, myfriends_.MY_FRIEND_FACEBOOK_ID from MyFriends myfriends_ where myfriends_.ME_FACEBOOK_ID=? and myfriends_.MY_FRIEND_FACEBOOK_ID=?
    Hibernate: insert into MyFriends (ME_FACEBOOK_ID, MY_FRIEND_FACEBOOK_ID) values (?, ?)
    

    A couple of notes

    • Hibernate does not support automatic generation of composite primary key. You must set up its value before saving
    • Your database must support the target generator strategy (If you does not know which generator strategy your database support, prefer to use a native strategy)
    • Each entity must supply a no-arg constructor

    Now why MutableLong (encapsulated by a Long property) instead of Long ?

    Number and its subclasses (Long is a Number) are immutable. So if you want Facebook.id (configured by database) and its counterpart MyFriend$MyFriendId.meId share the same value, you must use MutableLong. When the database set up Facebook.id, MyFriend$MyFriendId.meId automatically get its newest value. But it just occurs if you use a MutableLong.

    0 讨论(0)
提交回复
热议问题