I am practicing a simple Hibernate program and ran into the strange issue.
My Model :
public class Employee implements java.io.Serializable {
// Fields
private Integer empId;
private String empName;
private BigDecimal empSal;
//setters and getters
My Test class :
public static void main(String[] args) {
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
Employee emp = new Employee();
emp.setEmpName("abc");
emp.setEmpSal(new BigDecimal("100000"));
Transaction t = session.beginTransaction();
session.save(emp);
t.commit();
System.out.println("Object Saved Succesfully!!");
session.close();
factory.close();
}
exception I am getting :
Exception in thread "main" org.hibernate.InstantiationException: could not instantiate test objectcom.gbn.model.Employee
at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:25)
at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:44)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:123)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at com.gbn.test.Test.main(Test.java:19)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:22)
... 9 more
Caused by: java.lang.Error: Unresolved compilation problem:
The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files
at com.gbn.model.Employee.<init>(Employee.java:1)
... 14 more
If I don't implement Serializable in Employee model, I am able to save data without issue. the issue only when my pojo implements Serializable.
What causing the issue?
I have tried solutions as suggested here and here, but unable to resolve my issue.
hbm file :
<hibernate-mapping>
<class name="com.gbn.model.Employee" table="EMPLOYEE">
<id name="empId" type="java.lang.Integer">
<column name="EMP_ID" precision="22" scale="0" />
<generator class="increment" />
</id>
<property name="empName" type="java.lang.String">
<column name="EMP_NAME" length="20" />
</property>
<property name="empSal" type="java.math.BigDecimal">
<column name="EMP_SAL" precision="22" scale="0" />
</property>
</class>