博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java之hibernate之双向的多对一关联映射
阅读量:5038 次
发布时间:2019-06-12

本文共 4626 字,大约阅读时间需要 15 分钟。

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

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
books = new HashSet
(); 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
getBooks() { return books; } public void setBooks(Set
books) { this.books = books; }}

3.映射文件

Book.hbm.xml

Category.hbm.xml

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();    }}

 

转载于:https://www.cnblogs.com/Vincent-yuan/p/11198628.html

你可能感兴趣的文章
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>
聊天室(C++客户端+Pyhton服务器)_1.框架搭设
查看>>
UpdatePanel 内控件 更新“外的”控件【转】
查看>>
mybatis中>=和<=的实现方式
查看>>
Python面向对象03/继承
查看>>
java序列化和反序列化
查看>>
绝对定位
查看>>
flink源码编译(windows环境)
查看>>
dpkg 删除 百度网盘 程序
查看>>
服务器nginx安装
查看>>