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

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
You're using an assigned generator yet your unsaved value is null. This doesn't make sense.

Use a native generator to generate a primary key for a new object and check the docs in this regard.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 5:17 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Using an assigned generator means you need to assign the primary key. In Java code. Otherwise use a different key generation approach.


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