Reputation: 12063
I am getting the following
Hibernate: select employees0_.employee_id as employee1_0_0_, employees0_.commission_pct as commission2_0_0_, employees0_.department_id as department3_0_0_, employees0_.email as email0_0_, employees0_.first_name as first5_0_0_, employees0_.hire_date as hire6_0_0_, employees0_.job_id as job7_0_0_, employees0_.last_name as last8_0_0_, employees0_.manager_id as manager9_0_0_, employees0_.phone_number as phone10_0_0_, employees0_.salary as salary0_0_ from employees employees0_ where employees0_.employee_id=?
1566 [main] INFO org.hibernate.event.def.DefaultLoadEventListener - Error performing load command
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.prateek.hibernate.employees.commission_pct
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:143)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:583)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:229)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3847)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982)
at org.hibernate.loader.Loader.doQuery(Loader.java:857)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2037)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3293)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998)
at com.prateek.hibernate.FirstHibernate.main(FirstHibernate.java:17)
Caused by: java.lang.IllegalArgumentException: Can not set int field com.prateek.hibernate.employees.commission_pct to null value
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
How can I resolve this error?
package com.prateek.hibernate;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
public class employees {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int employee_id;
private String first_name;
private String last_name;
private String email;
private String phone_number;
@Temporal(TemporalType.DATE)
private Date hire_date;
private String job_id;
private long salary;
private int commission_pct;
private int manager_id;
private int department_id;
//Setter and Getters
}
Here is the main class
package com.prateek.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstHibernate {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
employees e = (employees) session.get(employees.class, 100);
System.out.println("First Name :" + e.getFirst_name()+","+
"Last Name :"+ e.getLast_name());
session.getTransaction().commit();
session.close();
}
}
Hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">hr</property>
<property name="connection.password">hr</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.prateek.hibernate.employees"/>
</session-factory>
</hibernate-configuration>
Upvotes: 3
Views: 9092
Reputation: 99
We can handle this case with out changing primitive to wrapper or even with out setting not null to db column.
We can use the JDBC ResultSet to return the default values for those types.
For your case you can getInt() of ResultSet.
int getInt(int columnIndex) throws SQLException; will return the column value. And if the value is SQL NULL, the value returned is 0.
Similar to this we do have others to handle all primitive types.
Upvotes: 0
Reputation: 1022
Change your data type from primitve to their Wrapper class (Boxed types).
So, you replace this:
private long salary;
private int commission_pct;
private int manager_id;
private int department_id;
with this:
private Long salary;
private Integer commission_pct;
private Inetegr manager_id;
private Integer department_id;
and do the same with your getters and setters!
It worked for me.
Upvotes: 3
Reputation: 367
Like the exception states, null
cannot be assigned to the primitive type int
.
I assume that the value of your database column that is mapped to the field commission_pct
is NULL
.
You have two options:
1) Change the value in the database to a value that is not NULL
2) Change the type of the field to java.lang.Integer
to allow NULL
values
Upvotes: 8