【问题标题】:Exception in thread "main" org.hibernate.MappingException: could not instantiate id generator at org.hibernate.id.IdentifierGeneratorFactory.create线程“主”org.hibernate.MappingException 中的异常:无法在 org.hibernate.id.IdentifierGeneratorFactory.create 处实例化 id 生成器
【发布时间】:2015-10-13 18:21:20
【问题描述】:

我是 Hibernate 框架的新手。我创建了一些代码来将数据存储在数据库中,但我无法存储它。它会抛出错误。

线程“主”org.hibernate.MappingException 中的异常:无法在 org.hibernate.id.IdentifierGeneratorFactory.create 处实例化 id 生成器

请告诉我我能做些什么来解决它?

我的目录结构在这里。

Employee.java

package com.hib;

public class Employee {

private int id;
private String firstName, lastName;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

员工.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="com.hib.Employee" table="emp1000">

    <id name="id">
        <generator class="assined"></generator>
    </id>

    <property name="firstName"></property>

    <property name="lastName"></property>


</class>

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>

    <property name="hbm2ddl.auto">update</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property   name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle12c</property>
    <property name="connection.username">AtulRai</property>
    <property name="connection.password">atulrai</property>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <mapping resource="employee.hbm.xml" />

</session-factory>

StoreData.java

  package com.hib;

 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;

public class StoreData {
public static void main(String[] args) {

    // creating configuration object
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");// populates the date of
                                        // configuration file

    // creating session object factory
    SessionFactory factory = cfg.buildSessionFactory();

    // creating session object

    Session session = factory.openSession();

    // creating transaction object
    Transaction t = session.beginTransaction();

    Employee e1 = new Employee();
    e1.setId(1111);
    e1.setFirstName("Atul");
    e1.setLastName("Rai");

    // persisting the object
    session.persist(e1);

    // transaction is commited
    t.commit();
    session.close();

    System.out.println("Data saved successfully");

}

}

以上都是我的代码,但是当我执行它时出现以下错误

log4j:WARN No appenders could be found for logger      (org.hibernate.cfg.Environment).
  log4j:WARN Please initialize the log4j system properly.
 Exception in thread "main" org.hibernate.MappingException: could not instantiate id generator
   at    org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98)
    at   org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
at com.hib.StoreData.main(StoreData.java:17)
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: assined
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:109)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:92)
... 4 more

【问题讨论】:

  • 你真的需要使用hbm文件吗?我建议使用@annotations
  • 如何在hibernate中使用@annotations?
  • 我回答了你一些关于 Hibernate Annotations 的信息,如果有帮助请告诉我。

标签: java eclipse hibernate log4j


【解决方案1】:

正如我在 cmets 中提到的,注解在很多情况下都非常简单和有用。

一些专家认为配置必须与代码分开,但我认为在这种情况下,您可以使用注释来配置如何将您的实体保存在 DB .

请注意,有很多相关的注释,例如:

  • @Entity 用来表示它是一个持久实体。
  • @Table 表示这个类必须持久化到一个数据库表中。
  • @Column 表示以下属性将是来自 @Table
  • @Id 这个属性是表的PK。
  • @GeneratedValue 提供主键值的生成策略规范。

您可以谷歌更多有用的@annotations 以获取更多信息。

下面是一个示例,说明如何使用注释来注释你的持久性类

@Entity
@Table(name = "stock", catalog = "mkyong", uniqueConstraints = {
        @UniqueConstraint(columnNames = "STOCK_NAME"),
        @UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {

    private Integer stockId;
    private String stockCode;
    private String stockName;

    public Stock() {
    }

    public Stock(String stockCode, String stockName) {
        this.stockCode = stockCode;
        this.stockName = stockName;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "STOCK_ID", unique = true, nullable = false)
    public Integer getStockId() {
        return this.stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
    public String getStockCode() {
        return this.stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
    public String getStockName() {
        return this.stockName;
    }

    public void setStockName(String stockName) {
        this.stockName = stockName;
    }

    @Override
    public String toString() {
        return "Stock [stockCode=" + stockCode + ", stockId=" + stockId
                + ", stockName=" + stockName + "]";
    }
}

更多信息请关注link.

【讨论】:

  • 感谢您的帮助。实际上我的问题是由 JAR 文件造成的。我在我的项目中添加了一些 jars 文件。现在正在工作。
  • 投票支持关于注释的信息(主要区别于@Entity 和@Table)。
【解决方案2】:

我不知道这是否是一个错字,但我很确定这个类是assigned而不是assined

<id name="id"> <generator class="assined"></generator> </id>

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2016-03-01
    • 2017-06-03
    • 2023-03-19
    相关资源
    最近更新 更多