Greetings , at me such problem, I have noticed, that at use Hibernate, data are saved many times more slowly than at use JDBC. I have written an example, which puts in three tables 100, 1000 of objects (does 300, 3000 insert) and here results:
structure of tables:
Timestamp <-------> time_request <------> Request
Test a config.
JDK 1.6
HSQL
Windows Vista
100 objects
Hiberane 631ms
JDBC 7ms
1000 objects
Hiberane 5425ms
JDBC 56ms
As you can see, Hibernate many times more slowly. Whether a question it is possible to achieve more comprehensible results at hibernate.
The test project (the size ~6Mb) it is possible to download>>>
http://public.rz.fh-wolfenbuettel.de/~barvenko/HibernateVsJDBC/HibernateVsJDBC.zip<<<, it comprises all necessary, only to start and all. Start BD and GUI to it, is carried out through two *.bat a file laying in a folder lib.
Here so I save.
Code:
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.ejb.EntityManagerFactoryImpl;
import test.Request;
import test.RequestTime;
import test.TimeStamp;
public class MainTest {
private EntityManagerFactory emf;
private Session entityManager;
private Transaction tx2;
private int insert = 1000;
public MainTest() {
emf = Persistence.createEntityManagerFactory("test");
entityManager = (Session) emf.createEntityManager().getDelegate();
tx2 = entityManager.getTransaction();
start();
// saveWithNativeSQL();
}
private void start() {
tx2.begin();
ArrayList<RequestTime> liste = new ArrayList<RequestTime>();
for (int i = 0; i < insert; i++) {
TimeStamp timeStamp = new TimeStamp();
timeStamp.setDay((byte) (6 + i));
timeStamp.setHour((byte) (5 + i));
timeStamp.setMonth((byte) (7 - i));
Request request = new Request("XMLCON", "198.56.3.255", "wap");
RequestTime newLink = new RequestTime(666, timeStamp, request);
liste.add(newLink);
// List<RequestTime> criteria = entityManager.createCriteria(RequestTime.class).
// add( Restrictions.eq("request", request)).
// add(Restrictions.eq("timeStamp", timeStamp)).list();
}
long start = System.currentTimeMillis();
for (RequestTime time : liste) {
entityManager.save(time);
}
tx2.commit();
entityManager.close();
System.out.println("Done.\tExecuted time: "
+ (System.currentTimeMillis() - start));
}
private void saveWithNativeSQL() {
try {
Class.forName("org.hsqldb.jdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int[] updateCounts = null;
java.sql.Connection con = DriverManager.getConnection(
"jdbc:hsqldb:hsql://localhost/test", "sa","");
con.setAutoCommit(false);
long start = System.currentTimeMillis();
Statement s = con.createStatement();
for (int i = 1; i < insert; i++) {
s.addBatch("INSERT INTO Request VALUES (" + i
+ ",'aaa','aaa','aaa')");
s.addBatch("INSERT INTO TimeStamp VALUES (" + i + ",5,1,1,1)");
s.addBatch("INSERT INTO time_request VALUES (" + i + ",5," + i
+ "," + i + ")");
}
updateCounts = s.executeBatch();
con.commit();
System.out.println("Done.\tExecuted time: "
+ (System.currentTimeMillis() - start));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new MainTest();
}
}
So looks my config fileCode:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>test.TimeStamp</class>
<class>test.RequestTime</class>
<class>test.Request</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost/test" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<!-- <property name="hibernate.cache.provider_class"
value="org.hibernate.cache.EhCacheProvider" /> -->
<property name="hibernate.connection.username" value="sa" />
<!-- <property name="hibernate.connection.password" value="" />
<property name="hibernate.jdbc.batch_size" value="50" /> -->
<property name="hibernate.cache.use_second_level_cache"
value="false" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<!-- <property name="hibernate.show_sql" value="true" />
<property name="hibernate.max_fetch_depth" value="3" /> -->
</properties>
</persistence-unit>
</persistence>
This is my mapping filesCode:
/**
* (Start,[Via1,Via2,..Vian], Ziel)
*/
package test;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
/**
* @author Unkis
*
*/
@Entity
@Table(name = "Request")
public class Request implements Serializable {
private long id;
private Collection timeStamps;
private String type;
private String virtServer;
private String info;
public Request() {
}
public Request(Collection timeStamps, String type, String virtServer,
String info) {
super();
this.timeStamps = timeStamps;
this.type = type;
this.virtServer = virtServer;
this.info = info;
}
public Request(String type, String virtServer, String info) {
super();
this.type = type;
this.virtServer = virtServer;
this.info = info;
}
@Id
@Column(name = "request_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVirtServer() {
return virtServer;
}
public void setVirtServer(String virtServer) {
this.virtServer = virtServer;
}
@ManyToMany(targetEntity = TimeStamp.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "time_request", joinColumns = @JoinColumn(name = "request_id"), inverseJoinColumns = @JoinColumn(name = "time_id"))
public Collection getTimeStamps() {
return timeStamps;
}
public void setTimeStamps(Collection locations) {
this.timeStamps = locations;
}
}
Code:
package test;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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 = "time_request")
public class RequestTime implements Serializable {
private long id;
private Request request;
private TimeStamp timeStamp;
private int number;
public RequestTime() {
super();
// TODO Auto-generated constructor stub
}
public RequestTime(int number, TimeStamp timeStamps, Request request) {
super();
this.request = request;
this.timeStamp = timeStamps;
this.number = number;
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(nullable = false)
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "time_id", nullable = false, updatable = false)
public TimeStamp getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(TimeStamp timeStamp) {
this.timeStamp = timeStamp;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "request_id", nullable = false, updatable = false)
public Request getRequest() {
return request;
}
public void setRequest(Request request) {
this.request = request;
}
}
Code:
package test;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class TimeStamp implements Serializable {
// private Long id;
/**
* Id of location
*/
private Long time_id;
private Collection resuests;
private byte hour;
private byte day;
private byte month;
private short year;
public TimeStamp() {
super();
// TODO Auto-generated constructor stub
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "location_id")
public Long getTime_id() {
return time_id;
}
public void setTime_id(Long location_id) {
this.time_id = location_id;
}
public TimeStamp(Collection resuests, byte hour, byte day, byte month,
short year) {
super();
this.resuests = resuests;
this.hour = hour;
this.day = day;
this.month = month;
this.year = year;
}
public TimeStamp(byte hour, byte day, byte month, short year) {
super();
this.hour = hour;
this.day = day;
this.month = month;
this.year = year;
}
public byte getDay() {
return day;
}
public void setDay(byte day) {
this.day = day;
}
public byte getHour() {
return hour;
}
public void setHour(byte hour) {
this.hour = hour;
}
public byte getMonth() {
return month;
}
public void setMonth(byte month) {
this.month = month;
}
public short getYear() {
return year;
}
public void setYear(short year) {
this.year = year;
}
@ManyToMany(targetEntity = Request.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "time_request", joinColumns = @JoinColumn(name = "time_id"), inverseJoinColumns = @JoinColumn(name = "request_id"))
public Collection getResuests() {
return resuests;
}
public void setResuests(Collection resuests) {
this.resuests = resuests;
}
}