“一”的一方为主表,“多”的一方为副表,主表关联副表,应该在主表中加入副表对象作为属性。
根据顾客ID查询顾客信息 (一) ,同时将顾客名下所有订单查出 (多) 。
实现思路:


package org.example.Entity;import java.util.List;
public class CustomerEntity {private int id;private String name;private Integer age;//在顾客表中加入订单集合private List orders;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public List getOrders() {return orders;}public void setOrders(List orders) {this.orders = orders;}
}
package org.example.Entity;public class OrdersEntity {private int id;private String orderNumber;private Double orderPrice;//订单中有外键customer_id,所以需要添加顾客类private CustomerEntity customer;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getOrderNumber() {return orderNumber;}public void setOrderNumber(String orderNumber) {this.orderNumber = orderNumber;}public Double getOrderPrice() {return orderPrice;}public void setOrderPrice(Double orderPrice) {this.orderPrice = orderPrice;}public CustomerEntity getCustomer() {return customer;}public void setCustomer(CustomerEntity customer) {this.customer = customer;}
}
package org.example;import org.example.Entity.CustomerEntity;
import org.example.Entity.OrdersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;public class AppTest{@Testpublic void test(){//创建SessionFactory,从根路径下获取核心配置文件SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();//创建sessionSession session = factory.openSession();//创建事务Transaction transaction = session.beginTransaction();//创建顾客类对象并给相应属性赋值CustomerEntity customer = new CustomerEntity();customer.setId(4);customer.setName("44");customer.setAge(18);//创建订单类对象并给相应属性赋值OrdersEntity order = new OrdersEntity();order.setId(4);order.setOrderNumber("4");order.setOrderPrice(4.0);//建立关联关系,若不建立关联关系order表的数据为空order.setCustomer(customer);//保存,所有对象都要保存session.save(customer);session.save(order);//提交事务transaction.commit();//关闭SessionFactoryfactory.close();}
}

多对多关系通过一个中间表来维护。
实现思路:


package org.example.Entity;import java.util.Set;public class AccountsEntity {private int aid;private String aname;private Set courses;public int getAid() {return aid;}public void setAid(int aid) {this.aid = aid;}public String getAname() {return aname;}public void setAname(String aname) {this.aname = aname;}public Set getCourses() {return courses;}public void setCourses(Set courses) {this.courses = courses;}
}
package org.example.Entity;import java.util.Set;public class CoursesEntity {private int cid;private String cname;private Set accounts;public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Set getAccounts() {return accounts;}public void setAccounts(Set accounts) {this.accounts = accounts;}
}
package org.example.Entity;public class AccountCourseEntity {private int id;private Integer aid;private Integer cid;public int getId() {return id;}public void setId(int id) {this.id = id;}public Integer getAid() {return aid;}public void setAid(Integer aid) {this.aid = aid;}public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}
}
package org.example;import org.example.Entity.AccountsEntity;
import org.example.Entity.CoursesEntity;
import org.example.Entity.CustomerEntity;
import org.example.Entity.OrdersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import java.util.HashSet;
import java.util.Set;public class AppTest{@Testpublic void test2(){//创建SessionFactory,从根路径下获取核心配置文件SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();//创建sessionSession session = factory.openSession();//创建事务Transaction transaction = session.beginTransaction();//创建Courses类对象并给相应属性赋值CoursesEntity course = new CoursesEntity();course.setCname("JAVA");//创建Accounts类对象并给相应属性赋值AccountsEntity account = new AccountsEntity();account.setAname("张三");//建立关联关系Set courses = new HashSet<>();courses.add(course);account.setCourses(courses);//保存,所有对象都要保存session.save(course);session.save(account);//提交事务transaction.commit();//关闭SessionFactoryfactory.close();}
}

上一篇:我想要一些唯美的句子
下一篇:论语中关于友善的句子