测试数据库:ORACLE 11g
先测试一下,数据自己生成唯一ID的情况
domain:
hibernate:
jdbc:
测试1000条数据在两种方式的运行时间(毫秒):
hibernate 1500
jdbc 20
从上图可以看出,速度相差75倍。
结论:
当使用数据库生成ID时,JDBC在数据插入方面较HIBERNATE有较大优势。
接下来测试一下,自己生成唯一ID的情况
domain:
hibernate:
jdbc:
测试1000条数据在两种方式的运行时间(毫秒):
hibernate 65
jdbc 25
从上图可以看出,速度相差3倍。
结论:
当使用自己生成ID时,JDBC在数据插入方面较HIBERNATE的优势并不是很明显。
先测试一下,数据自己生成唯一ID的情况
domain:
@Entity
@Table(name = "test")
@GenericGenerator(name = "SEQ_Name", strategy = "sequence", parameters = {@Parameter(name = "sequence", value = "seq_test")})
public class Test {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_Name")
@Column
private Long id;
@Column
private int value;
@Column
private String type;
public Test() {
}
public Test(int value) {
this.value = value;
}
public Test(Long id, int value) {
this.id = id;
this.value = value;
}
public Test(int value, String type) {
this.value = value;
this.type = type;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
hibernate:
long start = System.currentTimeMillis();
for (int i = 0; i < DATA_COUNT; i++) {
getHibernateTemplate().save(new Test(12345678, getType()));
}
System.out.println("saveBatch\t\t" + (System.currentTimeMillis() - start));
jdbc:
PreparedStatement statement = connection.prepareStatement("insert into test (type, value) values (?, ?)");
for (int i = 0; i < DATA_COUNT; i++) {
statement.setString(1, getType());
statement.setInt(2, 12345678);
statement.addBatch();
}
statement.executeBatch();
测试1000条数据在两种方式的运行时间(毫秒):
hibernate 1500
jdbc 20
从上图可以看出,速度相差75倍。
结论:
当使用数据库生成ID时,JDBC在数据插入方面较HIBERNATE有较大优势。
接下来测试一下,自己生成唯一ID的情况
domain:
@Entity
@Table(name = "test2")
public class Test2 {
@Id
@Column(name = "id")
private Long id;
@Column
private int value;
@Column
private String type;
public Test2(long id, int value, String type) {
this.id = id;
this.value = value;
this.type = type;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
hibernate:
long start = System.currentTimeMillis();
for (int i = 0; i < DATA_COUNT; i++) {
getHibernateTemplate().save(new Test2(IdGenerator.getId(), 12345678, getType()));
}
System.out.println("saveBatch2\t\t" + (System.currentTimeMillis() - start));
jdbc:
PreparedStatement statement = connection.prepareStatement("insert into test2 (type, value, id) values (?, ?, ?)");
for (int i = 0; i < DATA_COUNT; i++) {
statement.setString(1, getType());
statement.setInt(2, 12345678);
statement.setLong(3, IdGenerator.getId());
statement.addBatch();
}
statement.executeBatch();
测试1000条数据在两种方式的运行时间(毫秒):
hibernate 65
jdbc 25
从上图可以看出,速度相差3倍。
结论:
当使用自己生成ID时,JDBC在数据插入方面较HIBERNATE的优势并不是很明显。