Advertisement

06.21.2007 at 04:12PM PDT, ID: 22650090
[x]
Attachment Details

Hibernate/Postgresql/jdbc issue!

Asked by phaidra in PostgreSQL Database, J2EE, WebApplications

Tags: hibernate, postgresql

I am having using inserting new values into what shoudl be a rather simple hibernate/postgresql map.  I have created all my mappings in myeclipse using the hibernate tool.  When I try to save to the postgresql database I am getting the following error:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select nextval ('people_pid_seq')
Hibernate: insert into public.People (fname, lname, sex, address, district, pid) values (?, ?, ?, ?, ?, ?)
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
      at com.genuitec.hibernate.WritePeopleTest.createPerson(WritePeopleTest.java:30)
      at com.genuitec.hibernate.WritePeopleTest.main(WritePeopleTest.java:19)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into public.People (fname, lname, sex, address, district, pid) values ('Ian', 'Smith', 'male', '16 Mount Mossey Street', 'Belmopan', 15) was aborted.  Call getNextException to see the cause.
      at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2530)
      at org.postgresql.core.v2.QueryExecutorImpl$2.handleError(QueryExecutorImpl.java:283)
      at org.postgresql.core.v2.QueryExecutorImpl.processResults(QueryExecutorImpl.java:480)
      at org.postgresql.core.v2.QueryExecutorImpl.execute(QueryExecutorImpl.java:364)
      at org.postgresql.core.v2.QueryExecutorImpl.execute(QueryExecutorImpl.java:291)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2592)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
      ... 9 more

My database table is:

 create table "public"."People"(
        "pid"  serial not null,
       "fname" varchar(20),
       "lname" varchar(30),
       "sex" varchar(6),
       "address" varchar(40),
       "district" varchar(20),
        constraint "People_pkey" primary key ("pid")
    );

    create unique index "People_pkey" on "public"."People"("pid");

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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
      <property name="connection.username">cisadmi_ians</property>
      <property name="connection.url">
            jdbc:postgresql://72.41.4.5:5432/cisadmi_earthlung
      </property>
      <property name="dialect">
            org.hibernate.dialect.PostgreSQLDialect
      </property>
      <property name="myeclipse.connection.profile">testing</property>
      <property name="connection.password">phaidra</property>
      <property name="connection.driver_class">
            org.postgresql.Driver
      </property>
      <property name="transaction.factory_class">
            org.hibernate.transaction.JDBCTransactionFactory
      </property>
      <property name="current_session_context_class">thread</property>
      <property name="show_sql">true</property>
      <property name="jdbc.batch_size">20</property>
      <mapping resource="com/genuitec/hibernate/People.hbm.xml" />

</session-factory>

</hibernate-configuration>

People.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.genuitec.hibernate.People" table="People" schema="public">
        <id name="pid" type="java.lang.Integer">
            <column name="pid" />
            <generator class="sequence">
                  <param name="sequence">people_pid_seq</param>
            </generator>
        </id>
        <property name="fname" type="java.lang.String">
            <column name="fname" length="20" />
        </property>
        <property name="lname" type="java.lang.String">
            <column name="lname" length="30" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" length="6" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" length="40" />
        </property>
        <property name="district" type="java.lang.String">
            <column name="district" length="20" />
        </property>
    </class>
</hibernate-mapping>

My java code to save the data is:

package com.genuitec.hibernate;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class WritePeopleTest {
      private static Logger log =Logger.getLogger(People.class);
            public static void main(String[] args) {
                  People p = new People();
                  p.setFname("Ian");
                  p.setLname("Smith");
                  p.setSex("male");
                  p.setAddress("16 Mount Mossey Street");
                  p.setDistrict("Belmopan");
                  createPerson(p);
                  log.debug(p);
            }
            
            private static void createPerson(People p) {
                  PeopleDAO pdao = new PeopleDAO();
                  Transaction tx = null;
                  Session session = pdao.getSession();
                  try {
                        tx = session.beginTransaction();
                        session.saveOrUpdate(p);
                        tx.commit();
                  } catch (HibernateException e) {
                        e.printStackTrace();
                        if (tx != null && tx.isActive())
                              tx.rollback();
                  }      
            }
}

The PeopleDAO.java file:

package com.genuitec.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class People.
 * @see com.genuitec.hibernate.People
 * @author MyEclipse - Hibernate Tools
 */
public class PeopleDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(PeopleDAO.class);

      //property constants
      public static final String FNAME = "fname";
      public static final String LNAME = "lname";
      public static final String SEX = "sex";
      public static final String ADDRESS = "address";
      public static final String DISTRICT = "district";

   
    public void save(People transientInstance) {
        log.debug("saving People instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
   
      public void delete(People persistentInstance) {
        log.debug("deleting People instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
   
    public People findById( java.lang.Integer id) {
        log.debug("getting People instance with id: " + id);
        try {
            People instance = (People) getSession()
                    .get("com.genuitec.hibernate.People", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
   
   
    public List findByExample(People instance) {
        log.debug("finding People instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.genuitec.hibernate.People")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
   
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding People instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from People as model where model."
                                             + propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
             queryObject.setParameter(0, value);
             return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
      }

      public List findByFname(Object fname) {
            return findByProperty(FNAME, fname);
      }
      
      public List findByLname(Object lname) {
            return findByProperty(LNAME, lname);
      }
      
      public List findBySex(Object sex) {
            return findByProperty(SEX, sex);
      }
      
      public List findByAddress(Object address) {
            return findByProperty(ADDRESS, address);
      }
      
      public List findByDistrict(Object district) {
            return findByProperty(DISTRICT, district);
      }
      
    public People merge(People detachedInstance) {
        log.debug("merging People instance");
        try {
            People result = (People) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(People instance) {
        log.debug("attaching dirty People instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
   
    public void attachClean(People instance) {
        log.debug("attaching clean People instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}

People.java file

package com.genuitec.hibernate;
// Generated by MyEclipse - Hibernate Tools



/**
 * People generated by MyEclipse - Hibernate Tools
 */
public class People extends AbstractPeople implements java.io.Serializable {

    // Constructors

    /** default constructor */
    public People() {
    }

   
    /** full constructor */
    public People(String fname, String lname, String sex, String address, String district) {
        super(fname, lname, sex, address, district);        
    }
   
}

AbstractPeople.java file:

package com.genuitec.hibernate;



/**
 * AbstractPeople generated by MyEclipse - Hibernate Tools
 */

public abstract class AbstractPeople  implements java.io.Serializable {


    // Fields    

     private Integer pid;
     private String fname;
     private String lname;
     private String sex;
     private String address;
     private String district;


    // Constructors

    /** default constructor */
    public AbstractPeople() {
    }

   
    /** full constructor */
    public AbstractPeople(String fname, String lname, String sex, String address, String district) {
        this.fname = fname;
        this.lname = lname;
        this.sex = sex;
        this.address = address;
        this.district = district;
    }

   
    // Property accessors

    public Integer getPid() {
        return this.pid;
    }
   
    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getFname() {
        return this.fname;
    }
   
    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return this.lname;
    }
   
    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getSex() {
        return this.sex;
    }
   
    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return this.address;
    }
   
    public void setAddress(String address) {
        this.address = address;
    }

    public String getDistrict() {
        return this.district;
    }
   
    public void setDistrict(String district) {
        this.district = district;
    }
   
}

I am using the following postgresql jdbc driver: postgresql-8.2-505.jdbc3.jar

Any help would be greatly appreciated.  Start Free Trial
[+][-]06.21.2007 at 05:44PM PDT, ID: 19338170

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2007 at 04:29AM PDT, ID: 19340375

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.22.2007 at 07:44AM PDT, ID: 19341804

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.22.2007 at 08:08AM PDT, ID: 19342045

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PostgreSQL Database, J2EE, WebApplications
Tags: hibernate, postgresql
Sign Up Now!
Solution Provided By: gheist
Participating Experts: 2
Solution Grade: B
 
 
[+][-]06.22.2007 at 03:54PM PDT, ID: 19345508

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32