是外键关联错了吧,相关表中没数据,就不能插入

解决方案 »

  1.   

    ----BlogUser 表 public class BlogUser implements java.io.Serializable {    private static final long serialVersionUID = 8850445505288137285L;    private Integer userId;    private String userName;    private String passWordOne;    private Set<BlogFile> blogFile = new HashSet<BlogFile>();
    //省略set & get ....
    }----BlogFile 表 
    public class BlogFile implements java.io.Serializable {    private static final long serialVersionUID = -1963389377781856294L;    private Integer fileId;    private String filePath;    private String fileName;
        
        private BlogUser blogUser;    //private String createUserId;
    //省略set & get ....
    }
    ---BlogUser.hbm.xml 
    <hibernate-mapping>
        <class name="model.user.BlogUser" table="BLOG_User" schema="dbo" catalog="Blog"
                dynamic-update="true"  optimistic-lock="version" >
            <id name="userId" type="java.lang.Integer">
                <column name="userId" />
                <generator class="native" />
            </id>
            <property name="userName" type="java.lang.String">
                <column name="userName" length="50"  />
            </property>
            <property name="passWordOne" type="java.lang.String">
                <column name="passWordOne" length="50"  />
            </property>
            <set name="blogFile" inverse="true" lazy="true" cascade="all-delete-orphan">
                <key>
                    <column name="userId" />
                </key>
                <one-to-many class="model.user.BlogFile"/>
            </set>
        </class>
    </hibernate-mapping>
    ---BlogFile.hbm.xml 
    <hibernate-mapping>
        <class name="model.user.BlogFile" table="Blog_File" schema="dbo" catalog="Blog">
            <id name="fileId" type="java.lang.Integer">
                <column name="fileId" />
                <generator class="native" />
            </id>
            <property name="filePath" type="java.lang.String">
                <column name="filePath" length="200" />
            </property>
            <property name="fileName" type="java.lang.String">
                <column name="fileName" length="200" />
            </property>
            <many-to-one name="blogUser"
                class="model.user.BlogUser">
                <column name="createUserId" /> <!--createUserId 是blogUser的主键userId -->
            </many-to-one>
        </class>
    </hibernate-mapping>---action public ActionForward insertUser(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            BlogUser blogUser = new BlogUser();
            
            Set<BlogFile> file = new HashSet<BlogFile>();
            
            blogUser.setUserName("mousetsun");
            blogUser.setPassWordOne("111111");
            
            BlogFile blogFile = new BlogFile();
            blogFile.setFileName("aaa");
            blogFile.setFilePath("bbb");
            blogFile.setBlogUser(blogUser);
            file.add(blogFile);
            
            BlogFile blogFile1 = new BlogFile();
            blogFile1.setFileName("ccc");
            blogFile1.setFilePath("ddd");
            blogFile1.setBlogUser(blogUser);
            file.add(blogFile1);
            
            blogUser.setBlogFile(file);
            //spring...service 调用dao
            BlogUserService blogUserService = Locator
                    .lookupService(BlogUserService.class);
            BlogUser user = blogUserService.insertUser(blogUser);
            //********
            System.out.println(user.getBlogFile().size());
            
            return mapping.findForward("index");
        }
    ---DAO //GenericDaoHibernateImpl 是我自己写的一些hibernate常用接口方法
    public class BlogUserDaoImpl extends GenericDaoHibernateImpl<BlogUser, Integer> implements BlogUserDao{    public BlogUser insertUser(BlogUser blogUser) {
            // TODO Auto-generated method stub
            this.getHibernateTemplate().clear();
            blogUser = (BlogUser) this.getHibernateTemplate()
                    .merge(blogUser);
            this.getHibernateTemplate().flush();
            return blogUser;
        }
    }希望对你有用 >_<
      

  2.   

    谢谢楼上各位 我再仔细看看吧