World.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package models;
  2. import java.util.List;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.Id;
  6. import io.ebean.DB;
  7. import io.ebean.Finder;
  8. import io.ebean.Model;
  9. import io.ebean.Transaction;
  10. @Entity
  11. public class World extends Model {
  12. private static final Finder<Long, World> find = new Finder<>(World.class);
  13. @Id
  14. public Long id;
  15. @Column(name = "randomNumber")
  16. public Long randomNumber;
  17. public static World find(final Long id) {
  18. return find.byId(id);
  19. }
  20. public static List<World> save(final List<World> worlds) {
  21. final int batchSize = 25;
  22. final int batches = ((worlds.size() / batchSize) + 1);
  23. for ( int i = 0 ; i < batches ; ++i ) {
  24. final Transaction transaction = DB.getDefault().beginTransaction();
  25. try {
  26. transaction.setBatchMode(true);
  27. transaction.setBatchSize(batchSize);
  28. for(int j = i * batchSize ; j < Math.min((i + 1) * batchSize, worlds.size()); ++j) {
  29. DB.getDefault().update(worlds.get(j), transaction);
  30. }
  31. transaction.commit();
  32. } finally {
  33. transaction.end();
  34. }
  35. }
  36. return worlds;
  37. }
  38. }