使用映射文件的hibernate auto increment id MySql

时间:2015-07-22 15:46:35

标签: mysql hibernate increment

我使用eclipse indigo,hibernate 3.6.4和MySQL 5.1.36运行简单的hibernate应用程序。我是hibernate的新手。请帮助我。

以下代码显示插入数据

public void addUserDetails(String userName, String password, String email,
            String phone, String city) {
        try {
            // 1. configuring hibernate
            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");

        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            System.out.println("error");
        }

    }
}

通过使用它,插入数据但每次只是替换第1行中的数据。

以下代码显示了数据库列和变量的映射文件。

<?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.milind.dto.User" table="USER">
        <id column="ID" name="id" type="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>

在MySQL中,它将Id列显示为自动增量,但不会发生增量。

运行应用程序后显示

跟随consol

Jul 22, 2015 8:58:23 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/test] has started
Jul 22, 2015 8:58:23 PM org.apache.catalina.loader.WebappClassLoader    clearReferencesJdbc
SEVERE: The web application [/test] registered the JDBC driver    [com.mysql.jdbc.Driver] but failed to unregister it when the web application was  stopped. To prevent a memory leak, the JDBC Driver has been forcibly  unregistered.
Jul 22, 2015 8:58:23 PM org.apache.catalina.loader.WebappClassLoader   clearReferencesThreads
SEVERE: The web application [/test] appears to have started a thread named   [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.
Jul 22, 2015 8:58:23 PM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable   debug logging for this logger for a complete list of JARs that were scanned but   no TLDs were found in them. Skipping unneeded JARs during scanning can improve   startup time and JSP compilation time.
Jul 22, 2015 8:58:23 PM org.apache.catalina.core.StandardContext reload 
INFO: Reloading Context with name [/test] is completed
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further   details.
Hibernate: 
    select
        max(ID) 
    from
        USER
Hibernate: 
    insert 
     into
        USER
        (USER_NAME, PASSWORD, EMAIL, PHONE, CITY, ID) 
    values
        (?, ?, ?, ?, ?, ?)


  Details Added 

这是hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright 
    (c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the 
    @author tags or express copyright attribution ~ statements applied by the 
    authors. All third-party contributions are ~ distributed under license by 
    Red Hat Inc. ~ ~ This copyrighted material is made available to anyone  wishing 
    to use, modify, ~ copy, or redistribute it subject to the terms and  conditions 
    of the GNU ~ Lesser General Public License, as published by the Free Software 
Foundation. ~ ~ This program is distributed in the hope that it will be useful, 
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
License ~ for more details. ~ ~ You should have received a copy of the GNU 
Lesser General Public License ~ along with this distribution; if not, write 
to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ 
Boston, MA 02110-1301 USA -->
  <?xml version='1.0' encoding='utf-8'?>
  <!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property   name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3535/students</property>

    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">qwera</property>

    <property name="hibernate.connection.autocommit">true</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

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

    <property name="show_sql">true</property>

    <!-- Mapping files -->
    <mapping class="com.milind.hibernate.dto.UserDetails" />


</session-factory>
</hibernate-configuration>

谢谢。

0 个答案:

没有答案