gpt4 book ai didi

java - hibernate 保存或更新: no SqlStatement when a field is present

转载 作者:行者123 更新时间:2023-12-02 02:37:01 25 4
gpt4 key购买 nike

Hibernate saveOrUpdate 的奇怪行为:

  1. 什么时候
    1. 实体的字段eqfComment(String)为空,
    2. 我修改了字段eqfDt(java.util.Date,只修改了小时部分),
    3. 然后我可以在日志中看到Hibernate生成的UPDATE SqlStatement,并在数据库中修改该行;
  2. 什么时候
    1. 字段 eqfComment 不为空,即使只是一个普通字符,例如“t”
    2. 我修改了字段eqfDt的小时
    3. 那么日志中就没有SqlStatement,数据库中也没有修改该行。

我可以在两种情况下的日志中看到修改字段的值。

实体:

@Entity
@Table(name = "SIT_EVENEMENT_QF")
public class EvenementQf {

@Id
@Column(name = "EQF_ID", unique = true, nullable = false)
private Long eqfId;

@Temporal(TemporalType.DATE)
@Column(name = "EQF_DT", nullable = false, length = 7)
private Date eqfDt;

@Column(name = "EQF_COMMENT", length = 4000)
private String eqfComment;

...

}

道:

public void saveOrUpdate(EvenementQf entity) throws EqfDAOException {
try {


if (entity.getId() != null) {
this.getSession().evict(this.get(entity.getId()));
}

this.getSession().saveOrUpdate(entity);


} catch (HibernateException var4) {
this.log.info("Erreur Hibernate " + var4);
throw var4;
} catch (Exception var5) {
this.log.error("Erreur inattendue : " + var5);
throw var5;
}
}

Debug模式下的 Hibernate 日志:

  1. 没有字段 EqfComment,因此有更新语句:

    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:114) - Listing entities:
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.SioTypeLid{tliLib=Ligne simple, tliCod=L, sioLids=<uninitialized>}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.EvenementQf{eqfComment=, eqfIndMarqNat=N, lid=com.rte.siquael.dao.entity.Lid#1476, eqfTypeMateriel=null, eqfEssNo=null, evtGrdAmpleur=null, eqfReprise=null, eqfIndRht=N, blocCoupure=null, eqfConstructeur=null, poste=null, sitPerturbations=[], eqfCod=17003430, eqfIndMarqLoc=O, indicateurZone=N, eqfCommentaireRh=null, eqfCommentaireRj=null, eqfDt=Sun Sep 10 10:10:10 CEST 2017, gesrId=null, eqfId=352082, eqfIrtsyst=N, sitCourtCircuits=[com.rte.siquael.dao.entity.CourtCircuit#337101], tessId=null, statut=E, sitAnomalies=[], eqfCommentaireRht=null, sitEvenementCbs=[], eqfIndTraitement=N, eqfEsrNo=null, motcleId=null, contrainteId=402, eqfRje=null, eqfMarquageDt=Mon Sep 11 16:08:09 CEST 2017, sitStatutAt=com.rte.siquael.dao.entity.SitStatutAt#E, eqfDtMs=0, eqfDtCreat=2017-09-11, eqfIndRj=N, clientId=null, eqfIndRh=N, eqfNoEga=null, sitEcartExploits=[], eqfAvtIncid=null, complementRegionaux=null, eqfDtMscal=212371812122000, eqfBloque=N, eqfOuvNtnLib=null, gessId=null, eqfDtMaj=Tue Sep 12 09:54:46 CEST 2017, eqfIndMarqReg=N, sitActionSuiviRexes=<uninitialized>, eqfOuvLib=null, eqfDateMiseEnService=null, tesrId=null, responsableId=463}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Poste{posIndiceCur=null, clients=<uninitialized>, blocCoupures=<uninitialized>, posAdr=Partenay les bonobonos, sioTypeSite=com.rte.siquael.dao.entity.TypeSite#P, posCoordonneeX=926619.18, posCoordonneeY=6514695.25, posEtat=Existant, posDtIn=1970-01-01, posId=1214, posNumNat=004, cdfIdTypProp=297, posDpde=1970-01-01, posDrfe=null, sioNiveauTensions=<uninitialized>, posDrde=1970-01-01, structureChargeeExploitation=com.rte.siquael.dao.entity.Structure#103, structureAdm=com.rte.siquael.dao.entity.StructureAdm#1312, commentaire=injection-soutirage , sioOuvrage=<uninitialized>, sioPays=com.rte.siquael.dao.entity.Pays#F, sioPsts=<uninitialized>, posCodNat=A.BAI, posDateModifInforef=2017-02-15, posDpfe=null, tranchesBt=<uninitialized>, complementRegionaux=null, posLibCrt=Partenay les bonobonos, posDtHs=null, posDtCre=1997-11-07, posDtMaj=2017-03-16, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, posCur=13800B670A522054E043A3681E7C2054}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Poste{posIndiceCur=null, clients=<uninitialized>, blocCoupures=<uninitialized>, posAdr=Partenay les bonobonos, sioTypeSite=com.rte.siquael.dao.entity.TypeSite#P, posCoordonneeX=null, posCoordonneeY=null, posEtat=Existant, posDtIn=1974-01-01, posId=1215, posNumNat=145, cdfIdTypProp=304, posDpde=1989-01-01, posDrfe=null, sioNiveauTensions=<uninitialized>, posDrde=1989-01-01, structureChargeeExploitation=com.rte.siquael.dao.entity.Structure#103, structureAdm=com.rte.siquael.dao.entity.StructureAdm#1312, commentaire=null, sioOuvrage=<uninitialized>, sioPays=com.rte.siquael.dao.entity.Pays#F, sioPsts=<uninitialized>, posCodNat=A.BA5, posDateModifInforef=2014-12-12, posDpfe=null, tranchesBt=<uninitialized>, complementRegionaux=null, posLibCrt=Partenay les bonobonos, posDtHs=null, posDtCre=1997-11-07, posDtMaj=1998-05-29, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, posCur=13800B670A512054E043A3681E7C2054}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Lid{sitAnomalies=<uninitialized>, sitEvenementQfs=<uninitialized>, lidNumNat=55S003300, sioTypeLid=com.rte.siquael.dao.entity.SioTypeLid#L, sitEvenementCbs=<uninitialized>, sitEvenementExploits=<uninitialized>, lidDtHs=null, lidDtIn=1974-01-01, sioTrancheTcds=<uninitialized>, structureChefFile=com.rte.siquael.dao.entity.Structure#103, lidDtMaj=2001-07-31, lidDtCre=1997-11-07, lidTer=N, commentaire=null, lidId=1476, silAcaOuvrages=<uninitialized>, sioOuvrages=<uninitialized>, sioNiveauTension=com.rte.siquael.dao.entity.SioNiveauTension#3, silActionCorrs=<uninitialized>, posteA=com.rte.siquael.dao.entity.Poste#1214, sioLits=<uninitialized>, posteB=com.rte.siquael.dao.entity.Poste#1215, sitCourtCircuits=<uninitialized>, lidCodNat=A.BAIL31A.BA5, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, lidLib=63kV Partenay les bonobonos}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Structure{sioPostesForStrIdCep=<uninitialized>, strLib=GdP de Longefan, strCmp=0, sioPostesForStrIdCcp=<uninitialized>, clients=<uninitialized>, sitAnomalies=<uninitialized>, strDtCre=1992-02-01, silPersonnesForStrId=<uninitialized>, siqCodifications=<uninitialized>, ssDtFin=null, strIndiceCur=-, sitEvenementExploits=<uninitialized>, strDir=5, strOss=25, sioLidsForStrIdCfd=<uninitialized>, sitSauveEtatSaisies=<uninitialized>, ssDtDeb=null, strSun=S, silActionAmeliorations=<uninitialized>, sioUtilisateurs=<uninitialized>, strCur=D86AC36BE9AF4798937552AE34228317, strDtSup=null, strLoc=St Jean de Maurienne, sioFiltreStds=<uninitialized>, strNumOrdre=5, strCodPos=73300, silAcaOuvrages=<uninitialized>, silGroupeClients=<uninitialized>, sioStructures=<uninitialized>, strId=103, sioProfils=<uninitialized>, sioProfils_1=<uninitialized>, sioLidsForStrIdCcd=<uninitialized>, sitEqfCresRexes=<uninitialized>, sioLits=<uninitialized>, structure=com.rte.siquael.dao.entity.Structure#41, strAdr2=Cedex 1708, typeStructure=com.rte.siquael.dao.entity.TypeStructure#GdP, strAdr1=X, strFax=04XXXXXXXX, strVis=O, silPersonnesForStrIdApp=<uninitialized>, strTel=04XXXXXXXX}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Structure{sioPostesForStrIdCep=<uninitialized>, strLib=Lyon, strCmp=null, sioPostesForStrIdCcp=<uninitialized>, clients=<uninitialized>, sitAnomalies=<uninitialized>, strDtCre=1992-02-01, silPersonnesForStrId=<uninitialized>, siqCodifications=<uninitialized>, ssDtFin=null, strIndiceCur=-, sitEvenementExploits=<uninitialized>, strDir=5, strOss=95, sioLidsForStrIdCfd=<uninitialized>, sitSauveEtatSaisies=<uninitialized>, ssDtDeb=null, strSun=null, silActionAmeliorations=<uninitialized>, sioUtilisateurs=<uninitialized>, strCur=40E5174E8FAF449A82EEB7328647A127, strDtSup=null, strLoc=Marteau Cedex 03, sioFiltreStds=<uninitialized>, strNumOrdre=null, strCodPos=69399, silAcaOuvrages=<uninitialized>, silGroupeClients=<uninitialized>, sioStructures=<uninitialized>, strId=8, sioProfils=<uninitialized>, sioProfils_1=<uninitialized>, sioLidsForStrIdCcd=<uninitialized>, sitEqfCresRexes=<uninitialized>, sioLits=<uninitialized>, structure=null, strAdr2=BP 3109, typeStructure=com.rte.siquael.dao.entity.TypeStructure#CRES, strAdr1=3 bis, rue des Matelots, strFax=04XXXXXXXX, strVis=O, silPersonnesForStrIdApp=<uninitialized>, strTel=04XXXXXXXX}
    org.hibernate.engine.jdbc.spi.SqlStatementLogger (SqlStatementLogger.java:109) - update SIT_EVENEMENT_QF set BCP_ID=?, CLI_ID_RESP=?, CPL_ID=?, CDF_ID_CONTR=?, EQF_AVT_INCID=?, EQF_BLOQUE=?, EQF_COD=?, EQF_COMMENT=?, EQF_COMMENTAIRE_RH=?, EQF_COMMENTAIRE_RHT=?, EQF_COMMENTAIRE_RJ=?, EQF_CONSTRUCTEUR=?, EQF_DATE_MISE_EN_SERVICE=?, EQF_DT=?, EQF_DT_CREAT=?, EQF_DT_MAJ=?, EQF_DT_MS=?, EQF_DT_MSCAL=?, EQF_ESR_NO=?, EQF_ESS_NO=?, EQF_IND_MARQ_LOC=?, EQF_IND_MARQ_NAT=?, EQF_IND_MARQ_REG=?, EQF_IND_RH=?, EQF_IND_RHT=?, EQF_IND_RJ=?, EQF_IND_TRAITEMENT=?, EQF_IRTSYST=?, EQF_MARQUAGE_DT=?, EQF_NO_EGA=?, EQF_OUV_LIB=?, EQF_OUV_NTN_LIB=?, EQF_REPRISE=?, EQF_RJE=?, EQF_TYPE_MATERIEL=?, EGA_COD=?, CDF_ID_GESR=?, CDF_ID_GESS=?, EQF_IND_ZONE=?, LID_ID=?, CDF_ID_MOTCLE=?, POS_ID=?, CDF_ID_RESP=?, STT_COD=?, CDF_ID_TESR=?, CDF_ID_TESS=? where EQF_ID=?
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction (JdbcTransaction.java:113) - committed JDBC Connection
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction (JdbcTransaction.java:126) - re-enabling autocommit
    org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl (JdbcCoordinatorImpl.java:201) - HHH000420: Closing un-released batch
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl (LogicalConnectionImpl.java:246) - Releasing JDBC connection
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl (LogicalConnectionImpl.java:264) - Released JDBC connection
  2. 在 eqfComment 字段中有一个值,因此没有 SqlStatement :

    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:114) - Listing entities:
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.SioTypeLid{tliLib=Ligne simple, tliCod=L, sioLids=<uninitialized>}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.EvenementQf{eqfComment=t, eqfIndMarqNat=N, lid=com.rte.siquael.dao.entity.Lid#1476, eqfTypeMateriel=null, eqfEssNo=null, evtGrdAmpleur=null, eqfReprise=null, eqfIndRht=N, blocCoupure=null, eqfConstructeur=null, poste=null, sitPerturbations=[], eqfCod=17003430, eqfIndMarqLoc=O, indicateurZone=N, eqfCommentaireRh=null, eqfCommentaireRj=null, eqfDt=Sun Sep 10 07:08:09 CEST 2017, gesrId=null, eqfId=352082, eqfIrtsyst=N, sitCourtCircuits=[com.rte.siquael.dao.entity.CourtCircuit#337101], tessId=null, statut=E, sitAnomalies=[], eqfCommentaireRht=null, sitEvenementCbs=[], eqfIndTraitement=N, eqfEsrNo=null, motcleId=null, contrainteId=402, eqfRje=null, eqfMarquageDt=Mon Sep 11 16:08:09 CEST 2017, sitStatutAt=com.rte.siquael.dao.entity.SitStatutAt#E, eqfDtMs=0, eqfDtCreat=2017-09-11, eqfIndRj=N, clientId=null, eqfIndRh=N, eqfNoEga=null, sitEcartExploits=[], eqfAvtIncid=null, complementRegionaux=null, eqfDtMscal=212371812122000, eqfBloque=N, eqfOuvNtnLib=null, gessId=null, eqfDtMaj=Tue Sep 12 10:40:22 CEST 2017, eqfIndMarqReg=N, sitActionSuiviRexes=<uninitialized>, eqfOuvLib=null, eqfDateMiseEnService=null, tesrId=null, responsableId=463}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Poste{posIndiceCur=null, clients=<uninitialized>, blocCoupures=<uninitialized>, posAdr=Partenay les bonobonos, sioTypeSite=com.rte.siquael.dao.entity.TypeSite#P, posCoordonneeX=926619.18, posCoordonneeY=6514695.25, posEtat=Existant, posDtIn=1970-01-01, posId=1214, posNumNat=004, cdfIdTypProp=297, posDpde=1970-01-01, posDrfe=null, sioNiveauTensions=<uninitialized>, posDrde=1970-01-01, structureChargeeExploitation=com.rte.siquael.dao.entity.Structure#103, structureAdm=com.rte.siquael.dao.entity.StructureAdm#1312, commentaire=injection-soutirage , sioOuvrage=<uninitialized>, sioPays=com.rte.siquael.dao.entity.Pays#F, sioPsts=<uninitialized>, posCodNat=A.BAI, posDateModifInforef=2017-02-15, posDpfe=null, tranchesBt=<uninitialized>, complementRegionaux=null, posLibCrt=Partenay les bonobonos, posDtHs=null, posDtCre=1997-11-07, posDtMaj=2017-03-16, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, posCur=13800B670A522054E043A3681E7C2054}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Poste{posIndiceCur=null, clients=<uninitialized>, blocCoupures=<uninitialized>, posAdr=Partenay les bonobonos, sioTypeSite=com.rte.siquael.dao.entity.TypeSite#P, posCoordonneeX=null, posCoordonneeY=null, posEtat=Existant, posDtIn=1974-01-01, posId=1215, posNumNat=145, cdfIdTypProp=304, posDpde=1989-01-01, posDrfe=null, sioNiveauTensions=<uninitialized>, posDrde=1989-01-01, structureChargeeExploitation=com.rte.siquael.dao.entity.Structure#103, structureAdm=com.rte.siquael.dao.entity.StructureAdm#1312, commentaire=null, sioOuvrage=<uninitialized>, sioPays=com.rte.siquael.dao.entity.Pays#F, sioPsts=<uninitialized>, posCodNat=A.BA5, posDateModifInforef=2014-12-12, posDpfe=null, tranchesBt=<uninitialized>, complementRegionaux=null, posLibCrt=Partenay les bonobonos, posDtHs=null, posDtCre=1997-11-07, posDtMaj=1998-05-29, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, posCur=13800B670A512054E043A3681E7C2054}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Lid{sitAnomalies=<uninitialized>, sitEvenementQfs=<uninitialized>, lidNumNat=55S003300, sioTypeLid=com.rte.siquael.dao.entity.SioTypeLid#L, sitEvenementCbs=<uninitialized>, sitEvenementExploits=<uninitialized>, lidDtHs=null, lidDtIn=1974-01-01, sioTrancheTcds=<uninitialized>, structureChefFile=com.rte.siquael.dao.entity.Structure#103, lidDtMaj=2001-07-31, lidDtCre=1997-11-07, lidTer=N, commentaire=null, lidId=1476, silAcaOuvrages=<uninitialized>, sioOuvrages=<uninitialized>, sioNiveauTension=com.rte.siquael.dao.entity.SioNiveauTension#3, silActionCorrs=<uninitialized>, posteA=com.rte.siquael.dao.entity.Poste#1214, sioLits=<uninitialized>, posteB=com.rte.siquael.dao.entity.Poste#1215, sitCourtCircuits=<uninitialized>, lidCodNat=A.BAIL31A.BA5, structureChargeeConduite=com.rte.siquael.dao.entity.Structure#8, lidLib=63kV Partenay les bonobonos
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Structure{sioPostesForStrIdCep=<uninitialized>, strLib=GdP de Longefan, strCmp=0, sioPostesForStrIdCcp=<uninitialized>, clients=<uninitialized>, sitAnomalies=<uninitialized>, strDtCre=1992-02-01, silPersonnesForStrId=<uninitialized>, siqCodifications=<uninitialized>, ssDtFin=null, strIndiceCur=-, sitEvenementExploits=<uninitialized>, strDir=5, strOss=25, sioLidsForStrIdCfd=<uninitialized>, sitSauveEtatSaisies=<uninitialized>, ssDtDeb=null, strSun=S, silActionAmeliorations=<uninitialized>, sioUtilisateurs=<uninitialized>, strCur=D86AC36BE9AF4798937552AE34228317, strDtSup=null, strLoc=St Jean de Maurienne, sioFiltreStds=<uninitialized>, strNumOrdre=5, strCodPos=73300, silAcaOuvrages=<uninitialized>, silGroupeClients=<uninitialized>, sioStructures=<uninitialized>, strId=103, sioProfils=<uninitialized>, sioProfils_1=<uninitialized>, sioLidsForStrIdCcd=<uninitialized>, sitEqfCresRexes=<uninitialized>, sioLits=<uninitialized>, structure=com.rte.siquael.dao.entity.Structure#41, strAdr2=Cedex 1708, typeStructure=com.rte.siquael.dao.entity.TypeStructure#GdP, strAdr1=X, strFax=0479644607, strVis=O, silPersonnesForStrIdApp=<uninitialized>, strTel=0479640532}
    org.hibernate.internal.util.EntityPrinter (EntityPrinter.java:121) - com.rte.siquael.dao.entity.Structure{sioPostesForStrIdCep=<uninitialized>, strLib=Lyon, strCmp=null, sioPostesForStrIdCcp=<uninitialized>, clients=<uninitialized>, sitAnomalies=<uninitialized>, strDtCre=1992-02-01, silPersonnesForStrId=<uninitialized>, siqCodifications=<uninitialized>, ssDtFin=null, strIndiceCur=-, sitEvenementExploits=<uninitialized>, strDir=5, strOss=95, sioLidsForStrIdCfd=<uninitialized>, sitSauveEtatSaisies=<uninitialized>, ssDtDeb=null, strSun=null, silActionAmeliorations=<uninitialized>, sioUtilisateurs=<uninitialized>, strCur=40E5174E8FAF449A82EEB7328647A127, strDtSup=null, strLoc=Lyon Cedex 03, sioFiltreStds=<uninitialized>, strNumOrdre=null, strCodPos=69399, silAcaOuvrages=<uninitialized>, silGroupeClients=<uninitialized>, sioStructures=<uninitialized>, strId=8, sioProfils=<uninitialized>, sioProfils_1=<uninitialized>, sioLidsForStrIdCcd=<uninitialized>, sitEqfCresRexes=<uninitialized>, sioLits=<uninitialized>, structure=null, strAdr2=BP 3109, typeStructure=com.rte.siquael.dao.entity.TypeStructure#CRES, strAdr1=3 bis, rue des Matelots, strFax=04XXXXXXXX, strVis=O, silPersonnesForStrIdApp=<uninitialized>, strTel=04XXXXXXXX}
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction (JdbcTransaction.java:113) - committed JDBC Connection
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction (JdbcTransaction.java:126) - re-enabling autocommit
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl (LogicalConnectionImpl.java:246) - Releasing JDBC connection
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl (LogicalConnectionImpl.java:264) - Released JDBC connection

对此有什么想法吗?或者也许有一些提示可以调查更多内容?

最佳答案

使用@Temporal注释,您将能够存储时间、日期和时间戳

特别是,@Temporal(TemporalType.DATE)将忽略时间,例如,如果您有一个bean,它使用仅映射为DATE的字段映射数据库表,您将看到数据库时间以这种方式 00:00:00

使用@Temporal(TemporalType.TIMESTAMP),时间将不会被忽略。

注意:有时,当您在数据库中检查日期字段时,即使您以时间戳方式存储,您也只能看到日期,这通常取决于您的客户端,例如在 sqldeveloper 中,您可以启用或禁用在字段中显示日期时间的 View 。

关于java - hibernate 保存或更新: no SqlStatement when a field is present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176738/

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