美文网首页
JPA学习笔记六 单向一对多

JPA学习笔记六 单向一对多

作者: 殷俊杰 | 来源:发表于2018-03-15 18:47 被阅读0次

    表结构

    CREATE TABLE `t_category` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
    
    CREATE TABLE `t_product` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(20) DEFAULT NULL,
      `typeid` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `FK_7kn4mhej5ccxp2ei4u2he6b8n` (`typeid`),
      CONSTRAINT `FK_7kn4mhej5ccxp2ei4u2he6b8n` FOREIGN KEY (`typeid`) REFERENCES `t_category` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
    

    实体类

    package com.yjj.entity;
    
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 单向一对多
     * t_category表字段,id,name
     */
    @Table(name = "t_category")
    @Entity
    public class Category {
        @Id
        @GeneratedValue
        private Integer id;
        private String name;
        @JoinColumn(name = "typeId")
        @OneToMany
        private List<Product> products=new ArrayList<>();
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Product> getProducts() {
            return products;
        }
    
        public void setProducts(List<Product> products) {
            this.products = products;
        }
    
        @Override
        public String toString() {
            return "Category{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", products=" + products +
                    '}';
        }
    }
    
    
    
    package com.yjj.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    /**
     * 单向一对多
     * t_product表字段,id,name,typeId
     */
    @Table(name = "t_product")
    @Entity
    public class Product {
        @Id
        @GeneratedValue
        private Integer id;
        private String name;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
    
        @Override
        public String toString() {
            return "Product{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    
    
           Product product=new Product();
            product.setName("手机");
            Product product1=new Product();
            product1.setName("电脑");
            Category category=new Category();
            category.setName("数码");
            category.getProducts().add(product);
            category.getProducts().add(product1);
    
            manager.persist(product);
            manager.persist(product1);
            manager.persist(category);
    

    输出

    Hibernate: 
        insert 
        into
            t_product
            (name) 
        values
            (?)
    Hibernate: 
        insert 
        into
            t_product
            (name) 
        values
            (?)
    Hibernate: 
        insert 
        into
            t_category
            (name) 
        values
            (?)
    Hibernate: 
        update
            t_product 
        set
            typeId=? 
        where
            id=?
    Hibernate: 
        update
            t_product 
        set
            typeId=? 
        where
            id=?
    

    查询一的一方

            Category category=manager.find(Category.class,1);
            System.out.println(category);
    

    输出

      Hibernate: 
        select
            category0_.id as id1_0_0_,
            category0_.name as name2_0_0_ 
        from
            t_category category0_ 
        where
            category0_.id=?
    Hibernate: 
        select
            products0_.typeId as typeId3_0_0_,
            products0_.id as id1_2_0_,
            products0_.id as id1_2_1_,
            products0_.name as name2_2_1_ 
        from
            t_product products0_ 
        where
            products0_.typeId=?
    Category{id=1, name='数码', products=[Product{id=1, name='手机'}, Product{id=2, name='电脑'}]}
    
    

    先看看数据库吧


    image.png
    image.png

    删除

            Category category=manager.find(Category.class,1);
            manager.remove(category);
    

    看输出

    Hibernate: 
        select
            category0_.id as id1_0_0_,
            category0_.name as name2_0_0_ 
        from
            t_category category0_ 
        where
            category0_.id=?
    Hibernate: 
        update
            t_product 
        set
            typeId=null 
        where
            typeId=?
    Hibernate: 
        delete 
        from
            t_category 
        where
            id=?
    
    image.png

    删除Category后,Product并没有级联删除,而是把Product中typeid字段置为空,如果想要级联删除
    @OneToMany(cascade=CascadeType.REMOVE)

    相关文章

      网友评论

          本文标题:JPA学习笔记六 单向一对多

          本文链接:https://www.haomeiwen.com/subject/tnahqftx.html