15.Hibernate一对多双向关联映射+分页
Posted 红酒人生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了15.Hibernate一对多双向关联映射+分页相关的知识,希望对你有一定的参考价值。
1.创建如下数据库脚本
1 --创建用户信息表 2 --编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址 3 create table users 4 ( 5 id number(10) primary key, 6 username varchar2(20) not null, 7 password varchar2(40) not null, 8 age number(10) not null, 9 sex number(10) not null, 10 nickname varchar2(20) not null, 11 mobile varchar2(15) not null, 12 address varchar2(50) not null, 13 supper number(10) not null, 14 picpath varchar2(100) not null 15 ); 16 17 --创建微博表 18 --编号,内容,发布时间,用户编号 19 create table blog 20 ( 21 id number(10) primary key, 22 content varchar2(1000) not null, 23 publishtime date not null, 24 userid number(10) references users(id) 25 ); 26 27 --创建序列 28 create sequence seq_users; 29 create sequence seq_blog; 30 31 --插入数据 32 insert into users 33 ( 34 id,username,password,age,sex,nickname, 35 mobile,address ,supper,picpath 36 ) 37 values 38 (seq_users.nextval,\'holly\',\'123\',18,0,\'holly上神\', 39 \'13451802404\',\'雨花台铁心桥新河苑\',0,\'holly.jpg\'); 40 41 insert into users 42 ( 43 id,username,password,age,sex,nickname, 44 mobile,address ,supper,picpath 45 ) 46 values 47 (seq_users.nextval,\'倩倩\',\'123\',18,0,\'倩倩上仙\', 48 \'13451805648\',\'大桥北路\',1,\'qianqian.jpg\'); 49 50 insert into users 51 ( 52 id,username,password,age,sex,nickname, 53 mobile,address ,supper,picpath 54 ) 55 values 56 (seq_users.nextval,\'死胖子\',\'123\',28,1,\'死盘子小仙\', 57 \'13451804869\',\'湖北非洲\',1,\'sipangzi.jpg\'); 58 59 insert into users 60 ( 61 id,username,password,age,sex,nickname, 62 mobile,address ,supper,picpath 63 ) 64 values 65 (seq_users.nextval,\'肉肉\',\'123\',38,1,\'肉肉小妖\', 66 \'13451885697\',\'新街口八条巷\',1,\'rourou.jpg\'); 67 68 insert into users 69 ( 70 id,username,password,age,sex,nickname, 71 mobile,address ,supper,picpath 72 ) 73 values 74 (seq_users.nextval,\'戴文\',\'123\',38,1,\'文文上仙\', 75 \'13451888569\',\'东海瀛洲\',1,\'daiwen.jpg\'); 76 77 commit; 78 79 insert into blog 80 (id,content,publishtime,userid) 81 values 82 ( 83 seq_blog.nextval,\'上神已经下凡渡劫失败...\', 84 to_date(\'2017-01-01\',\'yyyy-MM-dd\'),1 85 ); 86 87 insert into blog 88 (id,content,publishtime,userid) 89 values 90 ( 91 seq_blog.nextval,\'上神已经去东海瀛洲去营救...\', 92 to_date(\'2017-01-02\',\'yyyy-MM-dd\'),1 93 ); 94 95 insert into blog 96 (id,content,publishtime,userid) 97 values 98 ( 99 seq_blog.nextval,\'上神中午下凡去看团子...\', 100 to_date(\'2017-01-03\',\'yyyy-MM-dd\'),1 101 ); 102 103 insert into blog 104 (id,content,publishtime,userid) 105 values 106 ( 107 seq_blog.nextval,\'上神中午已去十里桃源...\', 108 to_date(\'2017-01-04\',\'yyyy-MM-dd\'),1 109 ); 110 111 insert into blog 112 (id,content,publishtime,userid) 113 values 114 ( 115 seq_blog.nextval,\'小仙已经去了诛仙台...\', 116 to_date(\'2017-01-05\',\'yyyy-MM-dd\'),2 117 ); 118 commit; 119 120 select * from users u,blog b 121 where u.id=b.userid;
2.创建如下项目结构
3.在src下的com.pojo包下创建Users.java 类
1 package com.pojo; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 /** 8 * 一方(一对多) 9 * 一方:引入多方集合 10 *@author 北大青鸟南京中博 Holly老师 11 * 12 */ 13 public class Users implements Serializable{ 14 /** 15 * 16 */ 17 private static final long serialVersionUID = 1L; 18 private Integer id; //用户编号 19 private String username; //用户名 20 private String password; //用户密码 21 private Integer age; //年龄 22 private Integer sex; //性别 23 private String nickname; //昵称 24 private String mobile; //手机 25 private String address; //地址 26 private Integer supper; //是否是管理员 27 private String picpath; //头像名称 28 /*hibernte一对多*/ 29 private Set<Blog> blogset=new HashSet<Blog>(); 30 31 public Users() { 32 } 33 34 public Users(String username, String password, Integer age, Integer sex, 35 String nickname, String mobile, String address, Integer supper, 36 String picpath) { 37 this.username = username; 38 this.password = password; 39 this.age = age; 40 this.sex = sex; 41 this.nickname = nickname; 42 this.mobile = mobile; 43 this.address = address; 44 this.supper = supper; 45 this.picpath = picpath; 46 } 47 48 public Users(Integer id, String username, String password, Integer age, 49 Integer sex, String nickname, String mobile, String address, 50 Integer supper, String picpath) { 51 this.id = id; 52 this.username = username; 53 this.password = password; 54 this.age = age; 55 this.sex = sex; 56 this.nickname = nickname; 57 this.mobile = mobile; 58 this.address = address; 59 this.supper = supper; 60 this.picpath = picpath; 61 } 62 63 64 65 public Users(Integer id, String username, String password, Integer age, 66 Integer sex, String nickname, String mobile, String address, 67 Integer supper, String picpath, Set<Blog> blogset) { 68 super(); 69 this.id = id; 70 this.username = username; 71 this.password = password; 72 this.age = age; 73 this.sex = sex; 74 this.nickname = nickname; 75 this.mobile = mobile; 76 this.address = address; 77 this.supper = supper; 78 this.picpath = picpath; 79 this.blogset = blogset; 80 } 81 82 public Integer getId() { 83 return id; 84 } 85 public void setId(Integer id) { 86 this.id = id; 87 } 88 public String getUsername() { 89 return username; 90 } 91 public void setUsername(String username) { 92 this.username = username; 93 } 94 public String getPassword() { 95 return password; 96 } 97 public void setPassword(String password) { 98 this.password = password; 99 } 100 public Integer getAge() { 101 return age; 102 } 103 public void setAge(Integer age) { 104 this.age = age; 105 } 106 public Integer getSex() { 107 return sex; 108 } 109 public void setSex(Integer sex) { 110 this.sex = sex; 111 } 112 public String getNickname() { 113 return nickname; 114 } 115 public void setNickname(String nickname) { 116 this.nickname = nickname; 117 } 118 public String getMobile() { 119 return mobile; 120 } 121 public void setMobile(String mobile) { 122 this.mobile = mobile; 123 } 124 public String getAddress() { 125 return address; 126 } 127 public void setAddress(String address) { 128 this.address = address; 129 } 130 public Integer getSupper() { 131 return supper; 132 } 133 public void setSupper(Integer supper) { 134 this.supper = supper; 135 } 136 public String getPicpath() { 137 return picpath; 138 } 139 public void setPicpath(String picpath) { 140 this.picpath = picpath; 141 } 142 143 144 145 public Set<Blog> getBlogset() { 146 return blogset; 147 } 148 149 public void setBlogset(Set<Blog> blogset) { 150 this.blogset = blogset; 151 } 152 153 @Override 154 public String toString() { 155 return "Users [address=" + address + ", age=" + age + ", blogset=" 156 + blogset + ", id=" + id + ", mobile=" + mobile + ", nickname=" 157 + nickname + ", password=" + password + ", picpath=" + picpath 158 + ", sex=" + sex + ", supper=" + supper + ", username=" 159 + username + "]"; 160 } 161 162 163 }
4.在src下的com.pojo包下创建Blog.java类
1 package com.pojo; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 /** 7 * 多方:多对一 8 * 多方:配置对象 9 * @author 北大青鸟南京中博 Holly老师 10 * 11 */ 12 public class Blog implements Serializable{ 13 /** 14 * 15 */ 16 private static final long serialVersionUID = 1L; 17 private Integer id; 18 private String content; 19 private Date publishtime; 20 21 /*多方外建列:引入一方对象*/ 22 private Users users; 23 24 public Blog() { 25 } 26 27 public Blog(String content, Date publishtime, Users users) { 28 this.content = content; 29 this.publishtime = publishtime; 30 this.users = users; 31 } 32 33 public Blog(Integer id, String content, Date publishtime, Users users) { 34 this.id = id; 35 this.content = content; 36 this.publishtime = publishtime; 37 this.users = users; 38 } 39 40 public Integer getId() { 41 return id; 42 } 43 44 public void setId(Integer id) { 45 this.id = id; 46 } 47 48 public String getContent() { 49 return content; 50 } 51 52 public void setContent(String content) { 53 this.content = content; 54 } 55 56 public Date getPublishtime() { 57 return publishtime; 58 } 59 60 public void setPublishtime(Date publishtime) { 61 this.publishtime = publishtime; 62 } 63 64 public Users getUsers() { 65 return users; 66 } 67 68 public void setUsers(Users users) { 69 this.users = users; 70 } 71 72 @Override 73 public String toString() { 74 return "Blog [content=" + content + ", id=" + id + ", publishtime=" 75 + publishtime + "]"; 76 } 77 78 79 }
5.在src下的com.pojo包下创建Users.hbm.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 06章 映射一对多双向关联关系以及cascadeinverse属性06章 映射一对多双向关联关系以及cascadeinverse属性