Hibernate 配置1对多,插入等问题的demo

Jacky_Zhu_1983 CSDN会员 Jacky_Zhu_1983 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近老是看到有人问Hibernate 1对多,多对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;
}
}


PS: 有写的不好地方希望大家改正!
...全文
评论 11
收藏 已收藏
分享
返回顶部
Jacky_Zhu_1983 CSDN会员
Web 开发 81,108 成员 341,721 内容