Hibernate - 没有为参数指定值

时间:2014-12-16 08:51:19

标签: spring hibernate

我有一个像这样映射的实体类:

@Entity
@Table(name = "configuration")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class JsonConfiguration implements JsonSerializable, Serializable {
private static final long serialVersionUID = 1L;

@Id
@NotNull
@JsonProperty("_id")
private Integer id;

@JsonProperty("config")
@NotNull
@Type(type = "jsonType")
private String config;

public JsonConfiguration(Integer id, String config) {
    this.id = id;
    this.config = config;
}

public JsonConfiguration(){ 
    super();
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getConfig() {
    return config;
}

public void setConfig(String config) {
    this.config = config;
}
}

每次我尝试更新或插入时都会遇到同样的错误:

Caused by: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:167)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:730)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:592)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:497)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.company.ngnms.server.services.cfg.PackitLoaderImpl$$EnhancerBySpringCGLIB$$3bbeeb14.createComponentFromPackit(<generated>)
at com.company.ngnms.server.management.data.DataPopulatorImpl.createComponentsAndEventDefinition(DataPopulatorImpl.java:126)
at com.company.ngnms.server.management.data.DataPopulatorImpl.createInitialComponents(DataPopulatorImpl.java:296)
at com.company.ngnms.server.management.data.DataPopulatorImpl.doPopulate(DataPopulatorImpl.java:274)
at com.company.ngnms.server.management.data.DataPopulatorImpl.populateData(DataPopulatorImpl.java:229)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1694)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 59 more
Caused by: org.hibernate.exception.DataException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:135)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
... 78 more
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:223)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:244)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:365)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)

以下是配置列的TypeDef:

public class JsonUserType extends AbstractUserType {
@Override
public int[] sqlTypes() {
    return new int[] { Types.JAVA_OBJECT};
}

@SuppressWarnings("rawtypes")
@Override
public Class returnedClass() {
    return String.class;
}

@Override
public Object nullSafeGet(ResultSet resultSet, String[] strings, SessionImplementor sessionImplementor, Object o) throws HibernateException, SQLException {
    if(resultSet.getString(strings[0]) == null){
        return null;
    }
    return resultSet.getString(strings[0]);
}

@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SessionImplementor sessionImplementor) throws HibernateException, SQLException {
    if (o == null) {
        preparedStatement.setNull(i, Types.OTHER);
        return;
    }
}

}

及其扩展的类:

abstract class AbstractUserType implements UserType {
@Override
public Object assemble(Serializable cached, Object owner) {
    return cached;
}

@Override
public Object deepCopy(Object value) {
    return value;
}

@Override
public Serializable disassemble(Object value) {
    return (Serializable)value;
}

@Override
public boolean equals(Object x, Object y) {
    return (x == y) || (x != null && y != null && (x.equals(y)));
}

@Override
public int hashCode(Object x) {
    return x.hashCode();
}

@Override
public boolean isMutable() {
    return false;
}

@Override
public Object replace(Object original, Object target, Object owner) {
    return original;
}

}

有人可以解释为什么会这样吗?这一切都是在我迁移到Spring 4和Hibernate 4之后开始的。从下面提供的Hibernate日志来看,它似乎无法绑定第一个参数,但我无法弄清楚原因。

insert 
into
    configuration
    (config, id) 
values
    (?, ?)
15-Dec-2014 17:16:54,594 TRACE [BasicBinder:main] binding parameter [2] as [INTEGER] - [229]
15-Dec-2014 17:16:54,595 WARN  [SqlExceptionHelper:main] SQL Error: 0, SQLState: 22023
15-Dec-2014 17:16:54,595 ERROR [SqlExceptionHelper:main] No value specified for parameter 1.

0 个答案:

没有答案