用xxx.hbm.xml映射文件配置的hibernate实例
2009-09-01 10:12
633 查看
一、实体类bean:News.java
package edu.hzu.entity;
import java.util.Date;
/**
* @hibernate.class table="t_news"
* @author laoyang
*/
public class News {
/**
* @hibernate.property
* @return
*/
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/**
* @hibernate.id generator-class="native"
* @return
*/
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.many-to-one class="edu.hzu.entity.NewsCategory" column="news_category_id"
* @return
*/
public NewsCategory getNewsCategory() {
return newsCategory;
}
public void setNewsCategory(NewsCategory newsCategory) {
this.newsCategory = newsCategory;
}
/**
* @hibernate.property
* @return
*/
public int getOrderIndex() {
return orderIndex;
}
public void setOrderIndex(int orderIndex) {
this.orderIndex = orderIndex;
}
/**
* @hibernate.property
* @return
*/
public Date getPublishdate() {
return publishdate;
}
public void setPublishdate(Date publishdate) {
this.publishdate = publishdate;
}
/**
* @hibernate.property
* @return
*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private Integer id;
private String title;
private Date publishdate;
private String content;
private int orderIndex;
private NewsCategory newsCategory;
}
2、hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="edu/hzu/entity/news.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、xxx.hbm.xml映射文件
<?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="edu.hzu.entity.News" table="t_news">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="content" type="java.lang.String" column="content"/>
<property name="title" type="java.lang.String" column="title"/>
<many-to-one name="newsCategory" class="edu.hzu.entity.NewsCategory" cascade="none" outer-join="auto" column="news_category_id"/>
<property name="orderIndex" type="int" column="orderIndex"/>
<property name="publishdate" type="java.util.Date" column="publishdate"/>
</class>
</hibernate-mapping>
4、测试类
package edu.hzu.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
configuration.buildSessionFactory().openSession();
}
}
运行这个测试类就可以得到对应的数据库及表(当然这只是写了一个例子,其实只是一个框架,要真运行,还要类似建一个NewsCategory类,与News相似)
package edu.hzu.entity;
import java.util.Date;
/**
* @hibernate.class table="t_news"
* @author laoyang
*/
public class News {
/**
* @hibernate.property
* @return
*/
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/**
* @hibernate.id generator-class="native"
* @return
*/
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.many-to-one class="edu.hzu.entity.NewsCategory" column="news_category_id"
* @return
*/
public NewsCategory getNewsCategory() {
return newsCategory;
}
public void setNewsCategory(NewsCategory newsCategory) {
this.newsCategory = newsCategory;
}
/**
* @hibernate.property
* @return
*/
public int getOrderIndex() {
return orderIndex;
}
public void setOrderIndex(int orderIndex) {
this.orderIndex = orderIndex;
}
/**
* @hibernate.property
* @return
*/
public Date getPublishdate() {
return publishdate;
}
public void setPublishdate(Date publishdate) {
this.publishdate = publishdate;
}
/**
* @hibernate.property
* @return
*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
private Integer id;
private String title;
private Date publishdate;
private String content;
private int orderIndex;
private NewsCategory newsCategory;
}
2、hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="edu/hzu/entity/news.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、xxx.hbm.xml映射文件
<?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="edu.hzu.entity.News" table="t_news">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="content" type="java.lang.String" column="content"/>
<property name="title" type="java.lang.String" column="title"/>
<many-to-one name="newsCategory" class="edu.hzu.entity.NewsCategory" cascade="none" outer-join="auto" column="news_category_id"/>
<property name="orderIndex" type="int" column="orderIndex"/>
<property name="publishdate" type="java.util.Date" column="publishdate"/>
</class>
</hibernate-mapping>
4、测试类
package edu.hzu.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class Test {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
configuration.buildSessionFactory().openSession();
}
}
运行这个测试类就可以得到对应的数据库及表(当然这只是写了一个例子,其实只是一个框架,要真运行,还要类似建一个NewsCategory类,与News相似)
相关文章推荐
- hibernate映射文件XXX.hbm.xml配置映射元素详解--对象标识符(OID)
- Hibernate --- xxx.hbm.xml映射配置文件详解
- hibernate映射配置文件(xxx.hbm.xml)内容及作用
- hibernate映射文件详解:xxx.hbm.xml文件详解
- Hibernate学习--映射文件xxx.hbm.xml详解
- 使用hibernate连接mysql数据库正向工程hibernate.cfg.xml文件和xxx.hbm.xml配置文件
- Hibernate-配置文件-映射文件.hbm.xml
- hibernate中的映射文件xxx.hbm.xml详解总结
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
- 使用hibernate连接mysql数据库正向工程hibernate.cfg.xml文件和xxx.hbm.xml配置文件
- hibernate中写配置文件xxx.hbm.xml时那个成员变量的类型type如何写的问题
- Hibernate框架中hibernate.properties属性文件,hibernate.cfg.xml配置文件,以及Users.hbm.xml映射文件的配置
- hibernate映射文件??.hbm.xml配置映射元素详解--Hibernate映射类型
- hibernate中写配置文件xxx.hbm.xml时那个成员变量的类型type如何写的问题
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)
- Hibernate的映射配置文件*.hbm.xml文件中class和property元素的各属性意义和设置
- Hibernate映射文件配置(hbm.xml和注解方式)
- Hibernate(2)映射文件Xxx-hbm.xml
- 使用Xdoclet生成Hibernate配置文件(hibernate.cfg.xml)和Hibernate映射文件(*.hbm.xml)
- Hibernate框架中在.hbm.xml映射文件中配置表与表之间的关联关系