码迷,mamicode.com
首页 > 其他好文 > 详细

造数据

时间:2017-11-29 20:51:56      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:修改   begin   local   sources   mapping   pojo   span   color   www.   

一、 工具

  利用Hibernate 生成数据,现有一条数据,进修改部分字段,进行复制。

二、 代码

  hibernate.cfg.xml  在resources 目录下

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    
    <session-factory >
         <!-- Mysql  -->
           <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">puriew</property>
        <property name="connection.password">puriew</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="com.cyd.helloworld.gateways.Gateways"/>
    </session-factory>
</hibernate-configuration>

pojo 实现clone 接口

@Table(name = "wd_iot_eqm_gateways")
@Entity
public class Gateways implements Cloneable {
    @Id
    private String id;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Gateways stu = null;
        try {
            stu = (Gateways) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return stu;
    }
}

Main

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class UpdataGateway {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Transaction tx = null;
        Session session = null;
        SessionFactory sf = null;
        ServiceRegistry serviceRegistry = null;
        try {

            Configuration cfg = new Configuration().configure();
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            sf = cfg.buildSessionFactory(serviceRegistry);
            session = sf.openSession();
            Gateways g=(Gateways) session.get(Gateways.class, "9B1CDEAA3BCD4B79B805A52E208B8572");
            tx = session.beginTransaction();
            Gateways way=null;
            for (int i = 2000; i < 3000; i++) {
                way=(Gateways) g.clone();
                way.setId("120020170626"+i);
                way.setCODE("120020170626"+i);
                session.save(way);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            if (sf != null) {
                sf.close();
            }
        }

    }
}

 

造数据

标签:修改   begin   local   sources   mapping   pojo   span   color   www.   

原文地址:http://www.cnblogs.com/litblank/p/7922333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!