小弟是想问关于Hibernate Annotations 用户自定义类型的问题,我在做插入操作的时候,数据就可以正确的插入到数据库中;但是执行查询操作的时候,就出现错误了,SQL语句可以出来的,也是可以在PL/SQL中正确执行,异常如下:
Hibernate: select customers_0_.id as id7_0_, customers_0_.COM_STREET as COM2_7_0_, customers_0_.COM_CITY as COM3_7_0_, customers_0_.COM_PROVINCE as COM4_7_0_, customers_0_.COM_ZIPCODE as COM5_7_0_, customers_0_.HOME_STREET as HOME6_7_0_, customers_0_.HOME_CITY as HOME7_7_0_, customers_0_.HOME_PROVINCE as HOME8_7_0_, customers_0_.HOME_ZIPCODE as HOME9_7_0_, customers_0_.sex as sex7_0_, customers_0_.phone as phone7_0_, customers_0_.FIRSTNAME as FIRSTNAME7_0_, customers_0_.LASTNAME as LASTNAME7_0_ from CUSTOMERS_USERTYPE customers_0_ where customers_0_.id=?
---------------------------SQL可以打出来了,可以在PL/SQL里面执行
10:41:23,125 WARN JDBCExceptionReporter:71 - SQL Error: 17024, SQLState: null
10:41:23,125 ERROR JDBCExceptionReporter:72 - 未读取数据
10:41:23,140 INFO DefaultLoadEventListener:109 - Error performing load command
org.hibernate.exception.GenericJDBCException: could not load an entity: [com.usertype.forms.Customers_UserType#7]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1798)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2977)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:393)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:374)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:137)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:193)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:101)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:460)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:454)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:448)
at com.usertype.dao.Customers_UserTypeImpDao.getCustomers_UserType(Customers_UserTypeImpDao.java:18)
at com.hibernate.Customers_UserTypeServices.showCustomers_UserType(Customers_UserTypeServices.java:59)
at com.hibernate.Main.main(Main.java:48)
Caused by: java.sql.SQLException: 未读取数据
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.driver.OracleStatement.wasNullValue(OracleStatement.java:3174)
at oracle.jdbc.driver.OracleResultSetImpl.wasNull(OracleResultSetImpl.java:127)
at com.usertype.AddressUserType.nullSafeGet(AddressUserType.java:53)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:105)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2031)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1371)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1784)
... 18 more
我的部分代码如下:
1.POJO
package com.usertype.forms;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.*;
/**
*@project clustering
*@author xuchao
*@create_date 2008-3-6
*@description
*/
@SuppressWarnings("serial")
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name="CUSTOMERS_USERTYPE")
@SequenceGenerator(name="CUSTOMERS_USERTYPE_SEQ",sequenceName="CUSTOMERS_USERTYPE_SEQ")
public class Customers_UserType implements Serializable {
private Integer id;
private Name name;
private Address homeAddress;
private Address comAddress;
private Gender gender;
private Integer phone;
/** full constructor */
public Customers_UserType(Name name, Address homeAddress, Address comAddress,Gender gender,Integer phone) {
this.name = name;
this.homeAddress = homeAddress;
this.comAddress = comAddress;
this.gender=gender;
this.phone=phone;
}
public Customers_UserType() {
}
@Type(type="com.usertype.AddressUserType")
@Columns(columns = {
@Column(name="COM_STREET",length=30),
@Column(name="COM_CITY",length=30),
@Column(name="COM_PROVINCE",length=30),
@Column(name="COM_ZIPCODE",length=30)
})
public Address getComAddress() {
return comAddress;
}
public void setComAddress(Address comAddress) {
this.comAddress = comAddress;
}
@Type(type = "com.usertype.GenderUserType")
@Column(name="sex",length=1)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Type(type="com.usertype.AddressUserType")
@Columns(columns = {
@Column(name="HOME_STREET",length=30),
@Column(name="HOME_CITY",length=30),
@Column(name="HOME_PROVINCE",length=30),
@Column(name="HOME_ZIPCODE",length=30)
})
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
@Id
@GeneratedValue(generator="CUSTOMERS_USERTYPE_SEQ")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Type(type="com.usertype.NameCompositeUserType")
@Columns(columns = {
@Column(name="FIRSTNAME",length=30),
@Column(name="LASTNAME",length=30)
})
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
@Type(type = "com.usertype.PhoneUserType")
@Column(name="phone",length=20)
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
}
2. DAO
package com.usertype.dao;
import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.usertype.forms.Customers_UserType;
/**
*@project clustering
*@author xuchao
*@create_date 2008-3-6
*@description
*/
public class Customers_UserTypeImpDao extends HibernateDaoSupport implements
Customers_UserTypeDao {
/*错误就在这里,执行Get方法的时候,报错*/
public Customers_UserType getCustomers_UserType(Serializable id) {
return (Customers_UserType) getHibernateTemplate().get(Customers_UserType.class, id);
}
/*在做插入操作的时候,就可以正常执行.*/
public void insertCustomers_UserType(Customers_UserType customers_UserType) {
getHibernateTemplate().save(customers_UserType);
}
}