博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate映射组成关系
阅读量:7223 次
发布时间:2019-06-29

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

目录结构

 

package com.hibernate.helloworld;public class School {     private String name;     private String address;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public School(String name, String address) {        super();        this.name = name;        this.address = address;    }    public School() {        super();        // TODO Auto-generated constructor stub    }    @Override    public String toString() {        return "School [name=" + name + ", address=" + address + "]";    }         }package com.hibernate.helloworld;import java.util.Date;public class Student {    private Integer id;    private String name;    private Date birth;    private School school;            public Student() {        // TODO Auto-generated constructor stub    }    public Student(String name, Date birth) {        super();        this.name = name;        this.birth = birth;    }    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 Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }            public School getSchool() {        return school;    }    public void setSchool(School school) {        this.school = school;    }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + ", birth=" + birth + ", school=" + school + "]";    }        }

hibernate.cfg.xml

root
1
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
org.hibernate.dialect.MySQLInnoDBDialect
true
true
update

Student.hbm.xml

Test

package com.hibernate.helloworld;import java.sql.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.Test;public class test {    @Test    public void test() {                System.out.println("test...");                //1. 创建一个 SessionFactory 对象        SessionFactory sessionFactory = null;                //1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息        Configuration configuration = new Configuration().configure();                //4.0 之前这样创建//        sessionFactory = configuration.buildSessionFactory();                //2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象        //hibernate 的任何配置和服务都需要在该对象中注册后才能有效.        ServiceRegistry serviceRegistry =                 new ServiceRegistryBuilder().applySettings(configuration.getProperties())                                            .buildServiceRegistry();                //3).        sessionFactory = configuration.buildSessionFactory(serviceRegistry);                //2. 创建一个 Session 对象        Session session = sessionFactory.openSession();                //3. 开启事务        Transaction transaction = session.beginTransaction();                //4. 执行保存操作        Student student = new Student("刘备", new Date(new java.util.Date().getTime()));        School school = new School("AAA","BBB");        student.setSchool(school);        session.save(student);                //5. 提交事务         transaction.commit();                //6. 关闭 Session        session.close();                //7. 关闭 SessionFactory 对象        sessionFactory.close();    }    }

 

转载于:https://www.cnblogs.com/lusufei/p/7323932.html

你可能感兴趣的文章
司法部:做好春节期间在押罪犯的离监探亲工作
查看>>
研究:印度气候变暖速度加剧 2040年或面临重灾
查看>>
中俄蒙三国六方签订白鹤研究与保护合作备忘录
查看>>
补贴退坡幅度进一步加大 新能源汽车会涨价吗
查看>>
python爬虫——爬取豆瓣TOP250电影
查看>>
ES6数组的扩展----Array.from()和Array.of()
查看>>
当 Node.js 遇见 Docker
查看>>
C++与Rust操作裸指针的比较
查看>>
[译] 尤雨溪:Vue 3.0 计划
查看>>
Android酷炫实用的开源框架(UI框架)
查看>>
10分钟了解JS堆、栈以及事件循环的概念
查看>>
CSS的垂直居中和水平居中总结
查看>>
67 亿美金搞个图,创建知识图谱的成本有多高你知道吗?
查看>>
To be or not
查看>>
HTTP协议小结
查看>>
JS Boolean,Array,Object的基础知识
查看>>
HashMap 源码分析
查看>>
Java类集框架 —— HashMap源码分析
查看>>
【火炉炼AI】机器学习022-使用均值漂移聚类算法构建模型
查看>>
如何才能弥补实际工作经验不足,而获得一份好工作?
查看>>