-->
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.  [ 5 posts ] 
Author Message
 Post subject: data not persistent in postgres
PostPosted: Thu Feb 19, 2009 11:11 am 
Newbie

Joined: Tue Aug 07, 2007 8:43 am
Posts: 4
i have a problem where everything seems to be ok, no exceptions are thrown but data is not written in database.
i've used Session.load() to verify that i can normally access data already in database (so configuration should be ok) and SQLQuery to avoid problems with mapping.

also, i've tried replacing postgres with mysql and everything worked as expected in mysql. is there some additional configuration required for this to work in postgres?

Aljosa

hibernate.cfg.xml:
Code:
<?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>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost/pgtest</property>
    <property name="hibernate.connection.username">pgtest</property>
    <property name="hibernate.connection.password">pgtest</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="pgtest/Pgtest.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


Postgres:
Code:
postgres=# create database pgtest;
CREATE DATABASE
postgres=# \c pgtest
You are now connected to database "pgtest".
pgtest=# CREATE SEQUENCE pgtest_seq;
CREATE SEQUENCE
pgtest=# CREATE TABLE pgtest(id integer not null primary key default nextval('pgtest_seq'), test varchar(50));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pgtest_pkey" for table "pgtest"
CREATE TABLE
pgtest=# insert into pgtest(test) values('test #1');
INSERT 0 1
pgtest=# SELECT * from pgtest;
id |  test
----+---------
  1 | test #1
(1 row)

pgtest=# CREATE USER pgtest with password 'pgtest';
CREATE ROLE
pgtest=# grant all privileges on pgtest to pgtest with grant option;
GRANT
pgtest=# grant all privileges on pgtest_seq to pgtest with grant option;
GRANT


Hibernate version:
Hibernate-Version: 3.2.5.g

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2009.02.19 15:23:31 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="pgtest.Pgtest" table="pgtest" schema="public">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="test" type="string">
            <column name="test" length="50" />
        </property>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Pgtest t = (Pgtest) session.load(Pgtest.class, 1);
System.out.println("pgtest(#1).test: " + t.getTest());

SQLQuery sql = session.createSQLQuery("insert into pgtest(test) values(?)");
sql.setString(0, "test #2");
System.out.println("sql: " + sql.getQueryString().toString());
System.out.println("result: " + sql.executeUpdate());

session.flush();


Run output:
Code:
Hibernate: select pgtest0_.id as id0_0_, pgtest0_.test as test0_0_ from public.pgtest pgtest0_ where pgtest0_.id=?
pgtest(#1).test: test #1
sql: insert into pgtest(test) values(?)
Hibernate: insert into pgtest(test) values(?)
result: 1


Full stack trace of any exception that occurs:
no exception occured

Name and version of the database you are using:
postgresql 8.3.5-0ubuntu0.8.04

The generated SQL (show_sql=true):
Code:
Hibernate: select pgtest0_.id as id0_0_, pgtest0_.test as test0_0_ from public.pgtest pgtest0_ where pgtest0_.id=?
Hibernate: insert into pgtest(test) values(?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 11:52 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
A few things to look into:

What is handling transactions?

Is your Postgres instance configured to accept connections over TCP/IP?


--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 12:34 pm 
Newbie

Joined: Tue Aug 07, 2007 8:43 am
Posts: 4
i think that i'm missing something in hibernate configuration because if i replace hibernate code with pure jdbc code everything works.
if i can connect and insert data using a simple jdbc query what i'm i doing wrong in hibernate?

jdbc code that works
Code:
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost/pgtest?user=pgtest&password=pgtest";
Connection conn = DriverManager.getConnection(url);

Statement in = conn.createStatement();
in.execute("insert into pgtest(test) values('test #3')");
in.close();

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM pgtest");
while (rs.next()) {
System.out.println("ID "+rs.getString(1)+": "+rs.getString(2));
}
rs.close();
st.close();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 12:47 pm 
Newbie

Joined: Tue Aug 07, 2007 8:43 am
Posts: 4
if i use transactions code works normally. my question now is why can't it work without transactions in postgres when it works normally in mysql without transactions?

Code:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
tx.begin();

Pgtest t2 = new Pgtest();
t2.setTest("test #3");
session.save(t2);

tx.commit();
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 3:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
if i use transactions code works normally. my question now is why can't it work without transactions in postgres when it works normally in mysql without transactions?


Which table type are you using in MySQL? It is important since not all table types supports transaction. For example MyISAM doesn't, but InnoDB does.

With PostgreSQL I transactions are always working, but when you are using plain JDBC it may be that connections are opened in auto-commit mode, which means that the JDBC driver will automatically commit the transaction after every statement. This can be changed by calling Connection.setAutoCommit(false).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.