-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: My data being over-written
PostPosted: Fri Jun 20, 2008 5:13 am 
Newbie

Joined: Wed Jun 18, 2008 4:10 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 21, 2008 11:17 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi genadinik,

I think your problem is here:
Quote:
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
.

So, each time you restart your program, it will recreate the database!

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 21, 2008 11:20 am 
Newbie

Joined: Wed Jun 18, 2008 4:10 pm
Posts: 2
Yes definitely!

I found the solution yesterday night. It was exactly that. I changed the value to "update" instead of "create" but I am not sure if that is the optimal option and what "update" actually means for my webapp. Do you know what update does?

Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 3:03 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
genadinik wrote:
Do you know what update does?

Alex


RTF!

this is from the Tools docs:

Quote:
4.1.3.1. Database schema exporter (<hbm2ddl>)
<hbm2ddl> lets you run schemaexport and schemaupdate which generates the appropriate SQL DDL and allow
you to store the result in a file or export it directly to the database. Remember that if a custom naming strategy
is needed it is placed on the configuration element.
<hbm2ddl
export="true|false" (1)
update="true|false" (2)
drop="true|false" (3)
create="true|false" (4)
outputfilename="filename.ddl" (5)
delimiter=";" (6)
format="true|false" (7)
>
(1) export (default: true): Execute the generated statements against the database
(2) update(default: false): Try and create an update script representing the "delta" between what is in the database
and what the mappings specify. Ignores create/update attributes. (Do *not* use against production
databases, no guarantees at all that the proper delta can be generated nor that the underlying database
can actually execute the needed operations)
(3) drop (default: false): Output will contain drop statements for the tables, indices & constraints
(4) create (default: true): Output will contain create statements for the tables, indices & constraints
(5) outputfilename (Optional): If specified the statements will be dumped to this file.
(6) delimiter (default: ";"): What delimter to use to separate statements
(7) format (default: false): Apply basic formatting to the statements.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.