1. jpa写下面语句执行报错,估计要先手动转成字符串吧,工作忙没继续下去了。
    public void persist(Goods goods) {
    Assert.notNull(goods);
    // goods.setId(new Long(1));
    List<GoodsItem> goodsItemList = goods.getGoodsItemList();
  2.  
  3. String jpql ="update Goods goods set goods.goodsItemList = :p1"+
    "where goods.id =1";
    entityManager.createQuery(jpql).setParameter("p1",goodsItemList).executeUpdate();
    }
    看到有直接保存一个类实例的方法,直接拿来用了。
  1. @Transactional
    public T update(T entity, String... ignoreProperties) {
    Assert.notNull(entity);
    Assert.isTrue(!entity.isNew());
    Assert.isTrue(!baseDao.isManaged(entity));
  2.  
  3. T persistant = baseDao.find(baseDao.getIdentifier(entity));
    if (persistant != null) {
    copyProperties(entity, persistant, (String[]) ArrayUtils.addAll(ignoreProperties, UPDATE_IGNORE_PROPERTIES));
    }
    return update(persistant);
    }
    /********************************一下为原类*********************************/
  1. @Transactional
    public abstract class BaseService<T extends BaseEntity<ID>, ID extends Serializable>{
  2.  
  3. /** 更新忽略属性 */
    private static final String[] UPDATE_IGNORE_PROPERTIES = new String[] { BaseEntity.CREATE_DATE_PROPERTY_NAME, BaseEntity.MODIFY_DATE_PROPERTY_NAME, BaseEntity.VERSION_PROPERTY_NAME };
  4.  
  5. /** BaseDao */
    private BaseDao<T, ID> baseDao;
  6.  
  7. @Autowired
    protected void setBaseDao(BaseDao<T, ID> baseDao) {
    this.baseDao = baseDao;
    }
  8.  
  9. @Transactional(readOnly = true)
    public T find(ID id) {
    return baseDao.find(id);
    }
  10.  
  11. @Transactional(readOnly = true)
    public List<T> findAll() {
    return findList(null, null, null, null);
    }
  12.  
  13. @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;
    }
  14.  
  15. @Transactional(readOnly = true)
    public List<T> findList(Integer count, List<Filter> filters, List<Order> orders) {
    return findList(null, count, filters, orders);
    }
  16.  
  17. @Transactional(readOnly = true)
    public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) {
    return baseDao.findList(first, count, filters, orders);
    }
  18.  
  19. @Transactional(readOnly = true)
    public Page<T> findPage(Pageable pageable) {
    return baseDao.findPage(pageable);
    }
  20.  
  21. @Transactional(readOnly = true)
    public long count() {
    return count(new Filter[] {});
    }
  22.  
  23. @Transactional(readOnly = true)
    public long count(Filter... filters) {
    return baseDao.count(filters);
    }
  24.  
  25. @Transactional(readOnly = true)
    public boolean exists(ID id) {
    return baseDao.find(id) != null;
    }
  26.  
  27. @Transactional(readOnly = true)
    public boolean exists(Filter... filters) {
    return baseDao.count(filters) > 0;
    }
  28.  
  29. @Transactional
    public T save(T entity) {
    Assert.notNull(entity);
    Assert.isTrue(entity.isNew());
  30.  
  31. baseDao.persist(entity);
    return entity;
    }
  32.  
  33. @Transactional
    public T update(T entity) {
    Assert.notNull(entity);
    Assert.isTrue(!entity.isNew());
  34.  
  35. if (!baseDao.isManaged(entity)) {
    T persistant = baseDao.find(baseDao.getIdentifier(entity));
    if (persistant != null) {
    copyProperties(entity, persistant, UPDATE_IGNORE_PROPERTIES);
    }
    return persistant;
    }
    return entity;
    }
  36.  
  37. @Transactional
    public T update(T entity, String... ignoreProperties) {
    Assert.notNull(entity);
    Assert.isTrue(!entity.isNew());
    Assert.isTrue(!baseDao.isManaged(entity));
  38.  
  39. T persistant = baseDao.find(baseDao.getIdentifier(entity));
    if (persistant != null) {
    copyProperties(entity, persistant, (String[]) ArrayUtils.addAll(ignoreProperties, UPDATE_IGNORE_PROPERTIES));
    }
    return update(persistant);
    }
  40.  
  41. @Transactional
    public void delete(ID id) {
    delete(baseDao.find(id));
    }
  42.  
  43. @Transactional
    public void delete(ID... ids) {
    if (ids != null) {
    for (ID id : ids) {
    delete(baseDao.find(id));
    }
    }
    }
  44.  
  45. @Transactional
    public void delete(T entity) {
    if (entity != null) {
    baseDao.remove(baseDao.isManaged(entity) ? entity : baseDao.merge(entity));
    }
    }
  46.  
  47. /**
    * 拷贝对象属性
    *
    * @param source
    * 源
    * @param target
    * 目标
    * @param ignoreProperties
    * 忽略属性
    */
    protected void copyProperties(T source, T target, String... ignoreProperties) {
    Assert.notNull(source);
    Assert.notNull(target);
  48.  
  49. 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);
    }
    }
    }
  50.  
  51. }
  1.  

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. javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX

    javax.el.PropertyNotFoundException: Property 'XXX' not found on type bean.XXXXX 先检查页面语法是否有问题,后在页面的el ...

  2. [总结] Synchronized汇总

    Java中的每一个对象都可以作为锁. 1对于同步方法,锁是当前实例对象. 2对于静态同步方法,锁是当前对象的Class对象. 3对于同步方法块,锁是Synchonized括号里配置的对象. 当一个线程 ...

  3. h5互动课件动画如何实现?如何快速开发h5互动课件动画

    最近几年随着h5的兴起,复杂的h5动画,甚至是交互动画类型的产品不断涌现,尤其在课件产品方面,很多公司都有相关需求,最近很多h5开发工程师想了解相关方面的技术. 针对h5,如果是简单的动画效果,可以考 ...

  4. 工控随笔_07_西门子_WinCC利用命令行实现操作log日志

    在WinCC中可以通过报警纪录来实现操作员纪录,这个需要WinCC的消息系统进行组态和配置. 利用消息系统进行实现上诉功能不但复杂而且时间久啦也不方便查询.那么有没有一种简单的方法来 实现操作员纪录呢 ...

  5. primo驱动启动顺序

    primo驱动启动顺序HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ServiceGroupOrderSystem ReservedEMSWdfLoa ...

  6. Docker安装使用battery historian

    apt-get insatll docker.io battery historian ubuntu下使用 首先要确保是google浏览器,然后用命令行 google-chrome --proxy-s ...

  7. [转][ActiveMQ]Apache.NMS.ActiveMQ 用法

    下载 C# 组件:http://archive.apache.org/dist/activemq/apache-nms/1.7.0/ 使用说明:https://www.cnblogs.com/cjm1 ...

  8. MySQL数据库事务各隔离级别加锁情况--read committed && MVCC

    之前已经转载过几篇相关的文章,此次基于mysql 5.7 版本,从测试和源码角度解释一下RR,RC级别为什么看到的数据不一样 先补充一下基础知识 基本知识 假设对于多版本(MVCC)的基础知识,有所了 ...

  9. Windows10中使用Anaconda安装keras-gpu版本(遇到的坑)

    1.使用conda install tensorflow-gpu 2.使用pip install keras 这里使用pip安装而不是使用conda,原因是使用conda安装会默认安装cpu版本的te ...

  10. WKWebView使用方法

    基本使用方法 WKWebView有两个delegate,WKUIDelegate 和 WKNavigationDelegate.WKNavigationDelegate主要处理一些跳转.加载处理操作, ...