gpt4 book ai didi

java - (Hibernate)由 : java. sql.SQLException 引起:字段 'UPDATE_TS' 没有默认值

转载 作者:行者123 更新时间:2023-12-02 05:46:42 26 4
gpt4 key购买 nike

我尝试过 hibernate 中的自动更新日期功能并遵循此link

界面

import entity.TechnicalColumns;

public interface EntityWithTechnicalColumns {
public TechnicalColumns getTechnicalColumns();

public void setTechnicalColumns(TechnicalColumns technicalColumns);
}

实现

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class TechnicalColumns {
@Column(name="UPDATE_TS", insertable=false, updatable=true,nullable = false)
private Timestamp dateMaj;
@Column(name="CREATION_TS", insertable=true, updatable=false,nullable = false)
private Timestamp dateCrea;
/**
* @return the dateMaj
*/
public Timestamp getDateMaj() {
return dateMaj;
}
/**
* @param dateMaj the dateMaj to set
*/
public void setDateMaj(Timestamp dateMaj) {
this.dateMaj = dateMaj;
}
/**
* @return the dateCrea
*/
public Timestamp getDateCrea() {
return dateCrea;
}
/**
* @param dateCrea the dateCrea to set
*/
public void setDateCrea(Timestamp dateCrea) {
this.dateCrea = dateCrea;
}
}

监听器类

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import interfaces.EntityWithTechnicalColumns;

public class TimestampEntityListener {
@PrePersist
void onCreate(Object entity) {
if(entity instanceof EntityWithTechnicalColumns) {
EntityWithTechnicalColumns eact = (EntityWithTechnicalColumns)entity;
if(eact.getTechnicalColumns() == null) {
eact.setTechnicalColumns(new TechnicalColumns());
}
eact.getTechnicalColumns().setDateCrea(new Timestamp((new Date()).getTime()));
}
}

@PreUpdate
void onPersist(Object entity) {
if(entity instanceof EntityWithTechnicalColumns) {
EntityWithTechnicalColumns eact = (EntityWithTechnicalColumns)entity;
if(eact.getTechnicalColumns() == null) {
eact.setTechnicalColumns(new TechnicalColumns());
}
eact.getTechnicalColumns().setDateMaj(new Timestamp((new Date()).getTime()));
}
}
}

我的测试类(class)

@Entity
@Table(name = "Test")
public class Test
implements EntityWithTechnicalColumns,Serializable{


@Embedded
private TechnicalColumns technicalColumns;


/** Primary key auto generated for Test table. */
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "test_id", unique = true, nullable = false)
private long id;


@Column
private String name;


@Column
private String code;

/**
* Creates a setter for the primary key.
* @param id to be assigned
*/
public void setId(long id) {
this.id = id;
}

/**
* Creates a getter for the primary key.
* @return id
*/
public long getId() {
return id;
}

/**
* Creates a setter for the name.
* @param name to be assigned
*/
public void setName(String name) {
this.name = name;
}
/**
* Creates a getter for the name.
* @return name
*/
public String getName() {
return name;
}
/**
* Creates a setter for the code.
* @param code to be assigned
*/
public void setCode(String code) {
this.code = code;
}
/**
* Creates a getter for the code.
* @return code
*/
public String getCode() {
return code;
}


@Override
public TechnicalColumns getTechnicalColumns() {
return technicalColumns;
}

@Override
public void setTechnicalColumns(TechnicalColumns technicalColumns) {
this.technicalColumns = technicalColumns;

}

}

最后是 persistence.xml 文件

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" version="1.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="com.octo.rnd.TimestampEntityListener">
<pre-persist method-name="onCreate">
<pre-update method-name="onPersist">
</pre-update></pre-persist></entity-listener>
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>

如果应用程序会抛出以下错误,

ERROR [org.hibernate.util.JDBCExceptionReporter] Field 'UPDATE_TS' doesn't have a default value
12:20:20,434 SEVERE [com.sun.jersey.spi.container.ContainerResponse] The RuntimeException could not be mapped to a response, re-throwing to the HTTP container: org.hibernate.exception.GenericJDBCException: could not insert: [com.entity.Test]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140) [:3.6.0.Final]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128) [:3.6.0.Final]
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) [:3.6.0.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) [:3.6.0.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2327) [:3.6.0.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2834) [:3.6.0.Final]
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) [:3.6.0.Final]
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) [:3.6.0.Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320) [:3.6.0.Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203) [:3.6.0.Final]
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) [:3.6.0.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) [:3.6.0.Final]
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) [:3.6.0.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) [:3.6.0.Final]
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) [:3.6.0.Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) [:3.6.0.Final]
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) [:3.6.0.Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) [:3.6.0.Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) [:3.6.0.Final]
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) [:]
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [:3.1.0.RELEASE]
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) [:3.1.0.RELEASE]
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) [:3.1.0.RELEASE]
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) [:3.1.0.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [:3.1.0.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [:3.1.0.RELEASE]
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) [:3.1.0.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.6.0_30]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.6.0_30]
at java.lang.reflect.Method.invoke(Method.java:622) [:1.6.0_30]
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) [:1.12]
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) [:1.12]
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) [:1.12]
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) [:1.12]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [:1.12]
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) [:1.12]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [:1.12]
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) [:1.12]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483) [:1.12]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414) [:1.12]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363) [:1.12]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353) [:1.12]
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414) [:1.12]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) [:1.12]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) [:1.12]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.6.0_30]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.6.0_30]
at java.lang.reflect.Method.invoke(Method.java:622) [:1.6.0_30]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [:3.1.0.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [:3.1.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [:3.1.0.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [:3.1.0.RELEASE]
at com.sun.proxy.$Proxy206.doFilter(Unknown Source)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [:3.1.0.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [:3.1.0.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
at java.lang.Thread.run(Thread.java:701) [:1.6.0_30]
Caused by: java.sql.SQLException: Field 'UPDATE_TS' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) [:]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) [:]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) [:]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) [:]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) [:]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) [:]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) [:]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427) [:]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345) [:]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330) [:]
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) [:1.4]
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) [:1.4]
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) [:3.6.0.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) [:3.6.0.Final]
... 92 more

如何解决此错误?

最佳答案

我相信 dateMaj 为空,根据您带注释的类,它不可为空。你需要做

        eact.getTechnicalColumns().setDateMaj(new Timestamp((new Date()).getTime()));

在用@PrePersist注释的方法中。

题外话:我会将方法 onPersist 重命名为 onUpdate 以提高代码可读性

关于java - (Hibernate)由 : java. sql.SQLException 引起:字段 'UPDATE_TS' 没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23989295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com