我有一个简单的 HQL 查询,我正在尝试执行以返回日期列表。我的映射将日期定义为日历。映射插入可以毫无问题地成功插入,并使用映射回其属性的正确数据值进行检索。例子:
@Entity
@org.hibernate.annotations.Proxy(lazy = false)
@Table(name = "TABLE_T")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Table {
@Column(name = "SOME_ID", nullable = false)
private Long stdIdNum = null;
@Column(name = "END_DATE", nullable = true)
private Calendar endDate;
但是,如果我执行以下 HQL 语句:
Query query = hsession.createQuery("select endDate from Table");
results = query.list();
我得到以下异常:
java.lang.IllegalStateException:节点没有数据类型:org.hibernate.hql.internal.ast.tree.IdentNode -[IDENT] IdentNode:'end_date' {originalText=end_date}
在 org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:154) 在 org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:865) 在 org.hibernate.hql .internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:650) 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:651) 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement (HqlSqlBaseWalker.java:287) 在 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235) 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl。在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:101) 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80) 在 org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:101) 编译(QueryTranslatorImpl.java:136) .hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119) 在 org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214) 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java :192) 在 org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537) 在AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214) 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192) 在 org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537) 在AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214) 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192) 在 org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537) 在
我显然正在使用带有 sqlite 后端 sqlite-jdbc-3.7.2.jar 的休眠版本 4.0.0。这是我的 SQLDialect:
import java.sql.Types;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.type.DateType;
import org.hibernate.type.IntegerType;
public class SQLiteDialect extends Dialect {
public SQLiteDialect() {
super();
registerColumnType(Types.BIT, "integer");
registerColumnType(Types.TINYINT, "tinyint");
registerColumnType(Types.SMALLINT, "smallint");
registerColumnType(Types.INTEGER, "integer");
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.FLOAT, "float");
registerColumnType(Types.REAL, "real");
registerColumnType(Types.DOUBLE, "double");
registerColumnType(Types.NUMERIC, "numeric");
registerColumnType(Types.DECIMAL, "decimal");
registerColumnType(Types.CHAR, "char");
registerColumnType(Types.VARCHAR, "varchar");
registerColumnType(Types.LONGVARCHAR, "longvarchar");
registerColumnType(Types.DATE, "date");
registerColumnType(Types.TIME, "time");
registerColumnType(Types.TIMESTAMP, "longvarchar");
registerColumnType(Types.BINARY, "blob");
registerColumnType(Types.VARBINARY, "blob");
registerColumnType(Types.LONGVARBINARY, "blob");
// registerColumnType(Types.NULL, "null");
registerColumnType(Types.BLOB, "blob");
registerColumnType(Types.CLOB, "clob");
registerColumnType(Types.BOOLEAN, "integer");
registerColumnType(Types.NULL, "null");
registerHibernateType(Types.NULL, "null");
registerFunction("concat", new VarArgsSQLFunction(IntegerType.INSTANCE, "",
"||", ""));
registerFunction("mod", new SQLFunctionTemplate(IntegerType.INSTANCE,
"?1 % ?2"));
registerFunction("substr", new StandardSQLFunction("substr",
IntegerType.INSTANCE));
registerFunction("substring", new StandardSQLFunction("substr",
IntegerType.INSTANCE));
registerFunction("replace", new StandardSQLFunction("replace",
IntegerType.INSTANCE));
registerFunction( "current_time", new NoArgSQLFunction( "date('now')", DateType.INSTANCE, false ) );
registerFunction( "current_timestamp", new NoArgSQLFunction( "date('now')", DateType.INSTANCE, false ) );
registerFunction( "current_date", new NoArgSQLFunction( "date('now')", DateType.INSTANCE, false ) );
}
@Override
public boolean supportsIdentityColumns() {
return true;
}
/*
public boolean supportsInsertSelectIdentity() {
return true; // As specify in NHibernate dialect
}
*/
@Override
public boolean hasDataTypeInIdentityColumn() {
return false; // As specify in NHibernate dialect
}
/*
public String appendIdentitySelectToInsert(String insertString) {
return new StringBuffer(insertString.length()+30). // As specify in NHibernate dialect
append(insertString).
append("; ").append(getIdentitySelectString()).
toString();
}
*/
@Override
public String getIdentityColumnString() {
// return "integer primary key autoincrement";
return "integer";
}
@Override
public String getIdentitySelectString() {
return "select last_insert_rowid()";
}
@Override
public boolean supportsLimit() {
return true;
}
// Added fix from bug, http://code.google.com/p/hibernate-sqlite/issues/detail?id=1
@Override
public boolean bindLimitParametersInReverseOrder() {
return true;
}
@Override
public String getLimitString(String query, boolean hasOffset) {
return new StringBuffer(query.length() + 20).append(query).append(
hasOffset ? " limit ? offset ?" : " limit ?").toString();
}
@Override
public String getLimitString(final String s, final int min, final int max) {
String limitString = super.getLimitString(s, min, max);
return limitString;
}
@Override
public boolean supportsTemporaryTables() {
return true;
}
@Override
public String getCreateTemporaryTableString() {
return "create temporary table if not exists";
}
@Override
public boolean dropTemporaryTableAfterUse() {
return false;
}
@Override
public boolean supportsCurrentTimestampSelection() {
return true;
}
@Override
public boolean isCurrentTimestampSelectStringCallable() {
return false;
}
@Override
public String getCurrentTimestampSelectString() {
return "select current_timestamp";
}
@Override
public boolean supportsUnionAll() {
return true;
}
@Override
public boolean hasAlterTable() {
return false; // As specify in NHibernate dialect
}
@Override
public boolean dropConstraints() {
return false;
}
@Override
public String getAddColumnString() {
return "add column";
}
@Override
public String getForUpdateString() {
return "";
}
@Override
public boolean supportsOuterJoinForUpdate() {
return false;
}
@Override
public String getDropForeignKeyString() {
throw new UnsupportedOperationException(
"No drop foreign key syntax supported by SQLiteDialect");
}
@Override
public String getAddForeignKeyConstraintString(String constraintName,
String[] foreignKey, String referencedTable, String[] primaryKey,
boolean referencesPrimaryKey) {
throw new UnsupportedOperationException(
"No add foreign key syntax supported by SQLiteDialect");
}
@Override
public String getAddPrimaryKeyConstraintString(String constraintName) {
throw new UnsupportedOperationException(
"No add primary key syntax supported by SQLiteDialect");
}
@Override
public boolean supportsIfExistsBeforeTableName() {
return true;
}
@Override
public boolean supportsCascadeDelete() {
return false;
}
}
更新 - 为什么我不使用日期?
好吧,有两个相关的原因。这是日期和时间概念的一个非常糟糕的实现,它被日历类所取代。
Calendar 类虽然有所改进,但也有很多不足之处,因此对于认真的日期/时间工作,每个人都推荐 JodaTime。在 Java 7 中,可能还会有一个用于日历的替代 API,目前称为 JSR-310。