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.Environ
ment).
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.Ge
nericJDBCE
xception: Could not execute JDBC batch update
at org.hibernate.exception.SQ
LStateConv
erter.hand
ledNonSpec
ificExcept
ion(SQLSta
teConverte
r.java:103
)
at org.hibernate.exception.SQ
LStateConv
erter.conv
ert(SQLSta
teConverte
r.java:91)
at org.hibernate.exception.JD
BCExceptio
nHelper.co
nvert(JDBC
ExceptionH
elper.java
:43)
at org.hibernate.jdbc.Abstrac
tBatcher.e
xecuteBatc
h(Abstract
Batcher.ja
va:202)
at org.hibernate.engine.Actio
nQueue.exe
cuteAction
s(ActionQu
eue.java:2
35)
at org.hibernate.engine.Actio
nQueue.exe
cuteAction
s(ActionQu
eue.java:1
39)
at org.hibernate.event.def.Ab
stractFlus
hingEventL
istener.pe
rformExecu
tions(Abst
ractFlushi
ngEventLis
tener.java
:297)
at org.hibernate.event.def.De
faultFlush
EventListe
ner.onFlus
h(DefaultF
lushEventL
istener.ja
va:27)
at org.hibernate.impl.Session
Impl.flush
(SessionIm
pl.java:98
5)
at org.hibernate.impl.Session
Impl.manag
edFlush(Se
ssionImpl.
java:333)
at org.hibernate.transaction.
JDBCTransa
ction.comm
it(JDBCTra
nsaction.j
ava:106)
at com.genuitec.hibernate.Wri
tePeopleTe
st.createP
erson(Writ
ePeopleTes
t.java:30)
at com.genuitec.hibernate.Wri
tePeopleTe
st.main(Wr
itePeopleT
est.java:1
9)
Caused by: java.sql.BatchUpdateExcept
ion: 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.Abstr
actJdbc2St
atement$Ba
tchResultH
andler.han
dleError(A
bstractJdb
c2Statemen
t.java:253
0)
at org.postgresql.core.v2.Que
ryExecutor
Impl$2.han
dleError(Q
ueryExecut
orImpl.jav
a:283)
at org.postgresql.core.v2.Que
ryExecutor
Impl.proce
ssResults(
QueryExecu
torImpl.ja
va:480)
at org.postgresql.core.v2.Que
ryExecutor
Impl.execu
te(QueryEx
ecutorImpl
.java:364)
at org.postgresql.core.v2.Que
ryExecutor
Impl.execu
te(QueryEx
ecutorImpl
.java:291)
at org.postgresql.jdbc2.Abstr
actJdbc2St
atement.ex
ecuteBatch
(AbstractJ
dbc2Statem
ent.java:2
592)
at org.hibernate.jdbc.Batchin
gBatcher.d
oExecuteBa
tch(Batchi
ngBatcher.
java:58)
at org.hibernate.jdbc.Abstrac
tBatcher.e
xecuteBatc
h(Abstract
Batcher.ja
va: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_i
ans</prope
rty>
<property name="connection.url">
jdbc:postgresql://72.41.4.
5:5432/cis
admi_earth
lung
</property>
<property name="dialect">
org.hibernate.dialect.Post
greSQLDial
ect
</property>
<property name="myeclipse.connection
.profile">
testing</p
roperty>
<property name="connection.password"
>phaidra</
property>
<property name="connection.driver_cl
ass">
org.postgresql.Driver
</property>
<property name="transaction.factory_
class">
org.hibernate.transaction.
JDBCTransa
ctionFacto
ry
</property>
<property name="current_session_cont
ext_class"
>thread</p
roperty>
<property name="show_sql">true</prop
erty>
<property name="jdbc.batch_size">20<
/property>
<mapping resource="com/genuitec/hib
ernate/Peo
ple.hbm.xm
l" />
</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.hiberna
te.People"
table="People" schema="public">
<id name="pid" type="java.lang.Integer">
<column name="pid" />
<generator class="sequence">
<param name="sequence">people_pid
_seq</para
m>
</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.HibernateExc
eption;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class WritePeopleTest {
private static Logger log =Logger.getLogger(People.c
lass);
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
.LogFactor
y;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Ex
ample;
/**
* Data access object (DAO) for domain model class People.
* @see com.genuitec.hibernate.Peo
ple
* @author MyEclipse - Hibernate Tools
*/
public class PeopleDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(PeopleDA
O.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(transien
tInstance)
;
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(persis
tentInstan
ce);
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.hiberna
te.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.genui
tec.hibern
ate.People
")
.add(Example.create(instan
ce))
.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(q
ueryString
);
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.j
ar
Any help would be greatly appreciated.
Start Free Trial