hibernate_06_单表操作_组件属性
什么是组件属性?
比如address是students的其中一个属性,而address又有三个属性:邮编、电话和地址。address就是hibernate的组件属性。
首先建立Address类:
package com.imooc.hibernate; public class Address { private String postcode; //邮编
private String phone; //电话
private String address; //地址 public Address() {}; public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Address(String postcode, String phone, String address) {
// super();
this.postcode = postcode;
this.phone = phone;
this.address = address;
}
@Override
public String toString() {
return "Address [postcode=" + postcode + ", phone=" + phone + ", address=" + address + "]";
} }
然后建立Students类:
package com.imooc.hibernate; import java.sql.Blob;
import java.util.Date; public class Students { private int sid;
private String sname;
private String gender;
private Date birthday;
private Address address;
private Blob picture; public Blob getPicture() {
return picture;
} public void setPicture(Blob picture) {
this.picture = picture;
} public Students() {} public Students(int sid, String sname, String gender, Date birthday, Address address, Blob picture) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
this.picture = picture;
} @Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + ", picture=" + picture + "]";
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} public int getSid() {
return sid;
} public void setSid(int sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} }
建立Students.hbm.xml文件,注意Address组件的写法:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.imooc.hibernate.Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="native" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" />
</property>
<property name="picture" type="java.sql.Blob">
<column name="PICTURE" />
</property> <component name="address" class="com.imooc.hibernate.Address">
<property name="postcode" column="POSTCODE"/>
<property name="phone" column="PHONE"/>
<property name="address" column="ADDRESS"/>
</component>
</class>
</hibernate-mapping>
建立hibernate.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property> <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
建立测试类:
package com.icoom.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.Date; import org.hibernate.Hibernate;
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.After;
import org.junit.Before;
import org.junit.Test; import com.imooc.hibernate.Address;
import com.imooc.hibernate.Students; public class StudentsTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init() {
// 1.创建配置对象
Configuration config = new Configuration().configure();
// 2.创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
// 3.创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
// 会话对象
session = sessionFactory.openSession();
// 开启事务
transaction = session.beginTransaction();
}
@After
public void destory() {
transaction.commit(); //提交事务
session.close(); //关闭session
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSaveStudents() {
Students s = new Students();
s.setSid(1);
s.setSname("老张");
s.setGender("男");
s.setBirthday(new Date());
Address address = new Address("250000", "1310531xxxx", "山东济南");
s.setAddress(address);
session.save(s);//保存对象进入数据库
}
}
执行成功,查看数据库:
hibernate_06_单表操作_组件属性的更多相关文章
- hibernate课程 初探单表映射3-4 组件属性
本节内容: 1 简介组件属性 2 demo 1 简介组件属性: <component name = "address" class = "Address" ...
- hibernate_07_单表操作_增删改操作
首先,创建类对象 package com.imooc.hibernate; public class Address { private String postcode; //邮编 private S ...
- hibernate_05_单表操作_对象类型
本篇使用hibernate输出一个对象(图片) 先写一个java类 package com.imooc.hibernate; import java.sql.Blob; import java.uti ...
- hibernate学习笔记(6)组件属性以及单表操作
组件属性:实体类中的某个属性属于用户自定义类的对象: 作用:将两个实体类合并在一起组建成一个表 在hbm.xml文件中配置: 格式: <component name="取的名字&quo ...
- Hibernate单表操作
单一主键 assigned:由Java应用程序负责生成(即手工的赋值) native:由底层的数据库自动的生成标示符,如果是MySQL就是auto_increment,如果是Oracle就是seque ...
- Django学习笔记(7)——单表操作和多表操作
单表操作 1,创建模型 创建名为book的APP,在book下的models.py中创建模型: from django.db import models # Create your models he ...
- Django模型层:单表操作
一 ORM简介 查询数据层次图解:如果操作mysql,ORM是在pymysq之上又进行了一层封装
- (三)hibernate单表操作
0. 贴上节hbm文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hib ...
- Django 模型层之单表操作
一.单表操作之创建表 在app的models.py文件中创建模型: from django.db import models class Book(models.Model): id = models ...
随机推荐
- webpack-dev-server和webpack
指导小伙伴在webstorm+nodejs环境下新建项目时,小伙伴出现了一个很神奇的问题:没有执行webpack-dev-server情况下,即使执行npm init,也不会出现package.jso ...
- Linux 实用指令(4)
目录 实用指令 1.指定运行级别 2.切换到指定运行级别的指令 3.帮助指令 3.1man获得帮助信息 3.2help指令 4.文件目录类 4.1pwd指令 4.2 ls指令 4.3 cd指令 4.4 ...
- Leetcode 48.旋转矩阵
旋转矩阵 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...
- 清北学堂模拟赛d3t2 b
分析:一道比较让人头疼的数学题. 先考虑怎么让分出来的三角形相似,先不考虑每个三角形的具体边长,设每个三角形的周长为li,则可知必然有一个数g = gcd{li},每一个三角形的周长都是g的倍数,这样 ...
- ant生成war包的简单实现
按网上的操作,实现一下. build.xml: <?xml version="1.0" ?> <project name ="antwebproject ...
- hdu_1020_Encoding_201310172120
Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- supervisord进程管理工具小结
前言 昨天临近下班,发现业务队列处理不及时,正好想到不久之前,上了一个新功能:通过队列异步转发微信消息.可能是消息太多了处理不过来,没怎么多想,处理不过来了,多增加处理进程就可以了,后来发现自己so ...
- SQLiteOpenHelper/SQLiteDatabase/Cursor源代码解析
转载请注明出处:http://blog.csdn.net/y_zhiwen/article/details/51583188 Github地址.欢迎star和follow 新增android sqli ...
- 《JAVA程序设计》实训第一天——《猜猜看》游戏
课程实训是做一个猜猜看的小游戏.对于对代码不熟悉不了解的我来说的确挺难的.可是我还是会努力的. import java.awt.EventQueue; import javax.swing.JFram ...
- 游戏人生(一),我的lua之旅:那些坑爹的CCBReaderLoad
首先,我们说说这个CCBReaderLoad. 这个脚本是cocos2dx自带的一个lua+cocosbuilder 的工具,详细功能呐,往下看. 先来看下我遇到的一个问题: ----美工给了我一个. ...