Hibernate ORM框架——第二章:Hibernate关联映射
1、hibernate.cfg.xml除了映射的路径变,其余不变;
2、类
package entity; public class Address { private String shengfen ; private String city ; public String getShengfen() { return shengfen; } public void setShengfen(String shengfen) { this.shengfen = shengfen; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
package entity; import java.util.Date; public class Person { private int pno ; private String pname ; private Date birthDay ; private Boolean gendar ; private java.math.BigDecimal salary ; private Address addr ; public int getPno() { return pno; } public void setPno(int pno) { this.pno = pno; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } public Boolean getGendar() { return gendar; } public void setGendar(Boolean gendar) { this.gendar = gendar; } public java.math.BigDecimal getSalary() { return salary; } public void setSalary(java.math.BigDecimal salary) { this.salary = salary; } public Address getAddr() { return addr; } public void setAddr(Address addr) { this.addr = addr; }
}
3、类的映射文件:本章重点
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="Person" table="persons"> <id name="pno" column="id"> <generator class="native"></generator> </id> <property name="pname" type="string" length="20" not-null="true"></property> <property name="birthDay" type="date"></property><!-- type="date" --> <property name="gendar" type="true_false"></property> <property name="salary" type="big_decimal" precision="10" scale="3"></property>
<!-- 关联表的写法 --> <component name="addr" class="Address"> <property name="shengfen" type="string"></property> <property name="city" type="string"></property> </component> </class> </hibernate-mapping>
(1)pname--varchar2类型--可以设置以下属性:
type="string" length="20"(设置长度) not-null="true"(是否为空)
(2)salary--java.math.BigDecimal类型(可以显示精度)--可以设置以下属性:
type="big_decimal" precision="10"(保留总位数,包括小数点) scale="3"(保留小数点)
具体的type=""的值,请参照“hibernate对应的sql类型和java类型图"(文章最前面的图),其中hibernate映射类型是作为type属性的值的。
4、util包中的HibernateUtil和以前一样不变
5、test测试包:Main.java
package test; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import entity.Address; import entity.Person; import util.HibernateUtil; public class Main { public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); Person ps = new Person(); ps.setPname("zss"); /*ps.setBirthDay(new Date());*/ //默认当前时间 //yyyy-MM-dd HH:mm:ss //格式化时间 SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); try { //提示输入时间 Scanner sc = new Scanner(System.in); System.out.println("请输入日期(格式为yyyy-MM-dd):"); String day = sc.next(); Date date = formatDate.parse(day); //Date date = formatDate.parse("1998-06-30");//输入时间:写死 ps.setBirthDay(date); sc.close(); } catch (ParseException e) { e.printStackTrace(); } ps.setGendar(true); BigDecimal a = new BigDecimal(1200); ps.setSalary(a); Address addr = new Address(); addr.setShengfen("广东"); addr.setCity("珠海"); ps.setAddr(addr); s.save(ps); tx.commit(); s.close(); } }
/*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/