一对多
一对多的实体 多对一的实体
需要使用set集合用于保存对象 需要一对多实体的对象
set<对象> 变量名 对象 变量名
package domain; import java.util.HashSet; import java.util.Set; public class Customer { public SetgetLinkMans() { return linkMans; } public void setLinkMans(Set linkMans) { this.linkMans = linkMans; } private Long cust_id; private String cust_name; 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 linkMans = new HashSet (); public Customer() { super(); } public Customer(String cust_name,Long cust_id) { super(); this.cust_id = cust_id; this.cust_name=cust_name; } 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 void setCust_name(String cust_name) { this.cust_name = cust_name; } 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 + "]"; } }
package domain; public class LinkMan { @Override public String toString() { return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone=" + lkm_phone + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq + ", lkm_mobile=" + lkm_mobile + ", lkm_memo=" + lkm_memo + ", lkm_postion=" + lkm_postion + ", customer=" + customer + "]"; } private Long lkm_id; private String lkm_name; private Character lkm_gender; private String lkm_phone; private String lkm_email; private String lkm_qq; private String lkm_mobile; private String lkm_memo; private String lkm_postion; private Customer customer; 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_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_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } public String getLkm_postion() { return lkm_postion; } public void setLkm_postion(String lkm_postion) { this.lkm_postion = lkm_postion; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
orm元数据的表达就是
多对一
配置号xml后即可开始操作
public void func1() { Session sesion = HibernateUtils.openSession(); Transaction tx = sesion.beginTransaction(); //-------------------------------------- Customer c = new Customer(); c.setCust_name("as"); LinkMan linkman = new LinkMan(); linkman.setLkm_name("sss"); LinkMan linkman2 = new LinkMan(); linkman2.setLkm_name("ccc"); //一对多 c.getLinkMans().add(linkman); c.getLinkMans().add(linkman2); //多对一 linkman.setCustomer(c); linkman2.setCustomer(c); sesion.save(c);
/*设置级联 不需要下面两行代码*/ sesion.save(linkman); sesion.save(linkman2); //-------------------------------------- tx.commit(); sesion.close(); }
这时候可以设置级联操作 就可以只写一边save了、 cascade属性
//一对多
//多对一
保存时.两方都会维护外键关系.关系维护两次,重复了.
显然只需要一端在维护关系
//一对多
//多对一
多对多
实体1 实体2
需要使用set集合用于保存对象 需要使用set集合用于保存对象
set<对象> 变量名 set<对象> 变量名
package domain; import java.util.HashSet; import java.util.Set; public class User { @Override public String toString() { return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password=" + user_password + ", user_state=" + user_state + ", roles=" + roles + "]"; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public SetgetRoles() { return roles; } public void setRoles(Set roles) { this.roles = roles; } private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; private Set roles = new HashSet (); public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } }
package domain; import java.util.HashSet; import java.util.Set; public class Role { public SetgetUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Long getRole_id() { return role_id; } public void setRole_id(Long role_id) { this.role_id = role_id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } public String getRole_memo() { return role_memo; } public void setRole_memo(String role_memo) { this.role_memo = role_memo; } private Long role_id; private String role_name; private String role_memo; private Set users = new HashSet (); }
orm元数据
操作多对多
public class demo3 { public void function1() { Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //-------------------------------------- User user1 = new User(); User user2 = new User(); user1.setUser_name("user1"); user2.setUser_name("user2"); Role role1 = new Role(); Role role2 = new Role(); role1.setRole_name("role1"); role2.setRole_name("role2"); user1.getRoles().add(role1); user1.getRoles().add(role2); user2.getRoles().add(role1); user2.getRoles().add(role2); role1.getUsers().add(user1); role1.getUsers().add(user2); role2.getUsers().add(user1); role2.getUsers().add(user2); session.save(user1); session.save(user2); session.save(role1); session.save(role2); //-------------------------------------- tx.commit(); session.close(); }
@Test
public void function3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//--------------------------------------
//删除关系
User user = session.get(User.class, 1l);
Role role1 = session.get(Role.class, 1l);
Role role2 = session.get(Role.class, 2l);
Role role3 = session.get(Role.class, 3l);
user.getRoles().remove(role1);
user.getRoles().remove(role2);
user.getRoles().remove(role3);
//session.save(user);
//--------------------------------------
tx.commit();
session.close();
}
}
多对多 使用inverse属性 只允许一方维护关系