欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hibernate的Annotation版Hello world实例

程序员文章站 2024-03-11 09:35:19
本文实例讲述了hibernate的annotation版hello world实现方法。分享给大家供大家参考,具体如下: 需要引入的包:hibernate-commons...

本文实例讲述了hibernate的annotation版hello world实现方法。分享给大家供大家参考,具体如下:

需要引入的包:hibernate-commons-annotations-4.0.4.final.jar
由于我使用的是:hibernate-release-4.3.5.final,在required目录下已经有了。

bean:

import javax.persistence.column;
import javax.persistence.entity;
import javax.persistence.id;
import javax.persistence.table;
@entity
@table(name="teacher")
public class teacher {
  private int id;
  private string name;
  private string title;
  @id
  public int getid() {
    return id;
  }
  public void setid(int id) {
    this.id = id;
  }
  @column(name="name")
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  @column(name="title")
  public string gettitle() {
    return title;
  }
  public void settitle(string title) {
    this.title = title;
  }
}

对应的hibernate.cfg.xml文件:

<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-configuration public
"-//hibernate/hibernate configuration dtd 3.0//en"
"http://www.hibernate.org/dtd/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/hibernate</property>
    <property name="connection.username"></property>
    <property name="connection.password"></property>
    <!-- jdbc connection pool (use the built-in) -->
    <!--
    <property name="connection.pool_size">1</property>
     -->
    <!-- sql dialect -->
    <property name="dialect">org.hibernate.dialect.mysqldialect</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.internal.nocacheprovider</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">update</property>
    -->
    <mapping resource="com/hibernate/model/student.hbm.xml"/>
    <mapping class="com.hibernate.model.teacher"/>
  </session-factory>
</hibernate-configuration>

测试类:

import org.hibernate.session;
import org.hibernate.sessionfactory;
import org.hibernate.cfg.annotationconfiguration;
import org.hibernate.cfg.configuration;
import com.huxing.hibernate.model.student;
import com.huxing.hibernate.model.teacher;
public class studenttest {
  public static void main(string[] args) {
    student a = new student();
    a.setid(123);
    a.setage(32);
    a.setname("hello hibernate!");
    teacher tea = new teacher();
    tea.setid(4);
    tea.setname("mysql");
    tea.settitle("high");
    configuration cfg = new annotationconfiguration();
    sessionfactory cf = cfg.configure().buildsessionfactory();
    session session = cf.opensession();
    session.begintransaction();
    session.save(tea);
    session.gettransaction().commit();
    session.close();
    cf.close();
  }
}

注意:代码省略了包路径。

其他方面:

1.注解可以加在属性上,也可以加在get方法上。
2.注解的mapping和xml配置的xml的不同!一个是resource,一个是class。

希望本文所述对大家hibernate框架程序设计有所帮助。