用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);
}
}
} }
												

hibernate写list到mysql的更多相关文章

  1. Robotframework使用自写库连接mysql数据库

    Robotframework使用自写库连接mysql数据库 新建库文件mysqltest.py 代码如下: # -*- coding: utf-8 -*- import MySQLdbimport o ...

  2. Hibernate原生SQL映射MySQL的CHAR(n)类型到String时出错

    今天在用Hibernate通过原生SQL和ResultTransformer映射时,出现数据类型不匹配的错误.但是通过Entity映射,没有问题.在网上找了好多答案,终于解决了. 核心代码: Stri ...

  3. Hibernate写配置文件无提示信息解决

    把Hibernate的相关jar包引入工程后,在配置hibernate.cfg.xml时没有提示信息,对于开发人员来说,那么多标签,标签有那么多属性,全部都记住显然是不可能的,遇到这种情况是很头疼的事 ...

  4. hibernate 解决诡异的mysql存入中文乱码

    使用hibernate查询mysql,通过bean的get方法拿到字符串再写入mysql中的字段会中文乱码,需要String string = xxx.get(),把get方法拿到的值传入到新的str ...

  5. 用if写一个备份mysql的脚本

    #!/bin/bash # 备份数据库 BAK_DIR=/data/backup/`date +%Y%m%d` MYSQLDB=dexin MYSQLUSER=root MYSQLPW=123456 ...

  6. Hibernate写hql语句与不写hql语句的区别?

    写hql语句与不写hql语句的区别? 写hql语句:书写HQL语句,所有的查询与投影的设计均使用HQL语句完成. 不写hql语句:没有任何查询语句,所有的查询与投影的设计使用面向对象格式完成. 二者选 ...

  7. STS 3.6.4 SpringMVC 4.1.6 Hibernate 4.3.8 MySQL

    开发环境: Java 1.8 Spring Tool Suite 3.6.4 Spring faramework 4.1.6 Hibernate 4.3.8 Maven 2.9 数据库是MySQL 5 ...

  8. hibernate保存数据到mysql时的中文乱码问题

    因为hibernate底层使用的是jdbc的技术,所以我参考了别人使用jdbc保存数据到mysql里面时解决乱码问题的方法! 首先要告诉数据库要插入的字符串的字符集,mysql 默认使用的字符集是 l ...

  9. python写的分析mysql binlog日志工具

    因为数据库增删改突然暴增,需要查询是那些表的操作特别频繁,写了一个用来分析bin-log的小工具,找出增删改查的表,并按照操作次数降序排列,以下是代码: 1 2 3 4 5 6 7 8 9 10 11 ...

随机推荐

  1. Opencv 图像读取与保存问题

    转自 @yhl_leo 1 图像读取 首先看一下,imread函数的声明: // C++: Mat based Mat imread( ); // C: IplImage based IplImage ...

  2. xyplorer设置备忘

    xyplorer设置备忘https://www.cnblogs.com/liuzhaoyzz/p/9911665.html1.双击向上一级工具→配置→菜单,鼠标,安全性→鼠标→双击空白空间时向上一级. ...

  3. 【linux】之日志查看

    搜索日志 -n 显示行号 grep 1570xxxx -n callback.tomcat-catalina-out 显示从第多少行~多少行 sed -n '464913,465020p' callb ...

  4. java8与函数编程资料

    Functional programming Java 8 idioms Java SE 8's new Streams API Spring 技术布道师 Josh Long 来华:用 Show 代码 ...

  5. Mysql-12条优化技巧

    应用程序慢如牛,原因多多,可能是网络的原因.可能是系统架构的原因,还有可能是数据库的原因. 那么如何提高数据库SQL语句执行速度呢?有人会说性能调优是数据库管理员(DBA)的事,然而性能调优跟程序员们 ...

  6. JavaScript数组方法--reduce、reduceRIght、reverse

    今天写的reduce是比较复杂的一个数组方法,其实在这之前我也用过reduce,可是每次用起来总感觉不那么顺手,主要还是因为不熟,对reduce本身不熟.首先reduce这个单词翻译为中文,不那么直观 ...

  7. echarts统计图Y轴(或X轴)文字过长问题解决

    echarts 统计图Y轴文字过长 在使用echarts时,出现数值非常大,Y轴又显示不下的情况就需要压缩Y轴数值刻度. 解决方法: yAxis: { type: 'value', axisLabel ...

  8. (python基础 函数)

    关键字参数:关键字参数和函数调用关系紧密,函数调用使用关键字参数来确定传入的参数值.使用关键字参数允许函数调用时参数的顺序与声明时不一致,因为 Python 解释器能够用参数名匹配参数值 必需参数:必 ...

  9. 团队作业-Week9-软件项目的风险

    一.软件项目中的风险 软件项目的风险无非体现在以下四个方面:需求.技术.成本和进度.IT项目开发中常见的风险有如下几类: (1)需求风险 ①需求已经成为项目基准,但需求还在继续变化: ②需求定义欠佳, ...

  10. c++数据类型漫谈

    在计算机眼里所有数据都是0101,二进制才是物理世界的主宰,c++的数据类型相对其他高级语言是相对较细的,因为是继承C而来,但是c++为什么要设计这么多数据类型呢?因为人类难以理解二进制,这就是数据类 ...