如何在hibernate框架中插入新行?

时间:2014-08-25 11:40:05

标签: java mysql sql hibernate

我是hibernate的新手。通过在线教程练习。我的问题是,我调用session.save(obj)方法,它将数据保存在表格中,它会删除以前的数据所以每次我用我的程序插入它都会删除前一行并插入新值所以我只有一行。我怎样才能克服这个问题,这是我的xml映射文件,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.bean.User" table="USER">
    <id column="ID" name="id" type="java.lang.Integer" >
    <generator class="increment" />
    </id>
    <property column="USER_NAME" name="userName" type="java.lang.String" />
    <property column="PASSWORD" name="password1" type="string" />
    <property column="EMAIL" name="email" type="java.lang.String" />
    <property column="PHONE" name="phone" type="java.lang.String" />
    <property column="CITY" name="city" type="java.lang.String" />
</class>
</hibernate-mapping>

在servlet中,我将save方法称为

        Configuration configuration = new Configuration().configure();

        // 2. create sessionfactory
        SessionFactory sessionFactory = configuration.buildSessionFactory();

        // 3. Get Session object
        Session session = sessionFactory.openSession();

        // 4. Starting Transaction
        Transaction transaction = session.beginTransaction();
        User user = new User();
        user.setUserName(userName);
        user.setPassword1(password);
        user.setEmail(email);
        user.setCity(city);
        user.setPhone(phone);
        session.save(user);
        transaction.commit();
        System.out.println("\n\n Details Added \n");

我的hibernate映射文件是这样的,

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <!-- Names the annotated entity class -->

    <mapping resource="com/hibernate/bean/user.hbm.xml"/>

</session-factory>

我想逐个插入行,我怎么能实现这一点,任何人都可以帮助我。

2 个答案:

答案 0 :(得分:2)

 <!-- Drop and re-create the database schema on startup -->
   <property name="hbm2ddl.auto">create</property>

这会在启动时重新创建数据库。

答案 1 :(得分:1)

<property name="hbm2ddl.auto">create</property>告诉Hibernate 创建所需的数据库表和列当读取配置文件时,所以基本上在每个应用程序启动时。为了不每次都重新创建数据库表,只需删除此行,或者如果要在开发时频繁更改所需的表和列,则删除此行,将create替换为update并且Hibernate仅添加新的如果您更改实体但不重新创建表,则会保留插入的行。