I have a JPA class that I'm trying to save to a postgres 9 DB using hibernate. But I get this exception:
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:212)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:212)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:52)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:368)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:349)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into blah.config_type (config_type_description, config_type_name, insert_by, insert_time, update_by, update_time, config_type_id) values ('config type description', 'test', NULL, NULL, NULL, NULL, '71') was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2621)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1837)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
Here's the JPA class:
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "config_type")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "ConfigType.findAll", query = "SELECT c FROM ConfigType c"),
@NamedQuery(name = "ConfigType.findByConfigTypeId", query = "SELECT c FROM ConfigType c WHERE c.configTypeId = :configTypeId"),
@NamedQuery(name = "ConfigType.findByConfigTypeName", query = "SELECT c FROM ConfigType c WHERE c.configTypeName = :configTypeName"),
@NamedQuery(name = "ConfigType.findByInsertBy", query = "SELECT c FROM ConfigType c WHERE c.insertBy = :insertBy"),
@NamedQuery(name = "ConfigType.findByUpdateBy", query = "SELECT c FROM ConfigType c WHERE c.updateBy = :updateBy"),
@NamedQuery(name = "ConfigType.findByInsertTime", query = "SELECT c FROM ConfigType c WHERE c.insertTime = :insertTime"),
@NamedQuery(name = "ConfigType.findByUpdateTime", query = "SELECT c FROM ConfigType c WHERE c.updateTime = :updateTime"),
@NamedQuery(name = "ConfigType.findByConfigTypeDescription", query = "SELECT c FROM ConfigType c WHERE c.configTypeDescription = :configTypeDescription")})
public class ConfigType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "config_type_id")
@SequenceGenerator(name = "config_type_seq", sequenceName = "config_type_config_type_id_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "config_type_seq")
private Integer configTypeId;
@Basic(optional = false)
@Column(name = "config_type_name")
private String configTypeName;
@Basic(optional = true)
@Column(name = "insert_by")
private String insertBy;
@Basic(optional = true)
@Column(name = "update_by")
private String updateBy;
@Basic(optional = true)
@Column(name = "insert_time")
@Temporal(TemporalType.TIMESTAMP)
private Date insertTime;
@Basic(optional = true)
@Column(name = "update_time")
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;
@Basic(optional = false)
@Column(name = "config_type_description")
private String configTypeDescription;
public ConfigType() {
}
public ConfigType(Integer configTypeId) {
this.configTypeId = configTypeId;
}
public ConfigType(Integer configTypeId, String configTypeName, String insertBy, String updateBy, Date insertTime, Date updateTime, String configTypeDescription) {
this.configTypeId = configTypeId;
this.configTypeName = configTypeName;
this.insertBy = insertBy;
this.updateBy = updateBy;
this.insertTime = insertTime;
this.updateTime = updateTime;
this.configTypeDescription = configTypeDescription;
}
public Integer getConfigTypeId() {
return configTypeId;
}
public void setConfigTypeId(Integer configTypeId) {
this.configTypeId = configTypeId;
}
public String getConfigTypeName() {
return configTypeName;
}
public void setConfigTypeName(String configTypeName) {
this.configTypeName = configTypeName;
}
public String getInsertBy() {
return insertBy;
}
public void setInsertBy(String insertBy) {
this.insertBy = insertBy;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getInsertTime() {
return insertTime;
}
public void setInsertTime(Date insertTime) {
this.insertTime = insertTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getConfigTypeDescription() {
return configTypeDescription;
}
public void setConfigTypeDescription(String configTypeDescription) {
this.configTypeDescription = configTypeDescription;
}
}
Here's the SQL creation in postgres:
-- Table: blah.config_type
-- DROP TABLE blah.config_type;
CREATE TABLE blah.config_type
(
config_type_id serial NOT NULL,
config_type_name character varying(50) NOT NULL,
insert_by character varying(50) NOT NULL DEFAULT 'SESSION_USER'::character varying,
update_by character varying(50) NOT NULL DEFAULT 'SESSION_USER'::character varying,
insert_time timestamp without time zone NOT NULL DEFAULT blah.f_utc_timestamp(),
update_time timestamp without time zone NOT NULL DEFAULT blah.f_utc_timestamp(),
config_type_description text NOT NULL,
CONSTRAINT pk_config_type PRIMARY KEY (config_type_id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE blah.config_type
OWNER TO postgres;
COMMENT ON TABLE blah.config_type
IS 'defines the type of config used by the UI. (String, Integer, Float etc).';
-- Trigger: TAU_CONFIG_TYPE on blah.config_type
-- DROP TRIGGER "TAU_CONFIG_TYPE" ON blah.config_type;
CREATE TRIGGER "TAU_CONFIG_TYPE"
AFTER UPDATE
ON blah.config_type
FOR EACH ROW
EXECUTE PROCEDURE blah.fau_date_by_version();
-- Sequence: blah.config_type_config_type_id_seq
-- DROP SEQUENCE blah.config_type_config_type_id_seq;
CREATE SEQUENCE blah.config_type_config_type_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE blah.config_type_config_type_id_seq
OWNER TO postgres;
Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.blah.db.model.ConfigType</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.default_schema" value="blah"/>
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>
</persistence>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">
<!-- Directory to scan for repository classes -->
<jpa:repositories
base-package="com.blah.db.model.repository"
query-lookup-strategy="create-if-not-found"/>
<context:annotation-config />
<context:component-scan base-package="com.blah.db.model.*" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="POSTGRESQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</bean>
</beans>
Thanks for any help.