在下列案例中:
客户是一;联系人是多;客户可以拥有多个联系人;一个联系人只有一个客户
1、编写实体类
package com.pojo; import java.util.HashSet; import java.util.Set; public class User { private Integer uId; //客户ID(主表) 客户是一 private String uName; //名字 private String uTel; //手机 // 在客户的表中显示多个联系人 // 使用set集合存储联系人 private Set<Link> link = new HashSet<Link>(); public Set<Link> getLink() { return link; } public void setLink(Set<Link> link) { this.link = link; } public Integer getuId() { return uId; } public void setuId(Integer uId) { this.uId = uId; } public String getuName() { return uName; } public void setuName(String uName) { this.uName = uName; } public String getuTel() { return uTel; } public void setuTel(String uTel) { this.uTel = uTel; } }
User实体类
package com.pojo; public class Link { private Integer cId; //联系人ID 联系人是多 private String cName; //姓名 private String cTel; //电话 //在联系人的实体类中显示所属客户列表;一个联系人只有一个客户 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getcId() { return cId; } public void setcId(Integer cId) { this.cId = cId; } public String getcName() { return cName; } public void setcName(String cName) { this.cName = cName; } public String getcTel() { return cTel; } public void setcTel(String cTel) { this.cTel = cTel; } }
联系人实体类
2、配置实体类映射文件
<?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="com.pojo.User" table="p_user"> <id name="uId" column="uId"> <generator class="native"></generator> </id> <property name="uName" column="uName"></property> <property name="uTel" column="uTel"></property> <!-- set标签的name属性:指的是写在客户表中的联系人的set集合的名称 inverse="true":让主表一方放弃维护 --> <set name="link" cascade="save-update,delete" inverse="false"> <!-- id 的 column 属性的值:数据表的外键名称 --> <key column="clid"></key> <!-- 客户的所有联系人; class写联系人的实体类路径 --> <one-to-many class="com.pojo.Link"/> </set> </class> </hibernate-mapping>
<?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="com.pojo.Link" table="p_link"> <id name="cId" column="cId"> <generator class="native"></generator> </id> <property name="cName" column="cName"></property> <property name="cTel" column="cTel"></property> <!-- 联系人所属客户 name:客户在联系人实体类的属性 class:联系人实体类的路径 column:外键名称 --> <many-to-one name="user" class="com.pojo.Link" column="clid"></many-to-one> </class> </hibernate-mapping>
3、配置核心文件
<?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> <!-- 1、配置数据库信息 必写的 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///person</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 2、配置hibernate信息 可写 --> <!-- 输出SQL底层语句 --> <property name="hibernate.show_sql">true</property> <!-- 输出SQL底层语句 进行格式化 --> <property name="hibernate.format_sql">true</property> <!-- 如果没有表。即创建,否则修改表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 设置数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 3、把映射文件放到核心配置文件中 必须的 --> <mapping resource="com/pojo/Link.hbm.xml"/> <mapping resource="com/pojo/User.hbm.xml"/> </session-factory> </hibernate-configuration>
4、加载核心配置文件测试是否搭建成功
package com.unitl; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static Configuration cfg = null; private static SessionFactory factory = null; static{ // 加载 hibernate 核心文件 cfg = new Configuration(); cfg.configure(); // 创建 SessionFactory对象 factory = cfg.buildSessionFactory(); } //获取 hibernate核心文件 public static SessionFactory getSessionFactory() { return factory; } //绑定本地线程session public static Session getSessionObject(){ return factory.getCurrentSession(); } /** * 调用方法 * SessionFactory factory = HibernateUtils.getSessionFactory(); * */ public static void main(String[] args) { //执行之后无报错,则配置成功 } }
5、使用方法实现增删改查操作
package com.junit; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.junit.Test; import com.pojo.Link; import com.pojo.User; import com.unitl.HibernateUtils; public class Text1 { @Test public void test() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); /** * 创建联系人 */ Link link1 = new Link(); link1.setcName("百度"); link1.setcTel("400-200-666"); /** * 创建客户 */ User user1 = new User(); user1.setuName("张三"); user1.setuTel("150*******"); /** * 把联系人对象放到客户的set集合里边 */ user1.getLink().add(link1); /** * 把客户放在联系人对象里 */ link1.setUser(user1); //添加到数据库 session.save(user1); session.save(link1); //提交事务 ts.commit(); } catch (Exception e) { ts.rollback(); } finally { session.close(); factory.close(); } } /** * 简化 * 添加操作 */ @Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //添加一个客户 User user1 = new User(); user1.setuName("李四"); user1.setuTel("150*******"); //创建联系人 Link link1 = new Link(); link1.setcName("腾讯"); link1.setcTel("400-200-666"); //把联系人放到客户的 set 集合里边 user1.getLink().add(link1); // 保存到 数据库里 session.save(user1); ts.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); ts.rollback(); }finally{ session.close(); factory.close(); } } /** * 添加一个联系人到已知的客户里 * 例子:向一个已知店铺里添加一个商品 */ @Test public void test3() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建一个联系人并添加到已知客户 Link link1 = new Link(); link1.setcName("阿里巴巴"); link1.setcTel("400-200-666"); //获取客户 User user = session.get(User.class,2); user.getLink().add(link1); session.save(user); ts.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); ts.rollback(); }finally{ session.close(); factory.close(); } } /** * 删除一个客户 */ @Test public void test4() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //查询客户ID User user = session.get(User.class, 3); session.delete(user); ts.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); ts.rollback(); }finally{ session.close(); factory.close(); } } /** * 修改操作 * 把联系人表中的ID为2的联系人改为客户一的联系人 */ @Test public void test5() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //查询联系人ID和用户ID User user = session.get(User.class, 1); Link link = session.get(Link.class, 2); // 把联系人放到客户里边;客户放到联系人里 user.getLink().add(link); link.setUser(user); ts.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); ts.rollback(); }finally{ session.close(); factory.close(); } } }
时间: 2024-11-06 10:43:02