我是Hibernate框架的全新手。我已经创建了一些代码来将数据存储在数据库中,但我无法存储它。它会抛出错误。
线程“main”中的异常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;
}
}
employee.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
答案 0 :(得分:1)
正如我在评论中提到的, 注释 在许多情况下非常简单和有用。
一些专家认为配置必须与代码分开,但我认为在这种情况下,您可以使用注释来配置如何在 数据库 中保留实体
请注意,有很多相关的注释,如:
您可以使用Google更有用的@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.
答案 1 :(得分:0)
我不知道它是否是拼写错误,但我很确定该课程已分配而非已确定
<id name="id">
<generator class="assined"></generator>
</id>