用户表配置:<hibernate-mapping>
    <class name="com.hbm.SysUser" table="sys_user" catalog="aiwb">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="basPosition" class="com.hbm.BasPosition" fetch="select" >
            <column name="post_id" />
        </many-to-one>
        <property name="userNo" type="java.lang.String">
            <column name="user_no" length="10" />
        </property>
        <property name="userName" type="java.lang.String">
            <column name="user_name" length="30" />
        </property>
        <property name="enName" type="java.lang.String">
            <column name="en_name" length="30" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="10" />
        </property>
        <property name="isStop" type="java.lang.Short">
            <column name="is_stop" />
        </property>
        <property name="re" type="java.lang.String">
            <column name="re" length="100" />
        </property>
        <property name="createUser" type="java.lang.String">
            <column name="create_user" length="20" />
        </property>
        <property name="createDate" type="java.util.Date">
            <column name="create_date" length="19" />
        </property>
        <property name="modifyUser" type="java.lang.String">
            <column name="modify_user" length="20" />
        </property>
        <property name="modifyDate" type="java.util.Date">
            <column name="modify_date" length="19" />
        </property>
        <set name="sysUserInfos" inverse="true">
            <key>
                <column name="user_id" not-null="true" />
            </key>
            <one-to-many class="com.hbm.SysUserInfo" />
        </set>
    </class>用户信息表配置:
<hibernate-mapping>
    <class name="com.hbm.SysUserInfo" table="sys_user_info" catalog="aiwb">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="sysUser" class="com.hbm.SysUser" fetch="select" cascade="all">
            <column name="user_id" not-null="true" />
        </many-to-one>
        <property name="sex" type="java.lang.Short">
            <column name="sex" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="birthday" length="19" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" length="100" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" length="50" />
        </property>
        <property name="contactTel" type="java.lang.String">
            <column name="contact_tel" length="15" />
        </property>
        <property name="officeTel" type="java.lang.String">
            <column name="office_tel" length="15" />
        </property>
        <property name="qqMsnNo" type="java.lang.String">
            <column name="qq_msn_no" length="15" />
        </property>
        <property name="workStatus" type="java.lang.Short">
            <column name="work_status" />
        </property>
        <property name="isStop" type="java.lang.Short">
            <column name="is_stop" />
        </property>
        <property name="re" type="java.lang.String">
            <column name="re" length="100" />
        </property>
    </class>POJO类:
public class SysUserInfo implements java.io.Serializable { // Fields private Integer id;
private SysUser sysUser;
private Short sex;
private Date birthday;
private String address;
private String email;
private String contactTel;
private String officeTel;
private String qqMsnNo;
private Short workStatus;
private Short isStop;
private String re; // Constructors /** default constructor */
public SysUserInfo() {
} /** minimal constructor */
public SysUserInfo(SysUser sysUser) {
this.sysUser = sysUser;
} /** full constructor */
public SysUserInfo(SysUser sysUser, Short sex, Date birthday,
String address, String email, String contactTel, String officeTel,
String qqMsnNo, Short workStatus, Short isStop, String re,
String createUser, Date createDate, String modifyUser,
Date modifyDate) {
this.sysUser = sysUser;
this.sex = sex;
this.birthday = birthday;
this.address = address;
this.email = email;
this.contactTel = contactTel;
this.officeTel = officeTel;
this.qqMsnNo = qqMsnNo;
this.workStatus = workStatus;
this.isStop = isStop;
this.re = re;
this.createUser = createUser;
this.createDate = createDate;
this.modifyUser = modifyUser;
this.modifyDate = modifyDate;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public SysUser getSysUser() {
return this.sysUser;
} public void setSysUser(SysUser sysUser) {
this.sysUser = sysUser;
} public Short getSex() {
return this.sex;
} public void setSex(Short sex) {
this.sex = sex;
} public Date getBirthday() {
return this.birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getAddress() {
return this.address;
} public void setAddress(String address) {
this.address = address;
} public String getEmail() {
return this.email;
} public void setEmail(String email) {
this.email = email;
} public String getContactTel() {
return this.contactTel;
} public void setContactTel(String contactTel) {
this.contactTel = contactTel;
} public String getOfficeTel() {
return this.officeTel;
} public void setOfficeTel(String officeTel) {
this.officeTel = officeTel;
} public String getQqMsnNo() {
return this.qqMsnNo;
} public void setQqMsnNo(String qqMsnNo) {
this.qqMsnNo = qqMsnNo;
} public Short getWorkStatus() {
return this.workStatus;
} public void setWorkStatus(Short workStatus) {
this.workStatus = workStatus;
} public Short getIsStop() {
return this.isStop;
} public void setIsStop(Short isStop) {
this.isStop = isStop;
} public String getRe() {
return this.re;
} public void setRe(String re) {
this.re = re;
}
}

解决方案 »

  1.   


    *********************************************public class SysUser implements java.io.Serializable { // Fields private Integer id;
    private BasPosition basPosition;
    private String userNo;
    private String userName;
    private String enName;
    private String password;
    private Short isStop;
    private String re;
    private String createUser;
    private Date createDate;
    private String modifyUser;
    private Date modifyDate;
    private Set sysUserInfos = new HashSet(0);
    private Set consCluesForToUserId = new HashSet(0);
    private Set consInfosForConsUserId = new HashSet(0);
    private Set ccSalesPlans = new HashSet(0);
    private Set consCluesForFromUserId = new HashSet(0);
    private Set consInfosForMarkUserId = new HashSet(0); // Constructors /** default constructor */
    public SysUser() {
    } /** full constructor */
    public SysUser(BasPosition basPosition, String userNo, String userName,
    String enName, String password, Short isStop, String re,
    String createUser, Date createDate, String modifyUser,
    Date modifyDate, Set sysUserInfos, Set consCluesForToUserId,
    Set consInfosForConsUserId, Set ccSalesPlans,
    Set consCluesForFromUserId, Set consInfosForMarkUserId) {
    this.basPosition = basPosition;
    this.userNo = userNo;
    this.userName = userName;
    this.enName = enName;
    this.password = password;
    this.isStop = isStop;
    this.re = re;
    this.createUser = createUser;
    this.createDate = createDate;
    this.modifyUser = modifyUser;
    this.modifyDate = modifyDate;
    this.sysUserInfos = sysUserInfos;
    this.consCluesForToUserId = consCluesForToUserId;
    this.consInfosForConsUserId = consInfosForConsUserId;
    this.ccSalesPlans = ccSalesPlans;
    this.consCluesForFromUserId = consCluesForFromUserId;
    this.consInfosForMarkUserId = consInfosForMarkUserId;
    } // Property accessors public Integer getId() {
    return this.id;
    } public void setId(Integer id) {
    this.id = id;
    } public BasPosition getBasPosition() {
    return this.basPosition;
    } public void setBasPosition(BasPosition basPosition) {
    this.basPosition = basPosition;
    } public String getUserNo() {
    return this.userNo;
    } public void setUserNo(String userNo) {
    this.userNo = userNo;
    } public String getUserName() {
    return this.userName;
    } public void setUserName(String userName) {
    this.userName = userName;
    } public String getEnName() {
    return this.enName;
    } public void setEnName(String enName) {
    this.enName = enName;
    } public String getPassword() {
    return this.password;
    } public void setPassword(String password) {
    this.password = password;
    } public Short getIsStop() {
    return this.isStop;
    } public void setIsStop(Short isStop) {
    this.isStop = isStop;
    } public String getRe() {
    return this.re;
    } public void setRe(String re) {
    this.re = re;
    } public String getCreateUser() {
    return this.createUser;
    } public void setCreateUser(String createUser) {
    this.createUser = createUser;
    } public Date getCreateDate() {
    return this.createDate;
    } public void setCreateDate(Date createDate) {
    this.createDate = createDate;
    } public String getModifyUser() {
    return this.modifyUser;
    } public void setModifyUser(String modifyUser) {
    this.modifyUser = modifyUser;
    } public Date getModifyDate() {
    return this.modifyDate;
    } public void setModifyDate(Date modifyDate) {
    this.modifyDate = modifyDate;
    } public Set getSysUserInfos() {
    return this.sysUserInfos;
    } public void setSysUserInfos(Set sysUserInfos) {
    this.sysUserInfos = sysUserInfos;
    }
    }*********************测试类*****************************
    public static void main(String[] args) {

    SysUserInfoDAO dao = new SysUserInfoDAO();
    Transaction ts =dao.getSession().beginTransaction();
    BasPosition bp = new BasPosition();
    SysUser su = new SysUser();
    SysUserInfo uinfo= new SysUserInfo();

    bp.setId(1);
    su.setEnName("zhangshan");
    su.setUserName("测试1");
    su.setPassword("123");
    su.setUserNo("aaaa");
    su.setBasPosition(bp);
    uinfo.setSysUser(su);
    uinfo.setBirthday(new Date());
    uinfo.setSex(Short.valueOf("0"));
    uinfo.setContactTel("55554444");
    dao.save(uinfo);
    ts.commit();
    System.out.println("OK");

    }*******************FLEX中关键代码*************************
    数据获取:
    var uinfo:UserInfoVO=new UserInfoVO();
    var user:UserVO=new UserVO();

    user.basPosition=bp;
    user.userName=username.text;
    user.userNo=userid.text;
    user.userPwd=userpwd.text;

    uinfo.sysUser=user;
    uinfo.birthday=new Date(birthday.selectedDate.toDateString());
    uinfo.contactTel=phone.text;
    uinfo.officeTel=officephone.text;
    uinfo.email=email.text;
    uinfo.address=address.text;
    impl类新增方法:
    public int addUserInfo(BaseDM baseDM) throws FatalException {

    SysUserInfoDAO dao = new SysUserInfoDAO();
    if(baseDM!=null) {
    SysUserInfo dm = (SysUserInfo)baseDM;
    Transaction t = dao.getSession().beginTransaction();
    dao.save(dm);
    t.commit();
    return 0;
    }
    return -1;
    }
    在普通的JAVA工程上测试,多对一级联插入成功,但是在flex+hibernate框架下执行保存时控制台上的hibernate提示信息却没看到有插入主表的sql语法,只有插入从表的语法,因主表没插入,关键找不到,晕倒,在DEBUG模式下POJO内数据均存在,郁闷ing......
    Cannot add or update a child row: a foreign key constraint fails (`aiwb/sys_user_info`, CONSTRAINT `FK660835B05E30B4FF` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`id`))到底是框架问题还是我的设置问题?烦请高手赐教.hibernate第一次采用多表级联,不太会用,TKS!