stdout.log
信息如下:
22:26:05,640  INFO Environment:462 - Hibernate 2.1.2
22:26:05,734  INFO Environment:491 - hibernate.properties not found
22:26:05,781  INFO Environment:519 - using CGLIB reflection optimizer
22:26:05,859  INFO Configuration:854 - configuring from resource: /hibernate.cfg.xml
22:26:05,859  INFO Configuration:826 - Configuration resource: /hibernate.cfg.xml
22:26:06,656  INFO Configuration:311 - Mapping resource: forum.hbm.xml
22:26:07,375  INFO Binder:229 - Mapping class: newtest.forumclass -> tbl_forum_class
22:26:07,875  INFO Configuration:1017 - Configured SessionFactory: null
22:26:07,875  INFO Configuration:595 - processing one-to-many association mappings
22:26:07,875  INFO Configuration:604 - processing one-to-one association property references
22:26:08,312  INFO Configuration:629 - processing foreign key constraints
22:26:08,515  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
22:26:08,515  INFO SettingsFactory:62 - Use outer join fetching: true
22:26:08,562  INFO NamingHelper:26 - JNDI InitialContext properties:{}
22:26:08,765  INFO DatasourceConnectionProvider:51 - Using datasource: java:comp/env/jdbc/sqlserver
22:26:08,796  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
22:26:12,156  INFO SettingsFactory:102 - Use scrollable result sets: true
22:26:12,171  INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
22:26:12,296  INFO SettingsFactory:108 - Optimize cache for minimal puts: false
22:26:12,296  INFO SettingsFactory:117 - Query language substitutions: {}
22:26:12,312  INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
22:26:12,328  INFO Configuration:1080 - instantiating and configuring caches
22:26:12,937  INFO SessionFactoryImpl:119 - building session factory
22:26:15,828  INFO SessionFactoryObjectFactory:82 - no JNDI name configured
22:26:16,937  INFO SessionFactoryImpl:531 - closing
hibernate.cfg.xml
内容如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">java:comp/env/jdbc/sqlserver</property>
        <property name="show_sql">false</property>
        <property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
        <!-- Mapping files -->
        <mapping resource="forum.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
forum.hbm.xml
内容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="newtest.forumclass" table="tbl_forum_class">
        <id name="id" type="long">
            <column name="iforum_class_id" sql-type="numeric" not-null="true"/>
            <generator class="increment"/>
        </id>
        <property name="name" type="java.lang.String">
            <column name="vforum_class_name" sql-type="varchar(20)" length="20" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>
对象:
package newtest;
public class forumclass
{
private long id;
private String name;
public forumclass()
{}
public long getId()
{
return id;
}
private void setId(long id)
{
this.id = id;
} public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
实现插入:
package newtest;
import newtest.forumclass;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class forumaddclass
{
private Session session;
private Transaction tx;
public forumaddclass()
{}
public void insertName(String name)
{
SessionFactory sessionFactory;
Session session;
try
{
/*
forumclass fc = new forumclass();
fc.setName(name);
session = HibernateUtil.currentSession();
tx= session.beginTransaction();
session.save(fc);
tx.commit();
*/
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
forumclass fc = new forumclass();
fc.setName(name);
session.save(fc);
session.close();
sessionFactory.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
//HibernateUtil.closeSession(); }
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
public void hehe()
{}
}
jsp文件
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="fcbean" scope="page" class="newtest.forumaddclass" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<% try
{
fcbean.insertName("1");
}
catch(Exception e)
{
System.out.println(e.toString());
}
%>
</body>
</html>
sql:
create table tbl_forum_class
(
    iforum_class_id int identity(1,1) constraint iforum_class_id_pk primary key,
    vforum_class_name varchar(20) not null
);
配置前面两个xml放 虚拟目录\WEB-INF\classes里面;第三方类、包和hibernate2.jar放虚拟目录\WEB-INF\lib里面;数据库池正确配置(其他项目正常使用);MsSql的包置于classpath。
环境:Tomcat5.5 + hibernate2.1 + jdk1.5
请问为什么无法插入数值?谢谢大家帮忙。

解决方案 »

  1.   

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
        <class name="newtest.forumclass" table="tbl_forum_class">
            <id name="id" type="long">
                <column name="iforum_class_id" sql-type="numeric" not-null="true"/>
                <generator class="increment"/>
            </id>
            <property name="name" type="java.lang.String">
                <column name="vforum_class_name" sql-type="varchar(20)" length="20" not-null="true"/>
            </property>
        </class>
    </hibernate-mapping>改成<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
        <class name="newtest.forumclass" table="tbl_forum_class">
            <id name="id" type="jav.util.Long">
                <column name="iforum_class_id"/>
                <generator class="increment"/>
            </id>
            <property name="name" type="java.lang.String">
                <column name="vforum_class_name" length="20" not-null="true"/>
            </property>
        </class>
    </hibernate-mapping>把JavaBean里的long换成Long试试
      

  2.   

    我干脆把bean 和 xml 中的long都改成 java.lang.Integer了,但是还是不行呀。信息.log文件还是:
    15:32:26,765  INFO Configuration:854 - configuring from resource: /hibernate.cfg.xml
    15:32:27,156  INFO Configuration:826 - Configuration resource: /hibernate.cfg.xml
    15:32:27,234  INFO Configuration:311 - Mapping resource: forum.hbm.xml
    15:32:27,796  INFO Binder:229 - Mapping class: newtest.forumclass -> tbl_forum_class
    15:32:27,828  INFO Configuration:1017 - Configured SessionFactory: null
    15:32:27,828  INFO Configuration:595 - processing one-to-many association mappings
    15:32:27,828  INFO Configuration:604 - processing one-to-one association property references
    15:32:27,828  INFO Configuration:629 - processing foreign key constraints
    15:32:27,828  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
    15:32:27,843  INFO SettingsFactory:62 - Use outer join fetching: true
    15:32:27,859  INFO NamingHelper:26 - JNDI InitialContext properties:{}
    15:32:27,859  INFO DatasourceConnectionProvider:51 - Using datasource: java:comp/env/jdbc/sqlserver
    15:32:27,859  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
    15:32:27,921  INFO SettingsFactory:102 - Use scrollable result sets: true
    15:32:27,921  INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
    15:32:27,937  INFO SettingsFactory:108 - Optimize cache for minimal puts: false
    15:32:27,937  INFO SettingsFactory:117 - Query language substitutions: {}
    15:32:27,953  INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
    15:32:27,953  INFO Configuration:1080 - instantiating and configuring caches
    15:32:27,953  INFO SessionFactoryImpl:119 - building session factory
    15:32:28,078  INFO SessionFactoryObjectFactory:82 - no JNDI name configured
    15:32:28,156  INFO SessionFactoryImpl:531 - closing
    照样没插入,老实说这些信息好象没提示很大的错误但是为什么无法插入呢?还有一点,我这个表的iforumclass_id主键,还做为其他表的外键约束,跟这个会不会有关系?