I have 2 tables Asset and Asset_Dist_Types. Asset is parent and Asset_Dist_Types is a child table. Asset_Dist_Types is having 2 columns asset_id and lkp_dist_type where asset_id is the primary key in Asset table. In Asset_Dist_Types it is a many to many (one asset_id can have multiple lkp_dist_type entries.) In java, we have entity class only for Asset table. In that for Asset_Dist_Type, they have mentioned it as collection of elements. In Asset.java, entry for Asset_Dist_Type is as follows.
@CollectionOfElements
@JoinTable(name = "ASSET_DIST_TYPE", joinColumns = @JoinColumn(name="ASSET_ID"))
@Column(name="LKP_DIST_TYPE")
private Set<Integer> distTypes = new LinkedHashSet<Integer>(0);
Now I would like to update Asset_Dist_Type table's lkp_dist_type column. I have list of asset id's. I have written following query to update it.
int hql = entityManager.createQuery(
"update Asset a set a.distTypes = :distTypeParamId where a.assetId in (:assetIdParam)")
.setParameter("distTypeParamId", distTypeList)
.setParameter("assetIdParam", assetIdListToUpdateLOB)
.executeUpdate();
But this is throwing
javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute update query.
Since I am new to hibernate I am not getting what is the solution. Can someone please help me?
Solved it by following a different way.. I got list of asset id's and list of distribution types. I iterated over the list of asset id's and for each asset id I added list of distribution types and flushed it. It is working now..!
After getting list of asset id's
for(int i=0;i<assetIdListToUpdateLOB.size();i++){
int assetId = assetIdListToUpdateLOB.get(i);
System.out.println(assetId);
List<Asset> assetDetailsList = entityManager
.createQuery(
"select distinct asset from Asset asset "
+ "left join fetch asset.distTypes dt "
+ "where asset.assetId = :assetIdParam")
.setParameter("assetIdParam", assetId).getResultList();
if(assetDetailsList.size()>0){
this.asset = assetDetailsList.get(0);
asset.getDistTypes().clear();
asset.getDistTypes().addAll(selectedDistributionTypes);
entityManager.flush();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.