hibernate auto increment id MySql using mapping file -
i running simple hibernate application using eclipse indigo,hibernate 3.6.4 , mysql 5.1.36. new hibernate.so please me.
following code shows inserting data
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. 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"); } } }
by using this,data inserted every time replace data in 1st row.
following code shows mapping file database columns , variables.
<?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>
in mysql shows id column auto increment increment not happening.
after running application shows
following in consol
jul 22, 2015 8:58:23 pm org.apache.catalina.core.standardcontext reload info: reloading context name [/test] has started jul 22, 2015 8:58:23 pm org.apache.catalina.loader.webappclassloader clearreferencesjdbc severe: web application [/test] registered jdbc driver [com.mysql.jdbc.driver] failed unregister when web application stopped. prevent memory leak, jdbc driver has been forcibly unregistered. jul 22, 2015 8:58:23 pm org.apache.catalina.loader.webappclassloader clearreferencesthreads severe: web application [/test] appears have started thread named [abandoned connection cleanup thread] has failed stop it. create memory leak. jul 22, 2015 8:58:23 pm org.apache.catalina.startup.tldconfig execute info: @ least 1 jar scanned tlds yet contained no tlds. enable debug logging logger complete list of jars scanned no tlds found in them. skipping unneeded jars during scanning can improve startup time , jsp compilation time. jul 22, 2015 8:58:23 pm org.apache.catalina.core.standardcontext reload info: reloading context name [/test] completed slf4j: failed load class "org.slf4j.impl.staticloggerbinder". slf4j: defaulting no-operation (nop) logger implementation slf4j: see http://www.slf4j.org/codes.html#staticloggerbinder further details. hibernate: select max(id) user hibernate: insert user (user_name, password, email, phone, city, id) values (?, ?, ?, ?, ?, ?) details added
here hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!-- ~ hibernate, relational persistence idiomatic java ~ ~ copyright (c) 2010, red hat inc. or third-party contributors ~ indicated @author tags or express copyright attribution ~ statements applied authors. third-party contributions ~ distributed under license red hat inc. ~ ~ copyrighted material made available wishing use, modify, ~ copy, or redistribute subject terms , conditions of gnu ~ lesser general public license, published free software foundation. ~ ~ program distributed in hope useful, ~ without warranty; without implied warranty of merchantability ~ or fitness particular purpose. see gnu lesser general public license ~ more details. ~ ~ should have received copy of gnu lesser general public license ~ along 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 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>
thank you.
Comments
Post a Comment