• java之hibernate之双向的多对一关联映射


    这篇讲解 双向的多对一关联映射

    1.表结构和多对一时,一致

    2.类结构

    Book.java

    public class Book implements Serializable{
        private int id;
        private String name;
        private String author;
        private double price;
        private Date pubDate;
        private Category category;
        public Book() {
        }
        public Book(String name, String author, double price, Date pubDate) {
            super();
            this.name = name;
            this.author = author;
            this.price = price;
            this.pubDate = pubDate;
        }
    
        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 String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public Date getPubDate() {
            return pubDate;
        }
        public void setPubDate(Date pubDate) {
            this.pubDate = pubDate;
        }
        public Category getCategory() {
            return category;
        }
        public void setCategory(Category category) {
            this.category = category;
        }
    }

    Category.java

    public class Category implements Serializable{
        private int id;
        private String name;
        private Set<Book> books = new HashSet<Book>();
        public Category() {
        }
        
        public Category(String name) {
            super();
            this.name = name;
        }
    
        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 Set<Book> getBooks() {
            return books;
        }
    
        public void setBooks(Set<Book> books) {
            this.books = books;
        }
    }

    3.映射文件

    Book.hbm.xml

    <hibernate-mapping package="cn.sxt.pojo">
        <class name="Book" table="t_book">
            <id name="id">
                <generator class="native"></generator>
            </id>
            <property name="name"/>
            <property name="author"/>
            <property name="price"/>
            <property name="pubDate"/>
            <!-- 多对一的关联关系设置   column指定外键的名称 -->
            <many-to-one name="category" column="cid"/>
        </class>
    </hibernate-mapping>

    Category.hbm.xml

    <hibernate-mapping package="cn.sxt.pojo">
        <class name="Category" table="t_category">
            <id name="id">
                <generator class="native"></generator>
            </id>
            <property name="name"/>
            <!-- 双向一对多的设置 -->
            <set name="books" inverse="true">
                <!-- 设置外键 -->
                <key column="cid"></key>
                <!-- 设置多的一端的类型 -->
                <one-to-many class="Book"/>
            </set>
        </class>
    </hibernate-mapping>

    4.测试

    public class HibernateTest {
        /**
         * 生成数据库表的工具方法
         * */
        @Test
        public void testCreateDB(){
            Configuration cfg = new Configuration().configure();
            SchemaExport se = new SchemaExport(cfg);
            //第一个参数  是否打印sql脚本
            //第二个参数 是否将脚本导出到数据库中执行
            se.create(true, true);
        }
        /**
         * 初始化表数据
         */
        @Test
        public void testInit(){
            Session session = null;
            Transaction tx = null;
            try {
                session = HibernateUtil.getSession();
                tx = session.beginTransaction();
                Category c1 = new Category("计算机类");
                Category c2 = new Category("文学");
                Category c3 = new Category("历史");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Book b1 = new Book("java","sun",30,df.parse("1995-05-23"));
                b1.setCategory(c1);
                Book b2 = new Book("struts","apache",40,df.parse("2006-09-12"));
                b2.setCategory(c1);
                Book b3 = new Book("明朝那些事儿","当年明月",70,df.parse("2008-05-23"));
                b3.setCategory(c3);
                Book b4 = new Book("水浒传","老撕",20,df.parse("1985-05-23"));
                b4.setCategory(c2);
                session.save(c1);
                session.save(c2);
                session.save(c3);
                session.save(b1);
                session.save(b2);
                session.save(b3);
                session.save(b4);
                tx.commit();
                
            } catch (Exception e) {
                if(tx!=null)
                    tx.rollback();
            }finally {
                HibernateUtil.close();
            }
        }
        /**
         * 在查询多的一端数据时可以获取一的一端的数据
         */
        @Test
        public void testGetData(){
            Session session = HibernateUtil.getSession();
            Book book = (Book)session.get(Book.class, 3);
            System.out.println(book.getId()+"--"+book.getName()+"---"+book.getAuthor()+
                    "---"+book.getPrice()+"---"+book.getPubDate()+"---"+book.getCategory().getName());
            System.out.println("=============================");
            Category c1 = (Category)session.get(Category.class, 1);
            System.out.println(c1.getId()+"-----"+c1.getName());
            for(Book b:c1.getBooks()){
                System.out.println(b);
            }
            HibernateUtil.close();
        }
    }
  • 相关阅读:
    VC连接数据库方式
    vc中有关数据类型的转换
    获得MFC窗口指针方法总结
    Windows 中绘图以及Windows 的图形设备接口(GDI )
    C#访问远程共享加锁文件夹
    C#操作注册表全攻略
    学习asp.net比较完整的流程
    sogou rank查询接口
    jquery星级插件、支持页面中多次使用
    防止网页被嵌入框架的js代码
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/11198628.html
Copyright © 2020-2023  润新知