我收到以下
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
有人可以帮我解决这个错误吗?
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
}
这是主要的课程
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>
任何人都可以告诉我这里我错了!!!
答案 0 :(得分:8)
与异常状态一样,null
无法分配给基本类型int
。
我假设映射到字段commission_pct
的数据库列的值为NULL
。
您有两种选择:
1)将数据库中的值更改为非NULL
的值
2)将字段类型更改为java.lang.Integer
以允许NULL
值
答案 1 :(得分:3)
将您的数据类型从primitve更改为Wrapper类(Boxed types)。
所以,你替换它:
private long salary;
private int commission_pct;
private int manager_id;
private int department_id;
用这个:
private Long salary;
private Integer commission_pct;
private Inetegr manager_id;
private Integer department_id;
并对你的吸气剂和制定者做同样的事情!
它对我有用。
答案 2 :(得分:0)
我们可以处理这种情况,不需要将原语更改为包装器,甚至可以在不设置为db列的情况下设置。
我们可以使用JDBC ResultSet返回这些类型的默认值。
对于您的情况,您可以使用ResultSet的getInt()。
int getInt(int columnIndex)抛出SQLException; 将返回列值。如果值为SQL NULL,则返回的值为0.
与此类似,我们还有其他人可以处理所有原始类型。