Reputation: 1324
I am new to hibernate. Practicing by seeing online tutorials. My problem is, I call session.save(obj)
method, it saves data in table bur it erases previous data So any time I made insert with my program it erases previous row and insert new values So that I have only one row . How can I overcome this, here is my xml mapping file,
<?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>
In servlet I call save
method as,
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");
my hibernate mapping file is like this,
<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>
I want to insert rows one by one how can I achieve this can anyone help me please.
Upvotes: 1
Views: 2334
Reputation: 1054
The line <property name="hbm2ddl.auto">create</property>
tells Hibernate to create the required database tables and columns when the config file is read, so basically on every application startup. To have your database tables not being recreated every time, just remove this line or even better if you are going to change the needed tables and columns frequently while developing, replace create
with update
and Hibernate only adds new columns if you change the Entity but it doesn't recreate the tables, hence your inserted rows will be kept.
Upvotes: 1
Reputation: 1333
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
this recreates db on startup.
Upvotes: 2