有没有一种使用Hibernate进行批量插入或更新记录的方法?

huangapple go评论76阅读模式
英文:

Is there a way for bulk insert or update of records using Hibernate

问题

我正尝试使用Hibernate在Oracle中插入或更新少量记录。我大约有2000个实体(记录),我目前是逐个顺序地进行插入/更新。由于Hibernate内部会进行选择然后执行插入/更新操作,这需要我花费很多时间才能完成。我想知道是否有一种方式可以在Hibernate中进行批量插入/更新。如果是的话,请告诉我如何操作。还请告诉我在执行批量操作时是否存在任何瓶颈。

以下是我使用Hibernate进行保存/更新的方式:

@Autowired
private SessionFactory session;

private Session getSession() {
    return session.getCurrentSession();
}

@Override
public Integer saveOrUpdateTest(Test test) {
    getSession().saveOrUpdate(test);
    return test.getTestId();
}

以下是我使用的实体类:

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="Test")
public class Test implements Serializable {

private static final long serialVersionUID = 8844487972789619819L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="TEST_ID")
private Integer testid;

@Column(name="EMPLOYEE_ID")
private Integer employee_ID;

@Column(name="EMPLOYEE_SUB_ID")
private Integer employee_Sub_ID;

public Integer getTestId() {
    return testid;
}

public void setTestId(Integer testid) {
    this.testid = testid;
}

public Integer getEmployeeId() {
    return employee_ID;
}

public void setEmployeeId(Integer employee_ID) {
    this.employee_ID = employee_ID;
}

public Integer getEmployeeSubId() {
    return employee_Sub_ID;
}

public void setCapEmployeeSubId(Integer employee_Sub_ID) {
    this.employee_Sub_ID = employee_Sub_ID;
}

}
英文:

I am trying to insert or update few records in oracle using hibernate. I am having around 2000 entities (records), which i am currently inserting/updating one by one sequentially. this is taking a lot of time for me to complete as hibernate internally selecting and then doing insert/update. I want to know is there a way that i can do bulk insert/update in hibernate. If yes, please do let me know how i can do that. and also do let me know are there any bottlenecks in doing bulk operations.
below is the way how i am doing save/update using hibernate

@Autowired
private SessionFactory session;
private Session getSession() {
return session.getCurrentSession();
}
@Override
public Integer saveOrUpdateTest(Test test) {
getSession().saveOrUpdate(test);
return test.getTestId();
}

Below is the entity class that i am using.

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="Test")
public class Test implements Serializable{
private static final long serialVersionUID = 8844487972789619819L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="TEST_ID")
private Integer testid;
@Column(name="EMPLOYEE_ID")
private Integer employee_ID;
@Column(name="EMPLOYEE_SUB_ID")
private Integer employee_Sub_ID;
public Integer getTestId() {
return testid;
}
public void setTestId(Integer testid) {
this.testid = testid;
}
public Integer getEmployeeId() {
return employee_ID;
}
public void setEmployeeId(Integer employee_ID) {
this.employee_ID = employee_ID;
}
public Integer getEmployeeSubId() {
return employee_Sub_ID;
}
public void setCapEmployeeSubId(Integer employee_Sub_ID) {
this.employee_Sub_ID = employee_Sub_ID;
}
}

答案1

得分: 1

这里的链接 [here](https://stackoverflow.com/questions/7349464/bulk-insert-or-update-with-hibernate "Hibernate批量插入和更新")!这对于您的情况会起作用。

英文:

this [here](https://stackoverflow.com/questions/7349464/bulk-insert-or-update-with-hibernate "Hibernate Bulk Insertion ")!
This will work for your Scenario.

huangapple
  • 本文由 发表于 2020年10月6日 16:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定