Hibernate Batch Processing插入1000000条记录

时间:2014-12-20 08:53:44

标签: java hibernate batch-processing

当我尝试在MySQL DB中插入1000000条记录时,我真的只有1000条。有什么问题?

  1. 这是我的hibernate.cfg.xml

    com.mysql.jdbc.Driver JDBC:MySQL的://本地主机:3307 / hibernatedb 根 bervol13

    1

    50

    org.hibernate.dialect.MySQLDialect

    螺纹

    org.hibernate.cache.NoCacheProvider< /性> - >



    org.hibernate.cache.EhCacheProvider

    更新

  2. 这是我的学生:

    package ua.berchik.vovchuk.dto;
    

    import java.io.Serializable;

    import javax.persistence.Cacheable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;

    import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy;

    @Cacheable
    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) @实体 @table(名称= “学生”) public class Student实现Serializable {

    private static final long serialVersionUID = -6853928080190944025L;
    
    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column(name="STUDENT_NAME")
    private String studName;
    @Column(name="ROLE_NUMBER")
    private int rollNumb;
    @Column(name="COURSE")
    private String course;
    
    public Student(){}
    
    public Student(String studName, int rollNumb, String course) {
        super();
        this.studName = studName;
        this.rollNumb = rollNumb;
        this.course = course;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getStudName() {
        return studName;
    }
    public void setStudName(String studName) {
        this.studName = studName;
    }
    public int getRollNumb() {
        return rollNumb;
    }
    public void setRollNumb(int rollNumb) {
        this.rollNumb = rollNumb;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    
    public String toString()  
     {  
      return "Id: "+this.id+" ROLL Number: "+this.rollNumb+"| Name: "+this.studName+"| Course: "+this.course;  
     } 
    

    }

  3. 这是我的HibernateUtil:

    package ua.berchik.vovchuk.hibernate.util;
    

    import org.hibernate.SessionFactory; import org.hibernate.annotations.common.annotationfactory.AnnotationFactory; import org.hibernate.cfg.AnnotationConfiguration;

    public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static{
        try{
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch(Throwable th){
            System.err.println("Initial SessionFactory creation failed"+th);
            throw new ExceptionInInitializerError(th);
        }
    }
    
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
    

    }

  4. 这是我的主要内容:

    包ua.berchik.vovchuk.hibernate;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.crypto.Data;
    
    import org.hibernate.Criteria;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.criterion.Restrictions;
    
    import ua.berchik.vovchuk.dto.FourWheeler;
    import ua.berchik.vovchuk.dto.Student;
    import ua.berchik.vovchuk.dto.TwoWheeler;
    import ua.berchik.vovchuk.dto.User;
    import ua.berchik.vovchuk.dto.UserDetails;
    import ua.berchik.vovchuk.dto.Vehicle;
    import ua.berchik.vovchuk.hibernate.util.HibernateUtil;
    
    public class HiernateTest {
    
        public static void main(String[] args) {
    
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
    
            for ( int i=0; i<1000000; i++ )  
            {  
                Student student = new Student("Name "+i, i, "Course "+i);  
                session.save(student); 
                if(i%50 == 0){
                    session.flush();
                    session.clear();
                }
            }  
    
            session.getTransaction().commit();
            session.close();
        }
    
    }
    

0 个答案:

没有答案