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
|