`
xxp3369
  • 浏览: 152448 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

hibernate基本映射

阅读更多
hibernate基本映射

实体类---表
实体类中的普通属性---表字段

采用<class>标签映射成数据库表,通过<property>标签将普通属性映射成表字段
所谓普通属性指不包括自定义类、集合和数组等

注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名

实体类的设计原则:
* 实现一个默认的(即无参数的)构造方法(constructor)
* 提供一个标识属性(identifier property)(可选)
* 使用非final的类 (可选)
* 为持久化字段声明访问器(accessors)

主键生成策略:
uuid、native和assigned



package com.bjsxt.hibernate;

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

public class HibernateUtils {

	private static SessionFactory factory;
	
	static {
		try {
			Configuration cfg = new Configuration().configure();
			factory = cfg.buildSessionFactory();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	
	public static Session getSession() {
		return factory.openSession();
	}
	
	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
}



package com.bjsxt.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

	public static void main(String[] args) {
		
		//读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration().configure();
		
		SchemaExport export = new SchemaExport(cfg);
		
		export.create(true, true);
	}
}



package com.bjsxt.hibernate;

import java.util.Date;

public class User1 {
	
	private String id;
	
	private String name;
	
	private String password;
	
	private Date createTime;
	
	private Date expireTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}
}



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjsxt.hibernate">
	<class name="User1" table="t_user1">
		<id name="id" column="user_id" length="32">
			<generator class="uuid"/>
		</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>



package com.bjsxt.hibernate;

import java.util.Date;

public class User2 {
	
	private int id;
	
	private String name;
	
	private String password;
	
	private Date createTime;
	
	private Date expireTime;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<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" unique="true" 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>




package com.bjsxt.hibernate;

import java.util.Date;

public class User3 {
	
	private String id;
	
	private String name;
	
	private String password;
	
	private Date createTime;
	
	private Date expireTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}
}



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<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>



<!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="hibernate.connection.url">
		jdbc:mysql://localhost/hibernate_basemapping
	</property>
	<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">123</property>
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="myeclipse.connection.profile">
		hibernate_basemapping
	</property>
	<property name="connection.url">
		jdbc:mysql://&lt;hostname&gt;[&lt;:3306&gt;]/&lt;hibernate_basemapping&gt;
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">123</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>

	<mapping resource="com/bjsxt/hibernate/User1.hbm.xml" />
	<mapping resource="com/bjsxt/hibernate/User2.hbm.xml" />
	<mapping resource="com/bjsxt/hibernate/User3.hbm.xml" />
</session-factory>
</hibernate-configuration>



package com.bjsxt.hibernate;

import java.util.Date;

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

import junit.framework.TestCase;

public class BaseMappingTest extends TestCase {

	public void testSave1() {
		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() {
		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() {
		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);
		}
	}	
}
分享到:
评论

相关推荐

    Hibernate关联映射总结

    在探讨Hibernate中的关联映射之前,我们需要先理解几个基本概念,这将有助于我们更好地理解和应用这些关联映射。 - **主键**(Primary Key):主键是用来唯一标识一条记录的关键字段。例如,在一个用户表中,用户ID...

    [原]Hibernate集合映射

    例如,如果集合元素是基本类型,我们可以直接映射;如果是复杂类型的对象,就需要指定对应的实体类。 3. **一对一(One-to-One)映射** 在集合映射中,一对一关系通常表现为单个实体与集合的关系。例如,一个用户...

    Hibernate 注解映射

    一、 环境搭建和基本映射 在使用 Hibernate 注解映射之前,需要添加相关的 jar 包,包括 hibernate-annotations.jar、ejb3-persistence.jar 和 hibernate-commons-annotations.jar。 在实体类中,可以使用 JPA 的...

    hibernate array 数组映射

    在Java的持久化框架Hibernate中,数组映射是一种常见的数据模型转换方式,它允许我们将数据库中的数据以数组的形式存储在Java对象中。本篇将详细探讨`hibernate array 数组映射`的相关知识点,包括其原理、配置、...

    Hibernate实体映射

    综上所述,"Hibernate实体映射"的学习资源涵盖了从基本概念到实际操作的多个方面,对于想深入了解Hibernate ORM框架的开发者来说,无疑是一份宝贵的资料。通过实践这些实例代码,可以加深对Hibernate实体映射的理解...

    hibernate实体映射文件字段设置默认值

    首先,我们需要了解一个基本的Hibernate映射文件结构: ```xml &lt;hibernate-mapping&gt; &lt;!-- 映射类的属性 --&gt; &lt;/hibernate-mapping&gt; ``` 这里的`&lt;class&gt;`标签表示映射到特定的Java类,其属性`name`指定了对应的...

    Hibernate关系映射

    "多对一"关系映射是Hibernate支持的四种基本关联类型之一,包括一对一、一对多、多对一和多对多。本篇将深入探讨“多对一”关系映射的概念、配置以及在实际web系统中的应用。 **一、“多对一”关系映射概念** 在...

    JAVA数据类型与Hibernate的类型映射

    首先,Java的基本数据类型在Hibernate中有对应的类型映射。例如,int型在Hibernate中通常映射为Integer类型,因为数据库字段可能允许为空(NULL),而Java的基本类型不能是NULL。同样,boolean型在Hibernate中映射为...

    Hibernate映射配置实例大全(详细总结)

    综上所述,"Hibernate映射配置实例大全"涵盖的内容广泛,包括基本的配置文件设置、实体类的XML和注解映射、集合映射、加载策略、事务管理和缓存配置等。通过深入学习和实践这些实例,开发者可以熟练地运用Hibernate...

    Myeclipse生成Hibernate映射文件

    MyEclipse会自动生成一个基本的映射文件模板。 在生成的映射文件中,可以看到一些预定义的元素,如`&lt;class&gt;`、`&lt;id&gt;`、`&lt;property&gt;`等。`&lt;class&gt;`标签定义了映射的Java类,`&lt;id&gt;`标签用于标识主键,`&lt;property&gt;`...

    hibernate map 集合映射

    这篇文章将深入探讨Hibernate中的集合映射机制,包括其基本概念、类型以及如何在实际开发中进行配置。 ### 1. Hibernate集合映射的基本概念 集合映射是Hibernate中一个核心的概念,它允许我们将数据库表中的多对一...

    hibernate关系映射网址

    ##### 2.2 基本映射示例 以一个简单的用户(User)与地址(Address)为例,假设有一个用户表(users)和一个地址表(addresses),我们可以使用以下映射文件来建立它们之间的关系: ```xml ...

    Hibernate映射解析 七种映射关系

    以上是单向一对一关联映射的基本概念和实现方式。在实际开发中,根据业务需求,还有其他六种映射关系,包括双向一对一、一对多、多对一、多对多等,它们各自的实现方式和应用场景各有不同。理解并熟练掌握这些映射...

    Spring和Hibernate学习笔记

    第三课:hibernate基本映射 第四课:hibernate多对一关联映射 ...................... Spring: 第一课:构建Spring的环境并建立一个例子 第三课:spring Bean的作用域 第四课:spring对AOP的只是(采用Annotation的...

    java根据实体类生成Hibernate映射文件

    首先,我们需要了解Hibernate映射文件的基本结构。通常,一个映射文件以`.hbm.xml`为扩展名,它使用XML格式来描述Java类和数据库表的对应关系。映射文件包含了类名、表名、字段及其数据类型等信息。例如: ```xml ...

    Hibernate学习笔记特别详细

    006---Hibernate基本映射标签和属性介绍 - &lt;hibernate-mapping&gt;:定义映射文档的根元素。 - &lt;class&gt;:定义一个Java类到数据库表的映射。 - &lt;id&gt;:标识一个实体的唯一标识,对应数据库表的主键。 - &lt;property&gt;:映射...

    hibernate的映射文件配置

    描述:本文深入解析了Hibernate映射文件配置的核心概念,重点阐述了映射文件的基本结构及其组成部分,包括主键(id)、普通属性(property)等关键元素的配置方法和策略。 ### Hibernate映射文件配置详解 在...

    Hibernate 各种映射的配置文件总结

    - **Group.hbm.xml** 文件展示了`Group`实体的基本映射,包括主键的生成策略。 ```xml &lt;hibernate-mapping&gt; &lt;class name="com.dragon.hibernate.Group" table="t_group"&gt; &lt;/hibernate-mapping&gt; ``` - ...

Global site tag (gtag.js) - Google Analytics