首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

应用Hibernate例子annotations

2012-07-20 
使用Hibernate例子annotations下载hibernate-release-4.1.3.Final.tgz例子在这里:hibernate-release-4.1.3

使用Hibernate例子annotations

下载hibernate-release-4.1.3.Final.tgz

例子在这里:

hibernate-release-4.1.3.Final\documentation\quickstart\en-US\html\files\hibernate-tutorials.zip

这里说关于annotations的事。


============配置log4j修改hibernate-tutorials/pom.xml增加如下代码? ? <dependency>? ? ? ? <groupId>org.slf4j</groupId>? ? ? ? <artifactId>slf4j-log4j12</artifactId>? ? ? ? <version>1.6.1</version>? ? </dependency>这可以让maven使用log4j-1.2.16.jar

复制hibernate-release-4.1.3.Final\project\etc\log4j.properties到

hibernate-tutorials\annotations\src\test\resources中,

这样,可以控制log4j,省得运行时输出一堆log。

?

log4j.properties中,把log4j.logger.org.hibernate=debug改成log4j.logger.org.hibernate=warn

?

log就会干干净净。

============修改hibernate.cfg.xml,符合自己的数据库信息? ? ? ? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>? ? ? ? <property name="connection.url">jdbc:mysql://ip地址:端口/数据库名字?useUnicode=true&amp;characterEncoding=UTF-8</property>

?

============增加一个map类

在hibernate.cfg.xml中加:

<mapping name="code"><?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://aa:2222/dd?useUnicode=true&amp;characterEncoding=UTF-8</property> <property name="connection.username">ff</property> <property name="connection.password">ff</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> <!-- 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> <!-- Names the annotated entity class --> <mapping name="code">package org.hibernate.tutorial.annotations;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "food_item")public class FoodItem2 { @Id @Column(name = "foodItem_Id") public long getFoodItemId() { return _foodItemId; } public void setFoodItemId(long foodItemId) { _foodItemId = foodItemId; } @Column(name = "name_str") public String getName() { return _name; } public void setName(String name) { _name = name; } @Column(name = "points_num") public int getPoints() { return _points; } public void setPoints(int points) { _points = points; } private long _foodItemId; private String _name; private int _points; public String toString() { return _foodItemId + ", " + _name + ", " + _points; }}?A2.java

package org.hibernate.tutorial.annotations;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class A2 {  private SessionFactory sessionFactory;  protected void setUp() throws Exception {    // A SessionFactory is set up once for an application    sessionFactory = new Configuration().configure().buildSessionFactory();  }  protected void tearDown() throws Exception {    if (sessionFactory != null) {      sessionFactory.close();    }  }  @SuppressWarnings({ "unchecked" })  public void testBasicUsage() {    Session session = sessionFactory.openSession();    session.setDefaultReadOnly(true);    session.beginTransaction();    List result = session.createQuery("from FoodItem2").list();    for (FoodItem2 event : (List<FoodItem2>) result) {      System.out.println(event);    }    session.getTransaction().commit();    session.close();  }  public static void main(String[] args) throws Exception {    A2 a1 = new A2();    a1.setUp();    a1.testBasicUsage();    a1.tearDown();  }}
?应用Hibernate例子annotations树叶,阳光

热点排行