顾客与顾客的多对多映射,以及顾客和订单,商户与订单的一对多映射

Posted acefeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顾客与顾客的多对多映射,以及顾客和订单,商户与订单的一对多映射相关的知识,希望对你有一定的参考价值。

customer.java

 
package Entity;

import java.util.HashSet;
import java.util.Set;

//import org.hibernate.mapping.Set;

public class Customer {//每个顾客可以对应多个商家,每个商家可以对应多个顾客
	private int cid;
	private String name;
	private Set<Merchant> merchant=new HashSet<Merchant>();
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Merchant> getMerchant() {
		return merchant;
	}
	public void setMerchant(Set<Merchant> merchant) {
		this.merchant = merchant;
	}
	public Customer(int cid, String name, Set<Merchant> merchant) {
		super();
		this.cid = cid;
		this.name = name;
		this.merchant = merchant;
	}
	public Customer(){}
	@Override
	public String toString() {
		return "cid=" + cid + ", name=" + name ;
	}
}

  Merchant.java

package Entity;

import java.util.HashSet;
import java.util.Set;

public class Merchant {//多个商户可对应多个顾客
  private int mid;
  
private String name;
  private Set<Customer> customer=new HashSet<Customer>();
public int getMid() {
	return mid;
}
public void setMid(int mid) {
	this.mid = mid;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Set<Customer> getCustomer() {
	return customer;
}
public void setCustomer(Set<Customer> customer) {
	this.customer = customer;
}
public Merchant(int mid, String name, Set<Customer> customer) {
	super();
	this.mid = mid;
	this.name = name;
	this.customer = customer;
}
public Merchant(){}
@Override
public String toString() {
	return "mid=" + mid + ", name=" + name +"]";
}
}

  order.java

package Entity;

import java.util.Date;

public class Order {//与顾客是多对一的关系
 private int id;
 private String name;
 private Customer customer;
 private Merchant merchant;
private String message;
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 Customer getCustomer() {
	return customer;
}
public void setCustomer(Customer customer) {
	this.customer = customer;
}
public Merchant getMerchant() {
	return merchant;
}
public void setMerchant(Merchant merchant) {
	this.merchant = merchant;
}
public String getMessage() {
	return message;
}
public void setMessage(String message) {
	this.message = message;
}
public Order(int id, String name, Customer customer, Merchant merchant, String message) {
	super();
	this.id = id;
	this.name = name;
	this.customer = customer;
	this.merchant = merchant;
	this.message = message;
}
public Order(){}
@Override
public String toString() {
	return "id=" + id + ", name=" + name+ ", message="
			+ message ;
}

}

  

切记:toString方法中不要返回customer和merchant,上面的也是

映射文件:

customer:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Entity.Customer" table="t_customer">
    <id name="cid" column="cid"  type="int">
      	<generator class="increment"/>
    </id>
    <property name="name" column="name" type="java.lang.String"  />
   <set name="merchant" table="c_m" cascade="all">
    <key column="c_id"></key>
    <many-to-many class="Entity.Merchant" column="m_id"></many-to-many>
    </set>
  </class>
</hibernate-mapping>

  merchant的映射 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Entity.Merchant" table="t_merchant">
    <id name="mid" column="mid"  type="int">
      	<generator class="increment"/>
    </id>
    <property name="name" column="name" type="java.lang.String"  />
    <set name="customer" table="c_m" inverse="true"><!-- 顾客方决定 -->
    <key column="m_id"></key>
    <many-to-many class="Entity.Customer" column="c_id"></many-to-many>
    </set>
  </class>

</hibernate-mapping>

  order的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Entity.Order" table="t_order">
    <id name="id" column="id"  type="int">
      	<generator class="increment"/>
    </id>
    <property name="name" column="name" type="java.lang.String"  />
   
        <property name="message" column="message" type="java.lang.String"  />
        <many-to-one name="customer" column="cid" class="Entity.Customer" cascade="all"></many-to-one>
        <many-to-one name="merchant" column="mid" class="Entity.Merchant" cascade="all"></many-to-one>
        
  </class>
</hibernate-mapping>

  config配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd 

">
<hibernate-configuration>
<session-factory>   
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    
    <!--      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
     <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <property name="show_sql">true</property>
<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->
    <property name = "hibernate.current_session_context_class">thread</property>
<!--   <mapping resource="text/user.hbm.xml"/> -->
    <mapping resource="Entity/customer.hbm.xml"/>
      <mapping resource="Entity/merchant.hbm.xml"/>
            <mapping resource="Entity/order.hbm.xml"/>
</session-factory>

</hibernate-configuration>

  自己测试的

package Usertest;


import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import Entity.Customer;
import Entity.Merchant;
import Entity.Order;

import org.junit.Test;
//顾客方决定的


public class C_M_tset {

	@Test
	public void save(){
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.getCurrentSession();
			Transaction ts = session.beginTransaction();
			try {
			Customer customer1=new Customer();
			Customer customer2=new Customer();
		    Merchant merchant1=new Merchant();
			Merchant merchant2=new Merchant();
			Order order1=new Order();
			customer1.setName("customer3");
			merchant1.setName("merchant3");
			customer2.setName("customer4");
			merchant2.setName("merchant4");
			order1.setCustomer(customer1);
			order1.setName("order2");
			order1.setMessage("It is for customer3");
			order1.setMerchant(merchant1);
			customer1.getMerchant().add(merchant1);
			customer1.getMerchant().add(merchant2);
		    customer2.getMerchant().add(merchant1);
		   
			session.save(customer1);
            session.save(customer2);
            session.save(order1);
            System.out.println(order1);
			ts.commit();	
			
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(ts != null)
			{
				ts.rollback();
			}
			e.printStackTrace();
		}
	}

@Test
public void save2(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
		try {
		Customer customer1=new Customer();
		customer1.setName("Amy");
		Merchant merchant1=new Merchant();
		merchant1.setName("Tom");
		customer1.getMerchant().add(merchant1);
		session.save(merchant1);
		session.save(customer1);
		Order order1=new Order();
		order1.setCustomer(customer1);
		order1.setMerchant(merchant1);
		order1.setName("oh");
		order1.setMessage("about Tom AND Amy");
		session.save(order1);
		ts.commit();	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}
@Test
public void save3(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
		try {
		Customer customer1=new Customer();
		Customer customer2=new Customer();
	    Merchant merchant1=new Merchant();
		Merchant merchant2=new Merchant();
		Order order1=new Order();
		customer1.setName("customer3");
		merchant1.setName("merchant3");
		customer2.setName("customer4");
		merchant2.setName("merchant4");
		order1.setCustomer(customer1);
		order1.setName("order2");
		order1.setMessage("It is for customer3");
		order1.setMerchant(merchant1);
		//顾客1同时购买商家2和商家1的东西,顾客3同时购买商户3和商户4的东西
		customer1.getMerchant().add(merchant1);
		customer1.getMerchant().add(merchant2);
//		//顾客2只购买了商家1//顾客4购买商户3
	    customer2.getMerchant().add(merchant1);
	   
		session.save(customer1);
        session.save(customer2);
        session.save(order1);
        System.out.println(order1);
		ts.commit();	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}
@Test
public void query(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
		try {
		Order order=session.get(Order.class, 1);
		System.out.println(order);
		System.out.println("顾客"+order.getCustomer().getName()+
		"买的商户"+order.getMerchant().getName()+"的商品,订单号为"+order.getId()+
		"订单信息为"+order.getMessage());
		Customer customer=session.get(Customer.class, 1);
		System.out.println("顾客"+customer.getName());
		System.out.println("买了商家"+customer.getMerchant());
		ts.commit();	
	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}

@Test
public void update(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
		try {
			
	     Customer customer=session.get(Customer.class, 6);
	     customer.setName("baxueyu");
		ts.commit();	
	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}
@Test
public void queryCustomerbymerchantId(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
		try {
	   Query query=session.createQuery("select c from Customer c join c.merchant m where m.mid=1");
	   List customer = query.list();
	   for(int i=0;i<customer.size();i++)
	   {
	   	Customer customers =(Customer)customer.get(i);
	   		System.out.println(i+"--"+customers);
	   }

		ts.commit();	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}
@Test
public void queryCustomerbyOrderId(){
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction ts = session.beginTransaction();
//		int n;
//		Scanner sc=new Scanner(System.in);
//		n=sc.nextInt();
		try {
	   Query query=session.createQuery("select customer from Order o where  o.id=1" );
	   System.out.println(query);
	   List customer = query.list();   
	   for(int i=0;i<customer.size();i++)
	   {
	   	Customer customers =(Customer)customer.get(i);
	   		System.out.println(i+"--"+customers);
	   }

		ts.commit();	
		
	} catch (HibernateException e) {
		// TODO Auto-generated catch block
		if(ts != null)
		{
			ts.rollback();
		}
		e.printStackTrace();
	}
}

  

以上是关于顾客与顾客的多对多映射,以及顾客和订单,商户与订单的一对多映射的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode:Database 114.按月统计订单数与顾客数

LeetCode(数据库)- 按月统计订单数与顾客数

图书预定系统

数据库中五个表要关联起来怎么写sql语句?