81,092
社区成员
发帖
与我相关
我的任务
分享
package test;
import wind.rose.crm.entity.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import wind.rose.crm.customer.service.CustomerService;
import wind.rose.crm.customer.service.OrderService;
import wind.rose.crm.customer.service.ProductService;
import wind.rose.crm.customer.service.UserService;
import wind.rose.crm.entity.Customer;
import wind.rose.crm.entity.User;
import java.util.*;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
OrderService orderService = (OrderService) ac.getBean("orderService");
Product p = orderService.findByProductName("pg");
Order order = new Order();
Customer c = orderService.findCustomerByName("苹果");
order.setCustomer(c);
p.getOrders().add(order);
orderService.updateProduct(p);
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="wind.rose.crm.entity">
<class name="Order" table="crm_order">
<id column="id" name="id" type="long">
<generator class="native"></generator>
</id>
<property name="order_total_price" column="order_total_price" type="double"></property>
<property name="order_status" column="order_status" type="string"></property>
<property name="send_goods_addr" column="send_goods_addr" type="string"></property>
<property name="phone_no" column="phone_no" type="string"></property>
<property name="payment_type" column="payment_type" type="string"></property>
<property name="order_note" column="order_note" type="string"></property>
<many-to-one name="customer" class="Customer" column="customer_id" unique="true"></many-to-one>
<set name="products" cascade="save-update" inverse="true" table="order_product" lazy="true">
<key column="order_id"></key>
<many-to-many column="product_id" class="Product"></many-to-many>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="wind.rose.crm.entity">
<class name="Product" table="crm_product">
<id column="id" name="id" type="long">
<generator class="native"></generator>
</id>
<property name="product_name" column="product_name" type="string"></property>
<property name="product_type" column="product_type" type="string"></property>
<property name="product_base_price" column="product_base_price" type="double"></property>
<property name="product_status" column="product_status" type="string"></property>
<property name="product_note" column="product_note" type="string"></property>
<set name="orders" table="order_product" cascade="save-update" fetch="join" lazy="false">
<key column="product_id"></key>
<many-to-many column="order_id" class="Order"></many-to-many>
</set>
</class>
</hibernate-mapping>