Reputation: 354
I am a totally newbie of Hibernate framework. I have created some code to store the data in database but I am not able to store it. It will throw the error.
Exception in thread "main" org.hibernate.MappingException: could not instantiate id generator at org.hibernate.id.IdentifierGeneratorFactory.create
Please tell me what can I do to solve it?
My directory structure is here.
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");
}
}
All the above is my code but when I execute it I have the following error
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
Upvotes: 0
Views: 5125
Reputation: 399
I dont know if its a typo or not, but I'm pretty sure the class is assigned and not assined
<id name="id">
<generator class="assined"></generator>
</id>
Upvotes: 0
Reputation: 1234
as i mentioned in comments, annotations are really simple and usefull in many cases.
Some experts argument that configuration must be apart from code, but i think that in this cases, you could use annotations to configure how to persist your entities in your DB.
Please, note that the are many relevant annotations like:
You can Google more useful @annotations for more information.
Here is an example of how you could use annotations to anotate your persistent clases:
@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 + "]";
}
}
For more information, please follow this link.
Upvotes: 1