-->
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.  [ 8 posts ] 
Author Message
 Post subject: Saving to Database
PostPosted: Wed Feb 11, 2004 9:41 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
I've met a problem regarding saving to database. When I save a new object to my database, it overrides the current content of the database. It's probably incredibly stupid problem, but I've been stuck with this for a couple of days now..

Here are some of my code:


public static void saveToDatabase(User user) throws HibernateException
{
Session session = HibernateUtilities.currentSession();
Transaction tx = session.beginTransaction();
session.save(user);
session.flush();
tx.commit();
HibernateUtilities.closeSession();
}

Hibernate.properties
#define query langugage constants/function names
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'

##PLATFORM
#MySQL

hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///avvik
hibernate.connection.username dag
hibernate.connection.password password


#Hibernate Connection Pool
hibernate.connection.pool_size 5

###################################
### Apache DBCP Connection Pool ###
###################################

## connection pool

hibernate.dbcp.maxActive 100
hibernate.dbcp.whenExhaustedAction 1
hibernate.dbcp.maxWait 120000
hibernate.dbcp.maxIdle 10

## prepared statement cache

hibernate.dbcp.ps.maxActive 100
hibernate.dbcp.ps.whenExhaustedAction 1
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 10

## optional query to validate pooled connections:

#hibernate.dbcp.validationQuery select 1 from dual
#hibernate.dbcp.testOnBorrow true
#hibernate.dbcp.testOnReturn false

#Proxool Connection Pool
hibernate.proxool.pool_alias pool1


##MISCELLANEOUS SETTINGS
#Show SQL
hibernate.show_sql true

#auto shema export
hibernate.hbm2ddl.auto create

#set the the maximum JDBC 2 batch size (a nonzero value enables batching)
hibernate.jdbc.batch_size 0

#use streams when writing binary types to/from JDBC
hibernate.jdb.use_streams_for_binary true

#Set the maximum depth of the outer join fetch tree
hibernate.max_fetch_depth 1

#enable the query cache
hibernate.cache.use_query_cache true

#cache implementation
hibernate.cache.provider_class net.sf.ehcache.hibernate.Provider


Person.hbm.xml:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="people.Person"
table="person"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="P"
>

<id
name="personID"
column="personID"
type="long"
unsaved-value="null"
>
<generator class="assigned">
</generator>
</id>

<discriminator
column="subclass"
type="character"
/>

<property
name="name"
type="string"
update="true"
insert="true"
column="name"
length="60"
/>

<property
name="address"
type="string"
update="true"
insert="true"
column="address"
length="100"
/>

<property
name="phoneNo"
type="long"
update="true"
insert="true"
column="phoneNo"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Person.xml
containing the additional properties and place it in your merge dir.
-->
<subclass
name="people.User"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="U"
>
<property
name="department"
type="string"
update="true"
insert="true"
column="department"
length="50"
/>

<property
name="priviliges"
type="string"
update="true"
insert="true"
column="priviliges"
length="20"
/>

<property
name="position"
type="string"
update="true"
insert="true"
column="position"
length="30"
/>

<property
name="password"
type="string"
update="true"
insert="true"
column="password"
length="30"
/>

<property
name="emailAddress"
type="string"
update="true"
insert="true"
column="emailAddress"
length="40"
/>

<property
name="userName"
type="string"
update="true"
insert="true"
column="userName"
length="20"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->

</subclass>

</class>

</hibernate-mapping>

I get no Exceptions, but as i said; each time I add new objects to the database, it overrides the current contents.. I hope someone can help me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 10:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Most likely your unsaved-value mapping is wrong. Are you using a primitive type id?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:35 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
Thanx

But I still have the problem of overriding existing data..

Here is my new id-tag in Person.hbm.xml:

<id
name="personID"
column="personID"
type="long"
unsaved-value="-1"
>
<generator class="identity">
</generator>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I can hardly believe that, save should allways generate an INSERT. Have you really declared your column as identity, eg. auto-increment? Look at the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
And of course you've set your new object id to -1

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Still stupid
PostPosted: Wed Feb 11, 2004 12:34 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
I know!! It's probably a elemantary flaw. The SQL output looks like this:

Hibernate: insert into person (department, priviliges, position, password, emailAddress, userName, name, address, phoneNo, subclass, personID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, 'U', ?)

Hibernate: insert into person (department, priviliges, position, password, emailAddress, userName, name, address, phoneNo, subclass, personID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, 'U', ?)


I don't know why hibernate prints question marks except for on my discriminator-value, but at least the real values appear in the table.
I get two objects in to the database, but when i run main again, with different objects, it overriders the existing objects..

This is my class with a static method to get a Session-object, taken from some hibernate example. Is it ok?


package utilities;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;


public class HibernateUtilities {

private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}

public static final ThreadLocal local = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session session = (Session) local.get();
// Open a new Session, if this Thread has none yet
if (session == null) {
session = sessionFactory.openSession();
local.set(session);
}
return session;
}

public static void closeSession() throws HibernateException
{
Session session = (Session) local.get();
local.set(null);
if (session != null)
session.close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm pretty sure we gave you the answer, read the docs / FAQ and reread this thread.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 1:49 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
Sorry guys

I had my hibernate.hbm2ddl.auto set to create in hibernate.properties, so we created a new table each time.. pretty dumb huh..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.