[Hibernate单表操作] 组件属性
程序员文章站
2022-07-12 18:09:14
...
一 组件属性定义
实体类中某个属性属于用于自定义的类的对象。
二 组件属性应用
1、Address
public class Address {
private String postcode; //邮编
private String photo; //电话
private String address; //地址
public Address()
{
}
public Address(String postcode, String photo, String address) {
this.postcode = postcode;
this.photo = photo;
this.address = address;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2、Students
import java.sql.Blob;
import java.util.Date;
//学生类
public class Students {
/*
* JavaBeans的四点原则
* 1.必须是公有的类
* 2.提供公有的不带参数的默认的构造方法
* 3.属性私有
* 4.属性setter/getter封装
*/
private int sid; // 学号
private String sname; // 姓名
private String gender; // 性别
private Date birthday; // 出生日期
//private String address; // 地址
private Blob picture;//照片
private Address address; // 地址
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
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,
String address) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
//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;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
3、Students.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated 2017-9-17 10:33:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="native" />
<!-- <generator class="assigned" />-->
</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="timestamp">
<column name="BIRTHDAY" />
</property>
<!--
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
-->
<property name="picture" type="java.sql.Blob">
<column name="PICTURE" />
</property>
<component name="address" class="Address">
<property name="postcode" column="POSTCODE"></property>
<property name="photo" column="PHOTO"></property>
<property name="address" column="ADDRESS"></property>
</component>
</class>
</hibernate-mapping>
4、测试方法
@Test
public void testSaveStudents(){
//Students s= new Students(1,"张三丰","男",new Date(),"武当山");
Students s= new Students();
s.setSname("张三丰");
s.setGender("男");
s.setBirthday(new Date());
//s.setAddress("武当山");
Address address = new Address("7232","125674323","湖北");
s.setAddress(address);
session.save(s);
}
5、测试结果
下一篇: [Hibernate单表操作] 对象类型
推荐阅读
-
SQL Server 存储过程遇到“表 ''#TT'' 没有标识属性无法执行 SET 操作”错误
-
Hibernate单表操作实例解析
-
MySQL单表查询操作实例详解【语法、约束、分组、聚合、过滤、排序等】
-
Django-模型层(单表操作)
-
05.Django基础五之django模型层(一)单表操作
-
Django 模型层之单表操作
-
数据库(SQL Server )经典例题(二):对S表、P表、J表、SPJ表的操作——单表查询
-
SQL基础语法的单表操作 select|insert|update|delete(增删改查) 简单使用
-
Django -- 单表操作
-
postgresql 导入数据库表并重设自增属性的操作