初学hibernate   我现在遇到了运行时出现了log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
就算是不管警告,为什么我的数据库表时没有内容啊(eclipse+tomcat+sqlserver2005 数据库test 表test  属性:id int ;sname char(200); pw char(200) )//映射到数据库的那个bean
package mystudent;public class student {
private int id;
private String name;
private String pw;
/**
 * @return the name
 */public String getName() {
return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
this.name = name;
}
/**
 * @return the pw
 */
public String getPw() {
return pw;
}
/**
 * @param pw the pw to set
 */
public void setPw(String pw) {
this.pw = pw;
}
/**
 * @return the id
 */
public int getId() {
return id;
}
/**
 * @param id the id to set
 */
public void setId(int id) {
this.id = id;
}}
//测试将数据写入数据库的类package mystudent;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class teststudent { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
        student stu=new student();
        stu.setId(1);
        stu.setName("a");
        stu.setPw("2");
        System.out.println("1");
        Configuration config=new Configuration();
        SessionFactory sf=config.configure().buildSessionFactory();
        Session session=sf.openSession();
       // session.beginTransaction();
        
        session.save(stu);
       // session.getTransaction();
        session.close();
        sf.close();
        System.out.println("1");
        
}}//配置文件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-3.0.dtd"><hibernate-configuration>
    <session-factory > <!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">mysql</property>
<!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for Microsoft SQL Server -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>        <property name="hibernate.show_sql">true</property>
        <mapping resource="mystudent/student.hbm.xml"></mapping>
       <!--   <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>-->
    </session-factory>
    
</hibernate-configuration>
//配置文件student.hbm.xml
<?xml version="1.0" ?> 
 <!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="mystudent" >
  <class name="student" table="test" >
  <id name="id" >
  </id>
   <property name="name" column="sname">
  </property>
  <property name="pw" column="pw">
  </property>
  </class>
  </hibernate-mapping>

解决方案 »

  1.   

    不好意思我的包没有加完
    //现在出现了如下问题:出错显示的
    Hibernate: 
        insert 
        into
            test
            (sname, pw) 
        values
            (?, ?)为什么只有两个??
    12:06:02,359  INFO Environment:514 - Hibernate 3.2.6
    12:06:02,406  INFO Environment:532 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.HSQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:hsqldb:./build/db/hsqldb/hibernate, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
    12:06:02,406  INFO Environment:563 - using java.io streams to persist binary types
    12:06:02,406  INFO Environment:681 - Bytecode provider name : cglib
    12:06:02,421  INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
    12:06:02,656  INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml
    12:06:02,656  INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml
    12:06:03,296  INFO Configuration:559 - Reading mappings from resource : mystudent/student.hbm.xml
    12:06:03,546  INFO HbmBinder:300 - Mapping class: mystudent.student -> test
    12:06:03,609  INFO Configuration:1547 - Configured SessionFactory: null
    12:06:03,796  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
    12:06:03,812  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
    12:06:03,812  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
    12:06:04,062  INFO DriverManagerConnectionProvider:80 - using driver: com.microsoft.sqlserver.jdbc.SQLServerDriver at URL: jdbc:sqlserver://localhost:1433;DatabaseName=test
    12:06:04,062  INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
    12:06:05,203  INFO SettingsFactory:89 - RDBMS: Microsoft SQL Server, version: 9.00.1399
    12:06:05,203  INFO SettingsFactory:90 - JDBC driver: Microsoft SQL Server 2005 JDBC Driver, version: 1.2.2828.100
    12:06:05,281  INFO Dialect:152 - Using dialect: org.hibernate.dialect.SQLServerDialect
    12:06:05,296  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
    12:06:05,312  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    12:06:05,312  INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
    12:06:05,312  INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
    12:06:05,312  INFO SettingsFactory:162 - Scrollable result sets: enabled
    12:06:05,328  INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): enabled
    12:06:05,328  INFO SettingsFactory:178 - Connection release mode: auto
    12:06:05,328  INFO SettingsFactory:202 - Maximum outer join fetch depth: 1
    12:06:05,328  INFO SettingsFactory:205 - Default batch fetch size: 1
    12:06:05,328  INFO SettingsFactory:209 - Generate SQL with comments: disabled
    12:06:05,328  INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
    12:06:05,328  INFO SettingsFactory:217 - Order SQL inserts for batching: disabled
    12:06:05,328  INFO SettingsFactory:386 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    12:06:05,343  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
    12:06:05,343  INFO SettingsFactory:225 - Query language substitutions: {no='N', yes='Y'}
    12:06:05,343  INFO SettingsFactory:230 - JPA-QL strict compliance: disabled
    12:06:05,343  INFO SettingsFactory:235 - Second-level cache: enabled
    12:06:05,343  INFO SettingsFactory:239 - Query cache: disabled
    12:06:05,343  INFO SettingsFactory:373 - Cache provider: org.hibernate.cache.HashtableCacheProvider
    12:06:05,343  INFO SettingsFactory:254 - Optimize cache for minimal puts: disabled
    12:06:05,343  INFO SettingsFactory:259 - Cache region prefix: hibernate.test
    12:06:05,343  INFO SettingsFactory:263 - Structured second-level cache entries: disabled
    12:06:05,359  INFO SettingsFactory:283 - Echoing all SQL to stdout
    12:06:05,359  INFO SettingsFactory:290 - Statistics: disabled
    12:06:05,359  INFO SettingsFactory:294 - Deleted entity synthetic identifier rollback: disabled
    12:06:05,359  INFO SettingsFactory:309 - Default entity-mode: pojo
    12:06:05,375  INFO SettingsFactory:313 - Named query checking : enabled
    12:06:05,468  INFO SessionFactoryImpl:161 - building session factory
    12:06:06,203  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
    Hibernate: 
        insert 
        into
            test
            (sname, pw) 
        values
            (?, ?)
    12:06:06,562  WARN JDBCExceptionReporter:77 - SQL Error: 515, SQLState: 23000
    12:06:06,562 ERROR JDBCExceptionReporter:78 - 不能将值 NULL 插入列 'id',表 'test.dbo.test';列不允许有空值。INSERT 失败。
    Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [mystudent.student]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at mystudent.teststudent.main(teststudent.java:21)
    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'id',表 'test.dbo.test';列不允许有空值。INSERT 失败。
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
    ... 16 more
      

  2.   

     ID 设置为自动 增长
      
    <class name="student" table="test" >
     <column name="id" />
                <generator class="native" />
    </column>
      

  3.   

    看这个:
    http://topic.csdn.net/u/20100330/11/2c1818e2-7044-41e3-b89b-700dc9b89665.html?seed=93495102&r=64295645#r_64295645
      

  4.   

    那个问号没关系,hibernate的打印就是这样。问号只是个占位符,即使有值也打印问号。
    问题是你的id配置的不对。
    <id column="id" name="id" type="java.lang.Long">
    <generator class="increment" />
    </id>
    这样应该就没问题了。
      

  5.   

    用了increment可以,可是如果我不想让他自动增加,我要用自己设置的怎么办,是不是用identity那样就又是同样的问题了
      

  6.   

    increment -> Assigned
    这样应该就行了。
      

  7.   

    log4j:WARN Please initialize the log4j system properly.有没有使用log4j 或者 log4j的jar包是否存在问题?请您帮忙看一下关于Log4j日志中的问题