Hi I am new to Hibernate. I created a database (MySQL) table: member
create table member (
member_id int ( 6 ) unique not null AUTO_INCREMENT ,
screen_name varchar (50) not null unique,
password varchar (30) not null,
first_name varchar (30) not null ,
last_name varchar (30) not null ,
email_address varchar (100) not null ,
secondary_email_address varchar (100) ,
priviledge varchar ( 15 ) not null ,
phone varchar ( 20 )
);
And I am able to insert into it just fine. The problem occurs for me when I insert again, the system overwrites the original item :)
I have never had this happen with JDBC. Has anyone seen this sort of problem?
Hibernate version: 3.3
Mapping documents:
Member.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- Class: Event DBTable: collection -->
<class name="com.sj.subversivejewelry.data.Member" table="member">
<id name="memberId" column="member_id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<property name="memberName" column="screen_name" />
<property name="password" column="password" />
<property name="phone" column="phone" />
<property name="email" column="email_address" />
<property name="priviledge" column="priviledge" />
<property name="secondaryEmail" column="secondary_email_address" />
</class>
</hibernate-mapping>
Now hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/subversiveJ</property>
<property name="connection.username">root</property>
<property name="connection.password">sasha17</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>
<!-- 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>
<!-- 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>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="../classes/com/sj/subversivejewelry/data/SJCollection.hbm.xml"/>
<mapping resource="../classes/com/sj/subversivejewelry/data/Member.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My HibernateUtil.java
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(new File("/subversivejewelry/WEB-INF/hibernate.cfg.xml")).buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
And my insert method:
public Boolean insertMember ( Member member )
{
SessionFactory sessFac = HibernateUtil.getSessionFactory( );
Session session = sessFac.getCurrentSession( );
try
{
Transaction transaction = session.beginTransaction();
session.save ( member );
session.flush ( );
transaction.commit();
HibernateUtil.getSessionFactory ( ).close ( );
}
catch ( Exception e )
{
e.printStackTrace ( );
return Boolean.FALSE;
}
finally
{
session.close ( );
}
return Boolean.TRUE;
}
The generated SQL (show_sql=true):
Hibernate: insert into member (first_name, last_name, screen_name, password, phone, email_address, priviledge, secondary_email_address) values (?, ?, ?, ?, ?, ?, ?, ?)
I read the document on
http://hibernate.org/42.html about session and transaction management but I am still having trouble.
Can anyone explain to me why my data gets overwritten?
Thanks!
Alex