将一组数据保留在Spring Framework中的惰性集合中

时间:2014-09-15 17:14:19

标签: java hibernate persistence spring-roo

ds_schema 我是春天的新手。上图显示了我的数据库架构:

我需要每分钟为每个电台下载一个新的实时数据,我必须将其分配给该电台。我想设法:

  1. 持续
  2. 没有使用FetchType.EAGER(导致这个目的太慢)
  3. 持续一次(即与db进行单次通信,而不在列表中迭代每个RealTimeData上的持久化函数)前一分钟收集的RealTimeData集合。
  4. 这就是我想做的事情,如果我采取完全不同的方式来做这样的事情,任何建议都会受到赞赏。

    站类是这样的:

    @RooJavaBean
    @RooToString
    @RooJpaActiveRecord(finders = { "findStationsByNum" })
    @RooJson
    public class Station {
    
        private String address;
        private String name;
        private Integer num;
        @ManyToOne(cascade = CascadeType.ALL)
        private Location location;
        @ManyToOne
        private City city;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "station", fetch=FetchType.LAZY)
        private Set<RealTimeData> real_time_data = new HashSet<RealTimeData>();
    
    }
    

    RealTimeData类是这样的:

    @RooJavaBean
    @RooToString
    @RooJpaActiveRecord(finders = { "findRealTimeDatasByCollect_dateEquals" })
    @RooJson
    public class RealTimeData {
    
        private Integer available_bike_stands;
        /* ... ... ... */
        @Temporal(TemporalType.TIMESTAMP)
        @DateTimeFormat(style = "M-")
        private Date collect_date;
        @ManyToOne
        private Station station;
    }
    

1 个答案:

答案 0 :(得分:0)

您可以在此处阅读解决方案的详细信息: http://forum.spring.io/forum/spring-projects/roo/106714-bulk-operations-with-spring-roo

然而,合成它建议使用它:

@PersistenceContext
private EntityManager entityManager;

@Transactional
public void storeList(List<MyEntity> entities) {
  int imported = 0;
  for (MyEntity e: entities) {
    entityManager.persist(e);
    if (++imported % 50 == 0) {
      entityManager.flush();
      entityManager.clear();
    }
  }
}

或者如果您使用的是Spring Roo:

@Transactional
public void storeList(List<MyEntity> entities) {
    int imported = 0;
    for (MyEntity e: entities) {
        e.persist();
        if (++imported % 50 == 0) {
            e.flush();
            e.clear();
        }
    }
}

在persistence.xml的属性中设置:

<property name="hibernate.jdbc.batch_size" value="50"/>
相关问题