用xxx.hbm.xml映射文件配置的hibernate实例

一、实体类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相似)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值