这个是我的User.java:
package ch03.hibernate;public class User {
// id
private int id;

// UserName
private String username;

// Password
private String password;

// Email
private String email; public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
}}
这个是我的User.hbm.xml:
<?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">

<hibernate-mapping> <class name="ch03.hibernate.User" table="myusertable">
<id name="id">
<generator class=" identity "/>
</id>
<property name="username"/>
<property name="password"/>
<property name="email"/>

</class>

</hibernate-mapping>
这个是我的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/MyProject</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (连接池) (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect(SQL方言) -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<!-- 映射资源 -->
<mapping resource="ch03/hibernate/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>
这个是我的Test.java:
package ch03.hibernate;import org.hibernate.*;
import org.hibernate.cfg.*;public class Test { /**
 * @param args
 */
public static void main(String[] args) {
// TODO 自动生成方法存根
try {
// 常见Session工厂
SessionFactory sf = new Configuration().configure().buildSessionFactory();
// 获取Session实例
Session session = sf.openSession();
// 开始事务
Transaction tx = session.beginTransaction();
// 常见一个User对象
User user = new User();
// 为对象赋值
user.setUsername("Hibernate");
user.setPassword("123");
// 调用save()方法保存user实例到数据库
session.save(user);
// 提交事务
tx.commit();
// 关闭Session
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
正常的情况应该是有一行数据+入数据库中...我在MYSQL中已经建立了一个MYPROJECT的数据库.
可是却出现下面的异常:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:97)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:182)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at ch03.hibernate.Test.main(Test.java:15)
Caused by: org.hibernate.MappingException: could not interpret id generator strategy:  identity 
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:108)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:91)
... 4 more谁给指点一下为什么啊?

解决方案 »

  1.   

    没有加上log4j的包,貌似主键生成器有问题,加上log4j后把log级别设成debug再看看
      

  2.   

    一个正确的hibernate配置片段
    属性与列名都出现
    <id name="typeId" type="java.lang.Integer">
         <column name="typeId" />
         <generator class="identity" />
         </id>
         <property name="belongmodule" type="java.lang.String">
         <column name="belongmodule" length="45" not-null="true" />
         </property>
         <property name="note" type="java.lang.String">
         <column name="note" length="45" not-null="true" />
         </property>
         <property name="typeName" type="java.lang.String">
         <column name="type_name" length="45" not-null="true" />
         </property>
      

  3.   

    会不会是你书库表建的有问题呢?
    我知道如果在用命令建mysql数据表的时候,有个innodb的,如果没有这个,是不支持事务的。会不会和这个有关系?
      

  4.   

    identity:对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。返回的标识符是long, short 或者int类型的。 而你的配置文件只有<id   name= "id "> ,没有为其制定类型。
    加上 type= "java.lang.Integer "如果是自动增长的,要用increment