欢乐极客
2010-11-03 09:36
浏览 374
已采纳

hibernate自关联问题

    比如我要定义一个用户实体,如User。在这个用户实体里面,我想要定义用户的好友、黑名单和关注对象。于是我用了Set<User>的方式,然后在配置文件中使用了<Set>的配置,Set内使用many-to-many方式。我的目的就是让User自关联对多对,生成一个中间表,如User_friends。这样一来在运行的时候没问题,能生成所有预想的表,且能插入数据到User_INFO(User对应的表)中,但无论如何都不能将数据写入到对多对的关联表中。请高手们赐教,感谢不尽。


package org.fantasizer.blog.core.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

/**

  • [CN]用户实体
  • @author Marco Jee
  • @date 2010-11-1
    */
    @SuppressWarnings("serial")
    public class User implements Serializable {

    // ----------------------Instance Variables---------------------------//
    private Long id;// primary key
    private String loginname;// 登录名
    private String nickname;// 昵称
    private String password;// 密码MD5码
    private String email;// email
    private Date birthdate;// 生日
    private String gender;// 性别
    private String marriage;// 婚姻状况
    private String msn;// msn
    private String qq;// qq
    private String phone;// 电话号码
    private String realname;// 真实姓名
    private String country;// 国家
    private String address;// 地址
    private String birthplace;// 出生地
    private String schoolname;
    private String strongSuit;// 特长
    private String personalSign;// 个性签名
    private Date lastLoginTime;// 上次登录时间
    private Date lastLogoutTime;// 上次注销时间
    private Date registerTime;// 注册时间
    private Date loginTime;// 本次登录时间
    private Integer loginCount;// 登录次数
    private String lastIp;// 上次登录IP
    private String ip;// 本次登录IP
    private Boolean disabled;// 是否可用
    private String picture;// 头像路径

    private Set<User> friends;// 好友
    private Set<User> attentions;// 关注对象
    private Set<User> blcaklist;// 黑名单

    // ----------------------Getters and Setters---------------------------//
    //Getters和Setters以及equals与hashCode方法由于太长,在此省略
    }

    
    

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.fantasizer.blog.core.entity">
<class name="User" table="user_info" lazy="false">
<id name="id" column="userid">
<generator class="native" />
</id>
<property name="loginname" column="loginname" type="string"
length="20" unique="true" not-null="true" />
<property name="nickname" column="nickname" type="string"
length="20" not-null="true" />
<property name="password" column="password" type="string"
length="50" not-null="true" />
<property name="email" column="email" type="string" length="50"
not-null="true" unique="true" />
<property name="birthdate" column="birthdate" type="date" />
<property name="gender" column="gender" type="string" length="6" />
<property name="marriage" column="marriage" type="string"
length="6" />
<property name="msn" column="msn" type="string" length="50" />
<property name="qq" column="qq" type="string" length="50" />
<property name="phone" column="phone" type="string" length="15" />
<property name="realname" column="realname" type="string"
length="50" />
<property name="country" column="country" type="string"
length="50" />
<property name="address" column="address" type="string"
length="100" />
<property name="birthplace" column="birthplace" type="string"
length="100" />
<property name="schoolname" column="schoolname" type="string"
length="50" />
<property name="strongSuit" column="strongSuit" type="string"
length="200" />
<property name="personalSign" column="personalSign" type="string"
length="200" />
<property name="lastLoginTime" column="lastLoginTime" type="timestamp" />
<property name="lastLogoutTime" column="lastLogoutTime" type="timestamp" />
<property name="registerTime" column="registerTime" type="timestamp" />
<property name="loginTime" column="loginTime" type="timestamp" />
<property name="loginCount" column="loginCount" type="java.lang.Integer" />
<property name="lastIp" column="lastIp" type="string" length="15" />
<property name="ip" column="ip" type="string" length="15" />
<property name="disabled" column="disabled" type="boolean" />
<property name="picture" column="picture" type="string"
length="100" />

    &lt;set name="friends" inverse="true" table="user_friends" lazy="false"&gt;
        &lt;key column="userid" /&gt;
        &lt;many-to-many class="User" column="friendid" /&gt;
    &lt;/set&gt;
    &lt;set name="attentions" inverse="true" table="User_attentions"&gt;
        &lt;key column="userid" /&gt;
        &lt;many-to-many class="User" column="attentionid" /&gt;
    &lt;/set&gt;
    &lt;set name="blcaklist" inverse="true" table="user_blacklist"&gt;
        &lt;key column="userid" /&gt;
        &lt;many-to-many class="User" column="blcaklistid" /&gt;
    &lt;/set&gt;
&lt;/class&gt;

</hibernate-mapping>


另外补充一下,使用Hibernate的时候,想UserRelationship这样的类能不能当成实体?即表达的不是独立的实体,而是表示实体与实体之间关系的,谢谢各位~
问题补充

txc_tang 写道

帮你写了一个Annotation的实体类,映射关系劳烦你自己转换下,希望对你有用,呵呵

package com.bebig.shopping.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "tbl_QQUser")
public class QQUser {
    private Long id;// primary key
    private String loginname;// 登录名
    private String nickname;// 昵称
    private String password;// 密码MD5码
    private String email;// email
    private Date birthdate;// 生日
    private String gender;// 性别
    private String marriage;// 婚姻状况
    private String msn;// msn
    private String qq;// qq
    private String phone;// 电话号码
    private String realname;// 真实姓名
    private String country;// 国家
    private String address;// 地址
    private String birthplace;// 出生地
    private String schoolname;
    private String strongSuit;// 特长
    private String personalSign;// 个性签名
    private Date lastLoginTime;// 上次登录时间
    private Date lastLogoutTime;// 上次注销时间
    private Date registerTime;// 注册时间
    private Date loginTime;// 本次登录时间
    private Integer loginCount;// 登录次数
    private String lastIp;// 上次登录IP
    private String ip;// 本次登录IP
    private Boolean disabled;// 是否可用
    private String picture;// 头像路径

    private Set<QQUser> friends = new HashSet<QQUser>();// 好友
    private Set<QQUser> attentions= new HashSet<QQUser>();// 关注对象
    private Set<QQUser> blcaklist= new HashSet<QQUser>();// 黑名单

    public String getAddress() {
        return address;
    }
    @ManyToMany(cascade={CascadeType.ALL})
    @JoinTable(name = "tbl_QQUserAndAttentions", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "attentionId") })
    public Set<QQUser> getAttentions() {
        return attentions;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public String getBirthplace() {
        return birthplace;
    }
    
    @ManyToMany(cascade={CascadeType.ALL})
    @JoinTable(name = "tbl_QQUserAndBlcaklist", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "blcaklistId") })
    public Set<QQUser> getBlcaklist() {
        return blcaklist;
    }

    public String getCountry() {
        return country;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public String getEmail() {
        return email;
    }

    @ManyToMany(cascade={CascadeType.ALL})
    @JoinTable(name = "tbl_QQUserAndFriends", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "friendId") })
    public Set<QQUser> getFriends() {
        return friends;
    }

    public String getGender() {
        return gender;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public String getIp() {
        return ip;
    }

    public String getLastIp() {
        return lastIp;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public Date getLastLogoutTime() {
        return lastLogoutTime;
    }

    public Integer getLoginCount() {
        return loginCount;
    }

    public String getLoginname() {
        return loginname;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public String getMarriage() {
        return marriage;
    }

    public String getMsn() {
        return msn;
    }

    public String getNickname() {
        return nickname;
    }

    public String getPassword() {
        return password;
    }

    public String getPersonalSign() {
        return personalSign;
    }

    public String getPhone() {
        return phone;
    }

    public String getPicture() {
        return picture;
    }

    public String getQq() {
        return qq;
    }

    public String getRealname() {
        return realname;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public String getSchoolname() {
        return schoolname;
    }

    public String getStrongSuit() {
        return strongSuit;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAttentions(Set<QQUser> attentions) {
        this.attentions = attentions;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public void setBirthplace(String birthplace) {
        this.birthplace = birthplace;
    }

    public void setBlcaklist(Set<QQUser> blcaklist) {
        this.blcaklist = blcaklist;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFriends(Set<QQUser> friends) {
        this.friends = friends;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setLastIp(String lastIp) {
        this.lastIp = lastIp;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public void setLastLogoutTime(Date lastLogoutTime) {
        this.lastLogoutTime = lastLogoutTime;
    }

    public void setLoginCount(Integer loginCount) {
        this.loginCount = loginCount;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public void setMarriage(String marriage) {
        this.marriage = marriage;
    }

    public void setMsn(String msn) {
        this.msn = msn;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPersonalSign(String personalSign) {
        this.personalSign = personalSign;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public void setSchoolname(String schoolname) {
        this.schoolname = schoolname;
    }

    public void setStrongSuit(String strongSuit) {
        this.strongSuit = strongSuit;
    }
}

哥们儿,非常感谢你的回复。不过我对Annotation理解不深呀,只有继续坐等了。。

 

在此感谢~~

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • surechun 2010-11-03 09:36
    已采纳

    帮你写了一个Annotation的实体类,映射关系劳烦你自己转换下,希望对你有用,呵呵

    package com.bebig.shopping.model;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "tbl_QQUser")
    public class QQUser {
        private Long id;// primary key
        private String loginname;// 登录名
        private String nickname;// 昵称
        private String password;// 密码MD5码
        private String email;// email
        private Date birthdate;// 生日
        private String gender;// 性别
        private String marriage;// 婚姻状况
        private String msn;// msn
        private String qq;// qq
        private String phone;// 电话号码
        private String realname;// 真实姓名
        private String country;// 国家
        private String address;// 地址
        private String birthplace;// 出生地
        private String schoolname;
        private String strongSuit;// 特长
        private String personalSign;// 个性签名
        private Date lastLoginTime;// 上次登录时间
        private Date lastLogoutTime;// 上次注销时间
        private Date registerTime;// 注册时间
        private Date loginTime;// 本次登录时间
        private Integer loginCount;// 登录次数
        private String lastIp;// 上次登录IP
        private String ip;// 本次登录IP
        private Boolean disabled;// 是否可用
        private String picture;// 头像路径
    
        private Set<QQUser> friends = new HashSet<QQUser>();// 好友
        private Set<QQUser> attentions= new HashSet<QQUser>();// 关注对象
        private Set<QQUser> blcaklist= new HashSet<QQUser>();// 黑名单
    
        public String getAddress() {
            return address;
        }
        @ManyToMany(cascade={CascadeType.ALL})
        @JoinTable(name = "tbl_QQUserAndAttentions", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "attentionId") })
        public Set<QQUser> getAttentions() {
            return attentions;
        }
    
        public Date getBirthdate() {
            return birthdate;
        }
    
        public String getBirthplace() {
            return birthplace;
        }
        
        @ManyToMany(cascade={CascadeType.ALL})
        @JoinTable(name = "tbl_QQUserAndBlcaklist", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "blcaklistId") })
        public Set<QQUser> getBlcaklist() {
            return blcaklist;
        }
    
        public String getCountry() {
            return country;
        }
    
        public Boolean getDisabled() {
            return disabled;
        }
    
        public String getEmail() {
            return email;
        }
    
        @ManyToMany(cascade={CascadeType.ALL})
        @JoinTable(name = "tbl_QQUserAndFriends", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "friendId") })
        public Set<QQUser> getFriends() {
            return friends;
        }
    
        public String getGender() {
            return gender;
        }
    
        @Id
        @GeneratedValue
        public Long getId() {
            return id;
        }
    
        public String getIp() {
            return ip;
        }
    
        public String getLastIp() {
            return lastIp;
        }
    
        public Date getLastLoginTime() {
            return lastLoginTime;
        }
    
        public Date getLastLogoutTime() {
            return lastLogoutTime;
        }
    
        public Integer getLoginCount() {
            return loginCount;
        }
    
        public String getLoginname() {
            return loginname;
        }
    
        public Date getLoginTime() {
            return loginTime;
        }
    
        public String getMarriage() {
            return marriage;
        }
    
        public String getMsn() {
            return msn;
        }
    
        public String getNickname() {
            return nickname;
        }
    
        public String getPassword() {
            return password;
        }
    
        public String getPersonalSign() {
            return personalSign;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public String getPicture() {
            return picture;
        }
    
        public String getQq() {
            return qq;
        }
    
        public String getRealname() {
            return realname;
        }
    
        public Date getRegisterTime() {
            return registerTime;
        }
    
        public String getSchoolname() {
            return schoolname;
        }
    
        public String getStrongSuit() {
            return strongSuit;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public void setAttentions(Set<QQUser> attentions) {
            this.attentions = attentions;
        }
    
        public void setBirthdate(Date birthdate) {
            this.birthdate = birthdate;
        }
    
        public void setBirthplace(String birthplace) {
            this.birthplace = birthplace;
        }
    
        public void setBlcaklist(Set<QQUser> blcaklist) {
            this.blcaklist = blcaklist;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        public void setDisabled(Boolean disabled) {
            this.disabled = disabled;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public void setFriends(Set<QQUser> friends) {
            this.friends = friends;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public void setIp(String ip) {
            this.ip = ip;
        }
    
        public void setLastIp(String lastIp) {
            this.lastIp = lastIp;
        }
    
        public void setLastLoginTime(Date lastLoginTime) {
            this.lastLoginTime = lastLoginTime;
        }
    
        public void setLastLogoutTime(Date lastLogoutTime) {
            this.lastLogoutTime = lastLogoutTime;
        }
    
        public void setLoginCount(Integer loginCount) {
            this.loginCount = loginCount;
        }
    
        public void setLoginname(String loginname) {
            this.loginname = loginname;
        }
    
        public void setLoginTime(Date loginTime) {
            this.loginTime = loginTime;
        }
    
        public void setMarriage(String marriage) {
            this.marriage = marriage;
        }
    
        public void setMsn(String msn) {
            this.msn = msn;
        }
    
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void setPersonalSign(String personalSign) {
            this.personalSign = personalSign;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public void setPicture(String picture) {
            this.picture = picture;
        }
    
        public void setQq(String qq) {
            this.qq = qq;
        }
    
        public void setRealname(String realname) {
            this.realname = realname;
        }
    
        public void setRegisterTime(Date registerTime) {
            this.registerTime = registerTime;
        }
    
        public void setSchoolname(String schoolname) {
            this.schoolname = schoolname;
        }
    
        public void setStrongSuit(String strongSuit) {
            this.strongSuit = strongSuit;
        }
    }
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题