Hi All,
I am having my mapping like :
<class name="X" table="x" lazy="true"
dynamic-insert="true" >
<id name="xID" column="x_ID">
<generator class="native"/>
</id>
<property name="name" column="NAME" lazy="true"/>
<property name="desc" column="DESC" lazy="true"/>
<many-to-one name="y" class="Y"
column="x_ID" property-ref="xID" insert="false"
update="false" />
<many-to-one name="z" class="Z"
column="x_ID" property-ref="xID"
insert="false" update="false" />
</class>
Now My update Method is like :
public boolean editData(final ServiceBean s) {
if(s == null) {
throw new IllegalArgumentException("s can not be null");
}
TransactionTemplate template =
new TransactionTemplate(this.transactionManager);
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
try {
template.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionState) throws HibernateException {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
// I tried with following methods :
One is this...
String hqlQuery = new StringBuilder("FROM X x ")
.append("WHERE ")
.append("x.xID = ")
.append(+s.getID())
.toString();
Query query = session.createQuery(hqlQuery);
List aList = query.list();
for(Object object : aList) {
X x = (X)object;
x.setName(s.getName());
// note I am not setting desc here
getHibernateTemplate().update(x);
Another is this :
Integer id = new Integer(s.getID());
X x = (X)session.load(X.class, id);
x.setName("D");
getHibernateTemplate().update(x);
}
return null;
}
});
return null;
}
});
} catch(Exception exp) {
exp.printStackTrace();
return false;
}
return true;
}
Now I am getting this error :
Hibernate: select x0_.x_ID as x_35_2_, x0_.NAME as x2_35_2_, x0_.DESC as x12_35_2_, y1_.Y_ID as y1_22_0_, z2_.Z_ID as Z1_16_1_ from X x0_ left outer join Y y1_ on x0_.X_ID=y1_.Y_ID left outer join Z z2_ on x.X_ID=z.Z_ID where x0_.X_ID=?
Hibernate: select y0_.Y_ID as Y1_27_0_ from Y y0_ where y0_.Y_ID=?
Hibernate: select z0_.Z_ID as z1_20_0_ from Z z0_ where z0_.Z_ID=?
org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property of primitive type setter of X.xID; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of X.xID Caused by: org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of X.xID at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:85)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
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:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127)
Caused by: java.lang.IllegalArgumentException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 58 more
I am not in favour to write a select for all fields of table and set all values in bean. and If I am selecting the fields which are required to update. Other fields are getting updated with default values set in Database.
Is There a some solution to resolve this problem ?
If you thinks for some link to be useful or have a solution. please give me that. I will be thankful to you.
THANKS,
GOPAL
|