我在翻译这个 sql 时遇到问题:
select * from documentos d where
exists (select 1 from documento_timestamp dt
where d.id_documento = dt.id_documento
and dt.fecha between sysdate - 41 and sysdate -39
and dt.estado = 'RC');
使用条件查询到 Hibernate。
这是 Documento 实体映射:
@Entity
@Table(name = "DOCUMENTOS")
@SequenceGenerator(name = "SEQ_DOC_ELECTRONICOS", sequenceName = "SEQ_DOC_ELECTRONICOS", allocationSize = 1)
public class Documento implements Serializable { // NOPMD by gpautasso on 17/09/13 11:01
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_DOC_ELECTRONICOS")
@Column(name = "ID_DOCUMENTO")
private long idDocumento;
@Column(name = "COD_IDENTIFICATORIO")
private String codigoIdentificatorio;
@Column(name = "CUIT_CLIENTE")
private Long cuitCliente;
@Column(name = "RAZON_SOCIAL")
private String razonSocial;
@Column(name = "ORIGEN")
private String origen;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CODIGO_DOCUMENTO", referencedColumnName = "CODIGO_DOCUMENTO")
private CodigoDocumentos codigoDocumento;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "FECHA_ENVIO")
private Calendar fechaEnvio;
@Column(name = "EXTENSION")
private String extension;
@Column(name = "TAMANO")
private String tamano;
@Column(name = "BDOCUMENTO")
private byte[] documentoBynary;
@Lob
@Column(name = "XML_DOCUMENTO")
private String xmlDocumento;
@Column(name = "ESTADO")
private String estado;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "FECHA_ESTADO")
private Calendar fechaEstado;
@Column(name = "CLAVE_VINCULACION")
private String claveVinculacion;
@Column(name = "NOMBRE_ARCHIVO")
private String nombreArchivo;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TMSP_FECHA")
private Calendar tmspFecha;
@Column(name = "TMSP_ESTADO")
private String tmspEstado;
@Column(name = "COD_CLIENTE")
private Long codCliente;
@Column(name = "INFORMAR_TIB")
private String informarTIB;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "ID_DOCUMENTO")
private List<DocumentoDestinatarios> documentoDestinatariosList;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "ID_DOCUMENTO")
private List<DocumentoDestinatariosMail> documentoDestinatariosMailList;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "ID_DOCUMENTO")
private List<DocumentoInformacion> documentoInformacionList;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "ID_DOCUMENTO")
private List<DocumentoReferencias> documentoReferenciasList;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "ID_DOCUMENTO")
private List<DocumentoTimestamp> documentoTimestampList;
@Column(name = "FECHA_LIMITE_VISUALIZACION")
private Calendar fechaLimiteVisualizacion;
Documento_timestamp 实体映射:
@Entity
@Table(name = "DOCUMENTO_TIMESTAMP")
@SequenceGenerator(name = "SEQ_DOC_TIMESTAMP", sequenceName = "SEQ_DOC_TIMESTAMP", allocationSize = 1)
public class DocumentoTimestamp implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_DOC_TIMESTAMP")
@Column(name = "ID_TIMESTAMP")
private long idDocumentoTimestamp;
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST })
@JoinColumn(name = "ID_DOCUMENTO")
private Documento documento;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "FECHA")
private Date fecha;
@Column(name = "ESTADO")
private String estado;
@Column(name = "OBSERVACION")
private String observacion;
@Column(name = "ORIGEN")
private String origen;
@Column(name = "TIPO_DOC")
private String tipoDocumentoConsulta;
@Column(name = "NRO_DOC")
private Long numeroDocumentoConsulta;
@Column(name = "CUIT")
private Long cuit;
@Column(name = "RAZON_SOCIAL")
private String razonSocial;
@Column(name = "DESC_UNIFICADA")
private String descUnificada;
最后,这是我到目前为止想出的:
Session session = (Session) this.getSession();
Criteria criteria = session.createCriteria(Documento.class, "doc").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
DetachedCriteria timestampCriteria = DetachedCriteria.forClass(DocumentoTimestamp.class, "documento_timestamp");
if (!StringUtils.esVacioONull(request.getFechaRecepcionDesde())) {
Calendar fechaDesde = DateUtils.transform2Calendar((request.getFechaRecepcionDesde()) + "000000", DateUtils.YYYYMMDDHHMMSS);
timestampCriteria.add(Restrictions.ge("FECHA", fechaDesde));
}
if (!StringUtils.esVacioONull(request.getFechaRecepcionHasta())) {
Calendar fechaHasta = DateUtils.transform2Calendar((request.getFechaRecepcionHasta()) + "235959", DateUtils.YYYYMMDDHHMMSS);
timestampCriteria.add(Restrictions.le("FECHA", fechaHasta));
}
timestampCriteria.add(Restrictions.eq("ESTADO", "RC"));
timestampCriteria.add(Property.forName("documento_timestamp.documento").eqProperty("doc.idDocumento"));
criteria.add(Subqueries.exists(timestampCriteria.setProjection(Projections.property("documento_timestamp.documento"))));
这是堆栈跟踪
org.hibernate.MappingException: Unknown entity: null
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPropertyMapping(CriteriaQueryTranslator.java:596)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:547)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getTypeUsingProjection(CriteriaQueryTranslator.java:529)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getTypedValue(CriteriaQueryTranslator.java:587)
at org.hibernate.criterion.SimpleExpression.getTypedValues(SimpleExpression.java:94)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:315)
at org.hibernate.criterion.SubqueryExpression.createAndSetInnerQuery(SubqueryExpression.java:152)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:69)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at ar.com.ib.gde.domain.dao.DocumentoDaoImpl.consultaListadoDocumentosTimestamp(DocumentoDaoImpl.java:218)
at ar.com.ib.gde.domain.dao.DocumentoDaoImpl_vv3zuk_DocumentoDaoImpl.__WL_invoke(Unknown Source)
at weblogic.ejb.container.internal.SessionLocalMethodInvoker.invoke(SessionLocalMethodInvoker.java:31)
at ar.com.ib.gde.domain.dao.DocumentoDaoImpl_vv3zuk_DocumentoDaoImpl.consultaListadoDocumentosTimestamp(Unknown Source)
at ar.com.ib.gde.ejb.factory.DocumentosFactory.consultaListadoDocumentosTimestamp(DocumentosFactory.java:135)
at ar.com.ib.gde.business.ConsultaDocumento.consultaListadoDocumentosTimestamp(ConsultaDocumento.java:522)
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:601)
at com.bea.core.repackaged.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at com.oracle.pitchfork.intercept.MethodInvocationInvocationContext.proceed(MethodInvocationInvocationContext.java:103)
at ar.com.ib.gde.interceptors.RuleInterceptor.executeBusiness(RuleInterceptor.java:62)
at ar.com.ib.gde.interceptors.RuleInterceptor.execute(RuleInterceptor.java:29)
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:601)
at com.oracle.pitchfork.intercept.JeeInterceptorInterceptor.invoke(JeeInterceptorInterceptor.java:108)
at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at com.oracle.pitchfork.intercept.MethodInvocationInvocationContext.proceed(MethodInvocationInvocationContext.java:103)
at ar.com.ib.gde.interceptors.ErrorRuleInterceptor.execute(ErrorRuleInterceptor.java:25)
有任何想法吗?
提前致谢。