package org.whkt.hibernate;import junit.framework.TestCase;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.whkt.entity.Author;
public class Test extends TestCase{ Session session = null;
protected void setUp()
{
try
{
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();

session = sessionFactory.openSession();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}

}

protected void tearDown()
{
try
{
session.close();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}


public void testInsertAuthorities() 
{

Transaction tx = null;
try 
{
tx = session.beginTransaction();

Author user = new Author();
user.setUsername("bingxue");
user.setPassword("iloveyou");

session.save(user);

session.flush();

tx.commit();

catch (Exception ex) 
{
System.out.println("insertAuthorities()  has error!" + ex.getMessage());
tx.rollback();


}


}上面是测试写的一个Test类

解决方案 »

  1.   

    /*
     * Created Tue Feb 21 00:10:34 CST 2006 by MyEclipse Hibernate Tool.
     */ 
    package org.whkt.entity;import java.io.Serializable;/**
     * A class that represents a row in the 'author' table. 
     * This class may be customized as it is never re-generated 
     * after being created.
     */
    public class Author
        extends AbstractAuthor
        implements Serializable
    {
        /**
         * Simple constructor of Author instances.
         */ public Author()
        {
        
        }    /* Add customized code below */}
    /*
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     *
     * Created Tue Feb 21 00:10:34 CST 2006 by MyEclipse Hibernate Tool.
     */
    package org.whkt.entity;import java.io.Serializable;/**
     * A class that represents a row in the author table. 
     * You can customize the behavior of this class by editing the class, {@link Author()}.
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
     * by MyEclipse Hibernate tool integration.
     */
    public abstract class AbstractAuthor 
        implements Serializable
    {    /** The value of the simple id property. */
        private java.lang.Long id;    /** The value of the simple username property. */
        private java.lang.String username;    /** The value of the simple password property. */
        private java.lang.String password;    /**
         * Simple constructor of AbstractAuthor instances.
         */
        public AbstractAuthor()
        {
        }    /**
         * Return the value of the id column.
         * @return java.lang.Long
         */
        public java.lang.Long getId()
        {
            return this.id;
        }    /**
         * Set the value of the id column.
         * @param id
         */
        public void setId(java.lang.Long id)
        {
            this.id = id;
        }    /**
         * Return the value of the username column.
         * @return java.lang.String
         */
        public java.lang.String getUsername()
        {
            return this.username;
        }    /**
         * Set the value of the username column.
         * @param username
         */
        public void setUsername(java.lang.String username)
        {
            this.username = username;
        }    /**
         * Return the value of the password column.
         * @return java.lang.String
         */
        public java.lang.String getPassword()
        {
            return this.password;
        }    /**
         * Set the value of the password column.
         * @param password
         */
        public void setPassword(java.lang.String password)
        {
            this.password = password;
        }
    }
    <?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" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <!-- Created Tue Feb 21 00:10:32 CST 2006                         -->
    <hibernate-mapping package="org.whkt.entity">    <class name="Author" table="author">  <id name="id" type="long">
          <generator class="identity"/>
        </id>
        
        <component name="author" class="org.whkt.entity.Author">
           
            <property name="username" column="username" type="string" />
            <property name="password" column="password" type="string" />
        </component>
        </class>
        
    </hibernate-mapping>
    上面是实体的映射关系和实体  可能比较多问题
      

  2.   

    <?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"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration><session-factory>
    <property name="myeclipse.connection.profile">conn1</property>
    <property name="connection.url">
    jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=whkt
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">sa</property>
    <property name="show_sql">true</property>
    <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
    </property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property><mapping resource="org/whkt/entity/Author.hbm.xml"></mapping>
    </session-factory></hibernate-configuration>上面是Hibernate的配置文件
      

  3.   

    org.whkt.entity.Author是个不符合规范的javabean
    你要对auther属性加上getAuther(),setAuther()方法
      

  4.   

    <component name="author" class="org.whkt.entity.Author">
    </component>这两句应该去掉吧?