一对多或者多对一关系
表中的表达


orm元数据的表达


操作关系属性(保存客户)


为客户增加联系人

删除联系人

多对多
表中表达式

对象中的

orm元数据

操作关系属性

package com.domain;
public class LinkMan {
/*
* `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
* `lkm_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名', `lkm_cust_id`
* bigint(32) NOT NULL COMMENT '客户id', `lkm_gender` char(1) DEFAULT NULL
* COMMENT '联系人性别', `lkm_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
* `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机', `lkm_email`
* varchar(64) DEFAULT NULL COMMENT '联系人邮箱', `lkm_qq` varchar(16) DEFAULT
* NULL COMMENT '联系人qq', `lkm_position` varchar(16) DEFAULT NULL COMMENT
* '联系人职位', `lkm_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
*/
// 加一个非本表的字段(只是为了传值使用)
private Long cust_id;
private Long lkm_id; // 联系人编号
private String lkm_name; // 联系人姓名
private Character lkm_gender; // 联系人性别
private String lkm_phone; // 联系人办公电话
private String lkm_mobile; // 联系人手机
private String lkm_email; // 联系人邮箱
private String lkm_qq; // 联系人qq
private String lkm_position; // 联系人职位
private String lkm_memo; // 联系人备注
// 为了表达多对一的关系
private Customer customer;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public Long getLkm_id() {
return lkm_id;
}
public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public Character getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(Character lkm_gender) {
this.lkm_gender = lkm_gender;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
public String getLkm_mobile() {
return lkm_mobile;
}
public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
}
public String getLkm_email() {
return lkm_email;
}
public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
}
public String getLkm_qq() {
return lkm_qq;
}
public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
}
public String getLkm_position() {
return lkm_position;
}
public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
}
public String getLkm_memo() {
return lkm_memo;
}
public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
package com.domain;
import java.util.HashSet;
import java.util.Set;
public class Customer {
private Long cust_id;
// private String cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//表达与联系人表一对多的关系
private Set<LinkMan> linkmens =new HashSet<LinkMan>();
public Set<LinkMan> getLinkmens() {
return linkmens;
}
public void setLinkmens(Set<LinkMan> linkmens) {
this.linkmens = linkmens;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
// public String getCust_id() {
// return cust_id;
// }
// public void setCust_id(String cust_id) {
// this.cust_id = cust_id;
// }
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
+ cust_phone + ", cust_mobile=" + cust_mobile + "]";
}
}
<?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 package="com.domain">
<!-- class中的name写实体类名,table写数据库中对应的表名 -->
<class name="LinkMan" table="cst_linkman">
<!--id标签代表主键 -->
<id name="lkm_id">
<!-- 代表主键的生成模式 -->
<generator class="native"></generator>
</id>
<property name="lkm_name" ></property>
<property name="lkm_gender" ></property>
<property name="lkm_phone" ></property>
<property name="lkm_mobile" ></property>
<property name="lkm_email" ></property>
<property name="lkm_qq" ></property>
<property name="lkm_position" ></property>
<property name="lkm_memo" ></property>
<!-- 配置多对一的关系 -->
<many-to-one name="customer" column="lkm_cust_id" class="Customer"></many-to-one>
</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 package="com.domain">
<!-- class中的name写实体类名,table写数据库中对应的表名 -->
<class name="Customer" table="cst_customer">
<!--id标签代表主键 -->
<id name="cust_id" column="cust_id">
<!-- 代表主键的生成模式 -->
<generator class="native"></generator>
<!-- identity适用于Mysql主键生成策略
<generator class="identity"></generator> -->
</id>
<property name="cust_name" column="cust_name" ></property>
<property name="cust_user_id" column="cust_user_id" ></property>
<property name="cust_create_id" column="cust_create_id" ></property>
<property name="cust_source" column="cust_source" ></property>
<property name="cust_industry" column="cust_industry" ></property>
<property name="cust_level" column="cust_level" ></property>
<property name="cust_linkman" column="cust_linkman" ></property>
<property name="cust_phone" column="cust_phone" ></property>
<property name="cust_mobile" column="cust_mobile" ></property>
<!-- 配置一对多的关系 -->
<set name="linkmens">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
</class>
</hibernate-mapping>
<?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>
<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password-->
<!--必须配置的5条 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///crm?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--可选配置
#hibernate.show_sql true 自动打印sql在控制台
#hibernate.format_sql true 规范格式化打印在控制台的sql-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 配置hibernate操作数据库隔离级别 -->
<!-- #hibernate.connection.isolation 4 -->
<property name="hibernate.connection.isolation">4</property>
<!-- 获得与当前线程绑定session对象时必须要配置的 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 如果没有表就会自动生成表,如果有就会自动更新表
#hibernate.hbm2ddl.auto update -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/domain/Customer.hbm.xml"/>
<mapping resource="com/domain/LinkMan.hbm.xml"/>
<mapping resource="com/domain/User.hbm.xml"/>
<mapping resource="com/domain/Role.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.dao;
/*
*dao层写save方法
*/
import org.hibernate.Session;
import com.domain.LinkMan;
import com.util.HibernateUtil;
public class LinkManDao {
public void save(LinkMan linkman) {
Session s = HibernateUtil.getCurrentSession();
System.out.println(linkman.getCust_id());
s.save(linkman);
}
}
package com.service;
/*
* service层进行事务操作
*/
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.dao.CustomerDao;
import com.dao.LinkManDao;
import com.domain.Customer;
import com.domain.LinkMan;
import com.util.HibernateUtil;
public class LinkManService {
private CustomerDao customerDao = new CustomerDao();
private LinkManDao linkManDao = new LinkManDao();
public void save(LinkMan linkman) {
// 获得并开启事务
Session s = HibernateUtil.getCurrentSession();
Transaction tx = s.beginTransaction();
try {
// 获取所属客户id
Long cust_id = linkman.getCust_id();
// 根据客户id获得该客户对象
Customer c = customerDao.getById(cust_id);
// 表达关系
linkman.setCustomer(c);
// 保存
System.out.println("保存前");
linkManDao.save(linkman);
} catch (Exception ex) {
ex.printStackTrace();
// 事务回滚
tx.rollback();
}
// 事务提交
tx.commit();
}
}
package com.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.domain.Customer;
import com.domain.LinkMan;
import com.util.HibernateUtil;
public class Test04 {
// 保存联系人信息
@Test
public void Test1() {
// 会话(与数据库)
Session session = HibernateUtil.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 创建客户对象
Customer c = new Customer();
c.setCust_name("百度公司");
// 创建联系人对象
LinkMan lm1 = new LinkMan();
LinkMan lm2 = new LinkMan();
// 表达一对多的关系,表达一个客户对应多个联系人
c.getLinkmens().add(lm1);
c.getLinkmens().add(lm2);
// 表达多对一的关系,表达这个联系人属于哪个客户
lm1.setCustomer(c);
lm2.setCustomer(c);
// 将瞬时状态的对象变成持久化对象(把数据存入到数据库)
session.save(c);
session.save(lm1);
session.save(lm2);
// 提交事务
tx.commit();
// 关闭session
session.close();
}
// 为客户添加联系人
@Test
public void Test2() {
// 会话(与数据库)
Session session = HibernateUtil.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 获得要操作的客户
Customer c = session.get(Customer.class, 1l);
// 创建你要添加的联系人对象
LinkMan lm = new LinkMan();
lm.setLkm_name("马云");
// 表达多对一关系
lm.setCustomer(c);
// 存入数据库
session.save(lm);
// 关闭事务
tx.commit();
session.close();
}
// 删除联系人
@Test
public void Test3() {
// 会话(与数据库)
Session session = HibernateUtil.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 获得要操作的客户对象
Customer c = session.get(Customer.class, 1l);
// 获得要删除的联系人
LinkMan lm = session.get(LinkMan.class, 3l);
// 先从客户集合中将联系人对象删除
c.getLinkmens().remove(lm);
// 表达多对一的关系
lm.setCustomer(null);
session.delete(lm);
// 关闭事务
tx.commit();
session.close();
}
}
package com.web;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.domain.LinkMan;
import com.service.LinkManService;
public class AddLinkManServlet extends HttpServlet {
private LinkManService linkManService = new LinkManService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 中文乱码
request.setCharacterEncoding("UTF-8");
// 接收数据
Map<String, String[]> map = request.getParameterMap();
// 封装数据
LinkMan linkman = new LinkMan();
try {
BeanUtils.populate(linkman, map);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存
linkManService.save(linkman);
// 重定向
response.sendRedirect(request.getContextPath() + "/ListLinkManServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}