在一个方添加
@oneToMany
list:@OrderBy("orderIndex desc"),
@OrderColumn(name = "orderIndex")
orderIndex是在多方的类中添加一个属性 为int类型,hibernate会帮助我们在数据库中自动排序
private int orderIndex;
/** * 在关系映射中一对多的情况和我们 平常在使用集合映射是一样 * List set Map来描述这种关系 * list @orderby * map @mapKeyColumn * @oneToMany 用于一方 * @joinColumn 通知多方去建立一个外键关联字段catalogid 来关联我的主键 * 在表中给你添加一个字段 存放就是顺序 1 2 3
*/
单向关联 操作一方 另外一方也会收到影响 增删查改的动作
一个种类有多种产品:
Catalog为一方
Product为多方
package com.oracle.bean; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; import java.util.List; import java.util.Set; /** * 产品的分类 */ //营养套餐 水果礼盒 箱装水果 国产水果 进口水果 干货食品 @Entity @Data @Table(name="TCatalog") public class Catalog { @Id @GeneratedValue(generator= "uuid") @GenericGenerator(name="uuid",strategy = "uuid") private String cataid; private String cataname; /** * 在关系映射中一对多的情况和我们 平常在使用集合映射是一样 * List set Map来描述这种关系 * list @orderby * map @mapKeyProperty * @oneToMany 用于一方 * @joinColumn 通知多方去建立一个外键关联字段catalogid 来关联我的主键 * 在表中给你添加一个字段 存放就是顺序 1 2 3 */
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name="catalogid") // @OrderBy("orderIndex desc") @OrderColumn(name = "orderIndex")
private List<Product> products;
}
package com.oracle.bean3; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; import java.math.BigDecimal; @Data @Entity @Table(name = "TProduct") public class Product { @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid",strategy = "uuid") private String pid; private String info; private String pname; private String productcode; private String artno; private String unit; private BigDecimal price; private BigDecimal saleprice; private int score; private BigDecimal discount; private BigDecimal pcount; private String remark; private int orderIndex; // private Set<Catalog> catalogs; }
package com.oracle.core; import com.oracle.bean3.Catalog; import com.oracle.bean3.Product; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class OnetoMany { private SessionFactory sessionFactory; private Session session; @Before public void begin(){ org.hibernate.cfg.Configuration cfg=new org.hibernate.cfg.Configuration(); cfg.configure(); sessionFactory = cfg.buildSessionFactory(); session = sessionFactory.openSession(); session.beginTransaction(); } /* @Test public void save(){ Catalog y=new Catalog(); y.setCataname("111"); Product p1=new Product(); p1.setPname("aaa"); p1.setPrice(new BigDecimal(111)); Product p2=new Product(); p2.setPname("bb"); p2.setPrice(new BigDecimal(222)); Set<Product> products=new HashSet<>(); products.add(p1); products.add(p2); y.setProducts(products); session.save(y); } */ //保存 @Test public void saveList(){ Catalog y=new Catalog(); y.setCataname("进口水果"); Product p1=new Product(); p1.setPname("香蕉"); p1.setPrice(new BigDecimal(111)); Product p2=new Product(); p2.setPname("苹果"); p2.setPrice(new BigDecimal(222)); Product p3=new Product(); p3.setPname("菠萝"); p3.setPrice(new BigDecimal(222)); List<Product> products=new ArrayList<Product>(); products.add(p1); products.add(p2); products.add(p3); y.setProducts(products); session.save(y); } //查询分类可以得到产品 @Test public void query(){ Catalog catalog = session.get(Catalog.class, "8a8aac94638783400163878345660000"); List<Product> products= catalog.getProducts(); for(Product product:products){ System.out.println(product); } } //保存2个分类,将礼盒分类和进口水果分类互换 @Test public void update(){ Catalog lihe = session.get(Catalog.class, "8a8aac946386ce27016386ce2a190000"); Catalog jinkou = session.get(Catalog.class, "8a8aac94638783400163878345660000"); List<Product> lihelist=lihe.getProducts(); List<Product> jinkoulist=jinkou.getProducts(); lihe.setProducts(jinkoulist); jinkou.setProducts(lihelist); session.update(lihe); session.update(jinkou); } //将所有的礼盒分类删除掉 将礼盒的商品合并到进口水果中去 @Test public void update2(){ Catalog lihe = session.get(Catalog.class, "8a8aac946386ce27016386ce2a190000"); Catalog shuiguo= session.get(Catalog.class, "8a8aac94638783400163878345660000"); shuiguo.getProducts().addAll(lihe.getProducts());//把查到的礼盒分类,添加到水果分类中 lihe.getProducts().clear();//把所有的产品都清除 session.update(lihe); session.update(shuiguo); session.delete(lihe);//删除 } //删除一个可以删除多个 @Test public void delete(){ Catalog catalog = session.get(Catalog.class, "8a8aac946386b3bc016386b3bfb30000"); session.delete(catalog); } @After public void end(){ session.getTransaction().commit(); session.close(); sessionFactory.close(); } }
在配置文件中映射
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:oracle:thin:@10.10.44.63:1521:XE</property> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="connection.username">XIA</property> <property name="connection.password">123456</property> <mapping class="com.oracle.bean3.Product"></mapping> <mapping class="com.oracle.bean3.Catalog"></mapping> </session-factory> </hibernate-configuration>