hibernate java.lang.IllegalArgumentException: object is not an instance of declaring class 问题 50
publicclassAddress{privateIntegeraddressId;privateStringaddressDetail;publicAddress()...
public class Address
{
private Integer addressId;
private String addressDetail;
public Address(){
}
public Address(String addressDetail){
this.addressDetail=addressDetail;
}
public Integer getAddressId()
{
return addressId;
}
public void setAddressId(Integer addressId)
{
this.addressId = addressId;
}
public String getAddressDetail()
{
return addressDetail;
}
public void setAddressDetail(String addressDetail)
{
this.addressDetail = addressDetail;
}
}
<hibernate-mapping>
<class name="dao.Address" table="address_inf">
<id name="addressId" type="java.lang.Integer">
<column name="address_id"></column>
<generator class="native">
<param name="sequence">testsequence</param>
</generator>
</id>
<property name="addressDetail" type="java.lang.String">
<column name="addressDetail"></column>
</property>
</class>
</hibernate-mapping>
public class TestManytoOne
{
public static void main(String[] args)
{
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Testtb user = new Testtb();
Address a = new Address("耶路撒冷");
user.setId("00000000001");
user.setName("ddddddddd");
user.setAddress(a.toString());
session.persist(user);//执行到这一步报的错
Address a2 = new Address("流沙河");
user.setAddress(a2.toString());
tx.commit();
session.close();
}
}
严重: IllegalArgumentException in class: dao.Address, getter method of property: addressId
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of dao.Address.addressId
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:199)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3605)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3321)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:535)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:93)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:637) 展开
{
private Integer addressId;
private String addressDetail;
public Address(){
}
public Address(String addressDetail){
this.addressDetail=addressDetail;
}
public Integer getAddressId()
{
return addressId;
}
public void setAddressId(Integer addressId)
{
this.addressId = addressId;
}
public String getAddressDetail()
{
return addressDetail;
}
public void setAddressDetail(String addressDetail)
{
this.addressDetail = addressDetail;
}
}
<hibernate-mapping>
<class name="dao.Address" table="address_inf">
<id name="addressId" type="java.lang.Integer">
<column name="address_id"></column>
<generator class="native">
<param name="sequence">testsequence</param>
</generator>
</id>
<property name="addressDetail" type="java.lang.String">
<column name="addressDetail"></column>
</property>
</class>
</hibernate-mapping>
public class TestManytoOne
{
public static void main(String[] args)
{
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Testtb user = new Testtb();
Address a = new Address("耶路撒冷");
user.setId("00000000001");
user.setName("ddddddddd");
user.setAddress(a.toString());
session.persist(user);//执行到这一步报的错
Address a2 = new Address("流沙河");
user.setAddress(a2.toString());
tx.commit();
session.close();
}
}
严重: IllegalArgumentException in class: dao.Address, getter method of property: addressId
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of dao.Address.addressId
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:199)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3605)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3321)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:535)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:93)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:637) 展开
3个回答
2011-07-08
展开全部
<id name="addressId" type="java.lang.Integer">
<column name="address_id"></column>
<generator class="native">
<param name="sequence">testsequence</param>
</generator>
</id>
既然是配置一个Integer的值,为什么要用一个String类型的值?
还有就是和一楼的疑问一样,为什么要对Address a = new Address("耶路撒冷");中的a对象引用进行toString()方法呢?引用名.toString()你得到的结果将是 包名+类名+@十六进制的内存地址
<column name="address_id"></column>
<generator class="native">
<param name="sequence">testsequence</param>
</generator>
</id>
既然是配置一个Integer的值,为什么要用一个String类型的值?
还有就是和一楼的疑问一样,为什么要对Address a = new Address("耶路撒冷");中的a对象引用进行toString()方法呢?引用名.toString()你得到的结果将是 包名+类名+@十六进制的内存地址
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询