Spring数据存储库 - 性能问题

时间:2017-06-21 21:28:02

标签: spring hibernate jpa spring-data-jpa

我正在使用Spring,JPArepostories和hibernate将一些实体保存到数据库中。

我的实体:

用户:

@Entity
@Table(name = "users")

public class User {


@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "CARDID",unique=true)
private String cardId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name ="SUPPLIERUSERID", nullable = true)
@JsonIgnore
private SupplierUser supplierUser;
@Column(name = "NAME")
private String name;
@Column(name = "SURENAME")
private String sureName;
@Column(name = "ACTIVE")
private Boolean active;
@Column(name = "SMS")
private String sms;
@Column(name = "EMAIL")
private String email;

@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Box> boxList = new ArrayList<Box>();
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Notification> notificationList = new ArrayList<Notification>();



public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getCardId() {
    return cardId;
}
public void setCardId(String cardId) {
    this.cardId = cardId;
}
public SupplierUser getSupplierUser() {
    return supplierUser;
}
public void setSupplierUser(SupplierUser supplierUser) {
    this.supplierUser = supplierUser;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getSureName() {
    return sureName;
}
public void setSureName(String sureName) {
    this.sureName = sureName;
}
public Boolean getActive() {
    return active;
}
public void setActive(Boolean active) {
    this.active = active;
}
public String getSms() {
    return sms;
}
public void setSms(String sms) {
    this.sms = sms;
}
public List<Box> getBoxList() {
    return boxList;
}
public void setBoxList(List<Box> boxList) {
    this.boxList = boxList;
}
public List<Notification> getNotificationList() {
    return notificationList;
}
public void setNotificationList(List<Notification> notificationList) {
    this.notificationList = notificationList;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}


}

用户有框:

@Entity
@Table(name = "boxes")

public class Box {
    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name ="USERID", nullable = true)
    @JsonIgnore
    private User user;
    @Column(name = "BOXNUMBER",unique=true)
    private int boxNumber;
    @Column(name = "MODBUSADDRESS")
    private int modbusAddress;
    @Column(name = "MODBUSREGISTER")
    private int modbusRegister;
    @Column(name = "STATE")
    private String state;
    @OneToMany(mappedBy = "box", fetch =FetchType.EAGER,cascade = CascadeType.ALL)
    private List<Transaction> transactionsList = new ArrayList<Transaction>();

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public int getModbusAddress() {
        return modbusAddress;
    }
    public void setModbusAddress(int modbusAddress) {
        this.modbusAddress = modbusAddress;
    }
    public int getModbusRegister() {
        return modbusRegister;
    }
    public void setModbusRegister(int modbusRegister) {
        this.modbusRegister = modbusRegister;
    }

    public List<Transaction> getTransactionsList() {
        return transactionsList;
    }
    public void setTransactionsList(List<Transaction> transactionsList) {
        this.transactionsList = transactionsList;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public int getBoxNumber() {
        return boxNumber;
    }
    public void setBoxNumber(int boxNumber) {
        this.boxNumber = boxNumber;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

盒子有交易:

@Entity
@Table(name = "transactions")

public class Transaction {


@Id
@GeneratedValue
@Column(name = "ID")    
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name ="BOXID", nullable = true)
@JsonIgnore
private Box box;
@Column(name = "TYPE")  
private String type;
@Column(name = "SUPPLIERUSERCARDID")    
private String supplierUserCardId;
@Column(name = "DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@OneToMany(mappedBy = "transaction", fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List<Notification> notificationsList = new ArrayList<Notification>();

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public Box getBox() {
    return box;
}
public void setBox(Box box) {
    this.box = box;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}
public String getSupplierUserCardId() {
    return supplierUserCardId;
}
public void setSupplierUserCardId(String supplierUserCardId) {
    this.supplierUserCardId = supplierUserCardId;
}
public List<Notification> getNotificationsList() {
    return notificationsList;
}
public void setNotificationsList(List<Notification> notificationsList) {
    this.notificationsList = notificationsList;
}


}

交易有通知(通知也参考用户):

@Entity
@Table(name = "notifications")

public class Notification {
    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name ="TRANSACTIONID", nullable = true)
    @JsonIgnore
    private Transaction transaction;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name ="USERID", nullable = true)
    @JsonIgnore
    private User user;
    @Column(name = "TYPE")
    private String type;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED")
    private Date created;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "SENDED")
    private Date sended;
    @Column(name = "RETRIES")
    private Long retries;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public Date getSended() {
        return sended;
    }
    public void setSended(Date sended) {
        this.sended = sended;
    }
    public Long getRetries() {
        return retries;
    }
    public void setRetries(Long retries) {
        this.retries = retries;
    }
    public Transaction getTransaction() {
        return transaction;
    }
    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

}

我的问题是 - 我做错了什么,因为下面列出150个方框的方法需要大约20秒才能完成。

public void changeBoxOwners(ArrayList<Box> boxList){

        String id = theSoftwareCore.getSupplierUser().getCardId();
        ArrayList<Box> boxToSave = new ArrayList<Box>();

        for (Box box : boxList){        
                Box existingBox = theSoftwareCore.boxServiceImp.findByBoxNumber(box.getBoxNumber());
                existingBox.setState("full");
                User user = theSoftwareCore.userServiceImp.findOneByCardId(box.getUser().getCardId());                                                  
                //deleting not sent notifications
                for (Transaction trans : existingBox.getTransactionsList()){
                    for (Notification notif: trans.getNotificationsList()){
                        if (notif.getSended()==null){
                            notif.setSended(new Date(0));
                        }                       
                    }
                }
                Transaction transaction = new Transaction();
                transaction.setType("in");
                transaction.setSupplierUserCardId(id);          
                transaction.setDate(new Date());
                transaction.setBox(existingBox);
                Notification notification = new Notification();
                notification.setCreated(new Date());
                notification.setType("smsTakeYourStaff");
                notification.setTransaction(transaction);
                notification.setUser(user);
                existingBox.setUser(user);  
                transaction.getNotificationsList().add(notification);
                existingBox.getTransactionsList().add(transaction);             
                boxToSave.add(existingBox);
            }
            System.out.println("Start saving" + new Date());
            theSoftwareCore.boxServiceImp.saveAll(boxToSave);
            System.out.println("End " + new Date());

    }

感谢您的提前时间。

0 个答案:

没有答案