日期:2014-05-20  浏览次数:20726 次

Hibernate中数据库问题?
测试类代码:

public class TestTeacher {
  private static SessionFactory sf=null;
 @BeforeClass
 public static void factory(){
  sf=new AnnotationConfiguration().configure().buildSessionFactory();
 }
 @AfterClass
 public static void closesf(){
  sf.close();
 }

 @Test
     public void testget(){
      Session session=sf.getCurrentSession();
      session.beginTransaction();
      Teacher teacher=(Teacher)session.get(Teacher.class, 1);
          System.out.println(teacher.getAge());
      session.getTransaction().commit();
     }
}

Hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='utf-8'?>
<!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/student?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1282236828</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>
         
         <property name="current_session_context_class">thread</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.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property> 
       <mapping class="org.coreapi.demo.Teacher"/>
    </session-factory>

</hibernate-configuration>

类:teacher代码:

@Entity
public class Teacher {
     private int id;
     private String name;
     private String age;
     @Id
     @GeneratedValue
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }
     
}

------解决方案--------------------
具体什么问题。。。。