hibernate基本映射
实体类---表
实体类中的普通属性---表字段
采用<class>标签映射成数据库表,通过<property>标签将普通属性映射成表字段
所谓普通属性指不包括自定义类、集合和数组等
注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名
实体类的设计原则:
* 实现一个默认的(即无参数的)构造方法(constructor)
* 提供一个标识属性(identifier property)(可选)
* 使用非final的类 (可选)
* 为持久化字段声明访问器(accessors)
主键生成策略:
uuid(32位字符)、native(数字自增)和assigned(手动分配)
实体类---表
实体类中的普通属性---表字段
采用<class>标签映射成数据库表,通过<property>标签将普通属性映射成表字段
所谓普通属性指不包括自定义类、集合和数组等
注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名
实体类的设计原则:
* 实现一个默认的(即无参数的)构造方法(constructor)
* 提供一个标识属性(identifier property)(可选)
* 使用非final的类 (可选)
* 为持久化字段声明访问器(accessors)
主键生成策略:
uuid(32位字符)、native(数字自增)和assigned(手动分配)
//xml.文件
//User1.hbm.xml
<hibernate-mapping package="com.bjsxt.hibernate"><!-- package指定包名 -->
<class name="User1" table="t_user1">
<!-- class可以接完整路径名或类名,table用于命名表名 -->
<id name="id" column="user_id" length="32">
<generator class="uuid"/>
</id><!-- unique设置唯一性 not-null保证不为空-->
<property name="name" unique="true" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<property name="createTime" column="create_time"/><!-- column设置列名 -->
<property name="expireTime" column="expire_time"/>
</class>
</hibernate-mapping>
//User2.hbm.xml
<hibernate-mapping package="com.bjsxt.hibernate">
<class name="User2" table="t_user2">
<id name="id" column="user_id">
<generator class="native"/>
</id>
<property name="name" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<property name="createTime" column="createtime"/>
<property name="expireTime" column="expiretime"/>
</class>
</hibernate-mapping>
//User3.hbm.xml
<hibernate-mapping package="com.bjsxt.hibernate">
<class name="User3" table="t_user3">
<id name="id" column="user_id" length="32">
<generator class="assigned"/>
</id>
<property name="name" unique="true" not-null="true" length="20"/>
<property name="password" not-null="true" length="10"/>
<property name="createTime" column="create_time"/>
<property name="expireTime" column="expire_time"/>
</class>
</hibernate-mapping>
//hibernate.cfg.xml
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
<mapping class="com.bjsxt.hibernate.Teacher"/> //annotation方法,不用再编写xml文件
// annotation class 配置
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.IdClass;
import javax.persistence.SequenceGenerator;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@javax.persistence.TableGenerator(
name="Teacher_GEN",
table="GENERATOR_TABLE",
pkColumnName = "pk_key",
valueColumnName = "pk_value",
pkColumnValue="Teacher",
allocationSize=1
)
@SequenceGenerator(name="teacherSEQ", sequenceName="teacherSEQ_DB")
@IdClass(TeacherPK.class)
public class Teacher {
//private TeacherPK pk;
private int id;
private String name;
private String title;
private String yourWifeName;
private Date birthDate;
private boolean good;
private Gender gender;
@Enumerated(EnumType.STRING)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
@Transient
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
}
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="Teacher_GEN")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Temporal(TemporalType.TIME)
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
/*@EmbeddedId
public TeacherPK getPk() {
return pk;
}
public void setPk(TeacherPK pk) {
this.pk = pk;
}*/
}
@SuppressWarnings("serial")
public class TeacherPK implements java.io.Serializable {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (o instanceof TeacherPK) {
TeacherPK pk = (TeacherPK) o;
if (this.id == pk.getId() && this.name.equals(pk.getName())) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import junit.framework.TestCase;
public class BaseMappingTest extends TestCase {
public void testSave1() { //uuid分配主键,主键为32位字符
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
User1 user = new User1();
user.setName("李四");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testSave2() { //native分配主键,数字自增
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
User2 user = new User2();
user.setName("张三1");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testSave3() { //assigned分配,主键要自动分配
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
User3 user = new User3();
user.setId("001");
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}