hibernate写list到mysql

时间:2022-04-16 09:56:03
用jpa写下面语句执行报错,估计要先手动转成字符串吧,工作忙没继续下去了。
public void persist(Goods goods) {
Assert.notNull(goods);
// goods.setId(new Long(1));
List<GoodsItem> goodsItemList = goods.getGoodsItemList(); String jpql ="update Goods goods set goods.goodsItemList = :p1"+
"where goods.id =1";
entityManager.createQuery(jpql).setParameter("p1",goodsItemList).executeUpdate();
}
看到有直接保存一个类实例的方法,直接拿来用了。
@Transactional
public T update(T entity, String... ignoreProperties) {
Assert.notNull(entity);
Assert.isTrue(!entity.isNew());
Assert.isTrue(!baseDao.isManaged(entity)); T persistant = baseDao.find(baseDao.getIdentifier(entity));
if (persistant != null) {
copyProperties(entity, persistant, (String[]) ArrayUtils.addAll(ignoreProperties, UPDATE_IGNORE_PROPERTIES));
}
return update(persistant);
}
/********************************一下为原类*********************************/
@Transactional
public abstract class BaseService<T extends BaseEntity<ID>, ID extends Serializable>{ /** 更新忽略属性 */
private static final String[] UPDATE_IGNORE_PROPERTIES = new String[] { BaseEntity.CREATE_DATE_PROPERTY_NAME, BaseEntity.MODIFY_DATE_PROPERTY_NAME, BaseEntity.VERSION_PROPERTY_NAME }; /** BaseDao */
private BaseDao<T, ID> baseDao; @Autowired
protected void setBaseDao(BaseDao<T, ID> baseDao) {
this.baseDao = baseDao;
} @Transactional(readOnly = true)
public T find(ID id) {
return baseDao.find(id);
} @Transactional(readOnly = true)
public List<T> findAll() {
return findList(null, null, null, null);
} @Transactional(readOnly = true)
public List<T> findList(ID... ids) {
List<T> result = new ArrayList<T>();
if (ids != null) {
for (ID id : ids) {
T entity = find(id);
if (entity != null) {
result.add(entity);
}
}
}
return result;
} @Transactional(readOnly = true)
public List<T> findList(Integer count, List<Filter> filters, List<Order> orders) {
return findList(null, count, filters, orders);
} @Transactional(readOnly = true)
public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) {
return baseDao.findList(first, count, filters, orders);
} @Transactional(readOnly = true)
public Page<T> findPage(Pageable pageable) {
return baseDao.findPage(pageable);
} @Transactional(readOnly = true)
public long count() {
return count(new Filter[] {});
} @Transactional(readOnly = true)
public long count(Filter... filters) {
return baseDao.count(filters);
} @Transactional(readOnly = true)
public boolean exists(ID id) {
return baseDao.find(id) != null;
} @Transactional(readOnly = true)
public boolean exists(Filter... filters) {
return baseDao.count(filters) > 0;
} @Transactional
public T save(T entity) {
Assert.notNull(entity);
Assert.isTrue(entity.isNew()); baseDao.persist(entity);
return entity;
} @Transactional
public T update(T entity) {
Assert.notNull(entity);
Assert.isTrue(!entity.isNew()); if (!baseDao.isManaged(entity)) {
T persistant = baseDao.find(baseDao.getIdentifier(entity));
if (persistant != null) {
copyProperties(entity, persistant, UPDATE_IGNORE_PROPERTIES);
}
return persistant;
}
return entity;
} @Transactional
public T update(T entity, String... ignoreProperties) {
Assert.notNull(entity);
Assert.isTrue(!entity.isNew());
Assert.isTrue(!baseDao.isManaged(entity)); T persistant = baseDao.find(baseDao.getIdentifier(entity));
if (persistant != null) {
copyProperties(entity, persistant, (String[]) ArrayUtils.addAll(ignoreProperties, UPDATE_IGNORE_PROPERTIES));
}
return update(persistant);
} @Transactional
public void delete(ID id) {
delete(baseDao.find(id));
} @Transactional
public void delete(ID... ids) {
if (ids != null) {
for (ID id : ids) {
delete(baseDao.find(id));
}
}
} @Transactional
public void delete(T entity) {
if (entity != null) {
baseDao.remove(baseDao.isManaged(entity) ? entity : baseDao.merge(entity));
}
} /**
* 拷贝对象属性
*
* @param source
* 源
* @param target
* 目标
* @param ignoreProperties
* 忽略属性
*/
protected void copyProperties(T source, T target, String... ignoreProperties) {
Assert.notNull(source);
Assert.notNull(target); PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(target);
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String propertyName = propertyDescriptor.getName();
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
if (ArrayUtils.contains(ignoreProperties, propertyName) || readMethod == null || writeMethod == null || !baseDao.isLoaded(source, propertyName)) {
continue;
}
try {
Object sourceValue = readMethod.invoke(source);
writeMethod.invoke(target, sourceValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
} }