02属性-字段映射类型

1 hibernate不能自动处理java.lang.Boolean

2 hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- 数据库连接信息 -->
		<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
		<property name="connection.username">scott</property>
		<property name="connection.password">tiger</property>
		<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
		
		<property name="dialect">org.hibernate.dialect.OracleDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		
		<mapping resource="model/Emp.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

3 Emp.hbm.xml   type中是hibernate定义的类型

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping>
	<class name="model.Emp" table="testempbydf">
		<!-- integer -->
		<id name="id" type="integer" column="id">
			<generator class="sequence">
				<param name="sequence">emp_seq</param>
			</generator>
		</id>
		
		<property name="name" type="string" column="name"></property>
		<property name="age" type="integer" column="age"></property>
		<property name="salary" type="double" column="salary"></property>
		
		<!-- 数据库中存y/n,不支持java.lang.Boolean -->
		<property name="marry" type="yes_no" column="marry"></property>
		<!-- 数据库中存t/f
		<property name="marry" type="true_false" column="marry"></property>
		-->
		<property name="birthday" type="date" column="birthday"></property>
		<property name="lastLoginTime" type="timestamp" column="last_login_time"></property>
	</class>
</hibernate-mapping>

4 Emp.java

package model;

import java.sql.Date;
import java.sql.Timestamp;

public class Emp {
	private Integer id;
	private String name;
	private Integer age;
	private Double salary;
	private Boolean marry;
	private Date birthday;
	private Timestamp lastLoginTime;

	// getters & setters
}

5 数据库

CREATE TABLE testempbydf (
  ID                 NUMBER(4) CONSTRAINT EMP_ID_PK PRIMARY KEY,
  NAME               VARCHAR(50)  NOT NULL,
  AGE	               NUMBER(11),
  SALARY	           NUMBER(7,2),
  MARRY 		         CHAR(1),
  BIRTHDAY 	         DATE,
  LAST_LOGIN_TIME	   TIMESTAMP
);

6 导包 hibernate,JDBC,JUnit

7 HibernateUtil.java

package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	
	static {
		Configuration conf = new Configuration();
		conf.configure("/hibernate.cfg.xml");
		sf = conf.buildSessionFactory();
	}
	
	public static Session getSession() {
		return sf.openSession();
	}
}

8 测试

package test;

import java.sql.Date;
import java.sql.Timestamp;

import model.Emp;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

public class Test {
	@org.junit.Test
	public void testAdd() {
		Emp e = new Emp();
		e.setName("machao");
		e.setAge(21);
		e.setSalary(1700.0);
		e.setMarry(true);
		e.setBirthday(Date.valueOf("0178-03-03"));
		e.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
		
		Session session = HibernateUtil.getSession();
		Transaction ts = session.beginTransaction();
		try {
			session.save(e);
			ts.commit();
		} catch (Exception e2) {
			e2.printStackTrace();
			ts.rollback();
		} finally {
			session.close();
		}
	}
}


你可能感兴趣的:(02属性-字段映射类型)