Hibernate框架技术相信对大多数的 java 程序员并不陌生,数据表之间的关系如何通过Hibernate来建立,需要我们认真的分析数据表中数据项之间的交互;
数据库表的之间的关系有:
(1)一对多(1:n)(班级:学生)
(2)多对多 (m:n)(订单;商品)
(3)一对一 (1:1)用的 比较少 合并到 一个 表就可以达到需求
今天首先研究一下啊
表的一对多关系
Demo :实现t_user(用户表) 和 t_orders(订单表)的双向一对多关系的建表实现 {测试例:保存一个用户 user 包含多个 orders的用户 }
程序所有的 jar和 dtd:
数据库结构图:
)A:创建 两个 实体:让两个实体之间互相的引用##
User实体:注意 颜色文档
package store_entity; import java.sql.Timestamp; import java.util.Set; /** * 用户类 实体类 * @author Administrator * */ public class User { private int id; private String username; private String password; private String nickname; private String email;// 激活邮件地址 private String role; private int state;// 激活的状态 private String activecode;// 激活码 private Timestamp updatetime;// 时期的时间戳 private Set<Orders> orders;//用户 下的 订单集合 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getActivecode() { return activecode; } public void setActivecode(String activecode) { this.activecode = activecode; } public Timestamp getUpdatetime() { return updatetime; } public void setUpdatetime(Timestamp updatetime) { this.updatetime = updatetime; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickname=" + nickname + ", email=" + email + ", role=" + role + ", state=" + state + ", activecode=" + activecode + ", updatetime=" + updatetime + "]"; } public Set<Orders> getOrders() { return orders; } public void setOrders(Set<Orders> orders) { this.orders = orders; } }
Orders实体类 :注意颜色 文本
package store_entity; import java.sql.Timestamp; import java.util.Set; /** * * 订单 实体类 * @author Administrator * */ public class Orders { private String id;//数据库 需要 自己 手动 添加 private String receiverinfo;//收获地址 private int paystate;//付款 状态 private Timestamp ordertime;//订单创建 时间 private User user;//下单 用户 id private Set<OrderItem> set; public Set<OrderItem> getSet() { return set; } public void setSet(Set<OrderItem> set) { this.set = set; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getReceiverinfo() { return receiverinfo; } public void setReceiverinfo(String receiverinfo) { this.receiverinfo = receiverinfo; } public int getPaystate() { return paystate; } public void setPaystate(int paystate) { this.paystate = paystate; } public Timestamp getOrdertime() { return ordertime; } public void setOrdertime(Timestamp ordertime) { this.ordertime = ordertime; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Orders [id=" + id + ", receiverinfo=" + receiverinfo + ", paystate=" + paystate + ", ordertime=" + ordertime + ", user=" + user + ", set=" + set + "]"; } }
)B 配置映射文件
hibernate.cfg.xml (Hibernate配置文件)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置连接数据库的信息 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///Estore</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 配置hibernate的信息 可选 --> <property name="current_session_context_class">thread</property> <!-- 数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 输出底层sql语句 --> <property name="show_sql">true</property> <!-- 输出底层sql语句格式化 --> <property name="format_sql">true</property> <!-- hibernate创建表的策略 update: 没有就创建,不一个就更新 --> <property name="hbm2ddl.auto">update</property> <!-- 将映射文件配置到核心文件中 --> <mapping resource="store_entity/User.hbm.xml"/> <mapping resource="store_entity/Orders.hbm.xml"/> </session-factory> </hibernate-configuration>
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- class标记 name="实体类的全路径" table=数据库表名 --> <class name="store_entity.User" table="t_user"> <!-- id 表示主键 hibernate要求实体类有一个属性为主键 --> <id name="id" column="user_id"> <!-- 主键的生成策略 native:生成的表id为自增长的,它可以兼容多种数据库 --> <generator class="native"></generator> </id> <!-- 非主键列 name:实体类中的属性名 column:表的列名 --> <property name="username" column="name" ></property> <property name="password" column="password" ></property> <property name="nickname" column="nickname" ></property> <property name="email" column="email" ></property> <property name="role" column="role" ></property> <property name="state" column="state" ></property> <property name="activecode" column="activecode" ></property> <property name="updatetime" column="updatetime" type="timestamp"></property> <!--外界关系 设置 --> <!-- 使用set标签表示所有联系人 name:实体类中set集合的名字 cascade=save-update 级联保存和更新 --> <set name="orders" cascade="save-update,delete" inverse="true"> <!-- column:外键列的名称 --> <key column="user_id"></key> <!-- 包含外键列的表所对应的实体类 --> <one-to-many class="store_entity.Orders" ></one-to-many> </set> </class> </hibernate-mapping>
Orders.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- class标记 name="实体类的全路径" table=数据库表名 --> <class name="store_entity.Orders" table="t_orders"> <!-- id 表示主键 hibernate要求实体类有一个属性为主键 --> <id name="id" column="order_id"> </id> <property name="receiverinfo" column="receiverinfo" ></property> <property name="paystate" column="paystate" ></property> <property name="ordertime" column="ordertime" type="timestamp" ></property> <!-- 外键设置 --> <many-to-one name="user" class="store_entity.User" column="user_id"></many-to-one> <!-- 单项 一对多 --> </class> </hibernate-mapping>
)C 测试类编写:
package store_utils; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import store_entity.Orders; import store_entity.User; public class HibernateUtils { private static SessionFactory factory=null; static{ //1 加载配置文件(核心配置文件) Configuration cfg = new Configuration().configure(); //2 创建一个SessionFactory factory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return factory; } public static Session getCurrentSession(){ return factory.getCurrentSession(); } public static Session getSession(){ return factory.openSession(); } public static void main(String args[]){ //测试 存储 用户 和 订单 User user =new User(); user.setUsername("狗爷2"); user.setNickname("sss"); user.setEmail("7168@qq.com"); user.setPassword("1111"); user.setRole("1"); Set<Orders> set=new HashSet<Orders>(); Orders o1=new Orders(); o1.setId("1112"); o1.setPaystate(0); o1.setReceiverinfo("水果w"); o1.setUser(user); set.add(o1); user.setOrders(set); Session session =null; Transaction ts = null; try{ session = HibernateUtils.getCurrentSession(); ts = session.beginTransaction(); session.save(user); ts.commit();//事务提交 System.out.println("ddddd"); } catch(Exception e){ e.printStackTrace(); ts.rollback(); } } }
测试结果:
col输出: