HQL中cast的使用

cuizhiyong 2008-03-21 11:48:16
HQL中如何实现把string类型转换成float?
...全文
695 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cchaha 2008-03-22
  • 打赏
  • 举报
回复
传float的封装类对象实例,类似下面:

String s="123";
Float f=new Float(s);
Query query=session.createQuery("from User user where user.s:s");
query.setParameter("s",s);
handong890 2008-03-21
  • 打赏
  • 举报
回复
数据库SQL2000
表 testfloat
字段 类型
id varchar
num float

vo
public class Testfloat implements java.io.Serializable {


// Fields

private String id;
private Float num;


// Constructors

/** default constructor */
public Testfloat() {
}


/** full constructor */
public Testfloat(Float num) {
this.num = num;
}


// Property accessors

public String getId() {
return this.id;
}

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

public Float getNum() {
return this.num;
}

public void setNum(Float num) {
this.num = num;
}

}


DAO

public List test(String a)
{
float f = Float.valueOf(a);
List list = null;
String sql = "from Testfloat where num > "+f+" ";
System.out.println(sql);
Query query = getSession().createQuery(sql);
list = query.list();
return list;
}

main 方法

TestfloatDAO tdao = new TestfloatDAO();
List list2 = null;
list2 =tdao.test("1.7");
System.out.println("查到" + list2.size());

hbm.xml
<hibernate-mapping>
<class name="com.mybbs.DAO.Testfloat" table="testfloat" schema="dbo" catalog="BBS">
<id name="id" type="java.lang.String">
<column name="id" length="50" />
<generator class="uuid.hex" />
</id>
<property name="num" type="java.lang.Float">
<column name="num" precision="53" scale="0" />
</property>
</class>
</hibernate-mapping>


运行正确可以正常查询
cuizhiyong 2008-03-21
  • 打赏
  • 举报
回复
hibernate 官方文档中说使用cast,但我试了试也不行
cuizhiyong 2008-03-21
  • 打赏
  • 举报
回复
楼上的你试过了吗?
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode 错误
handong890 2008-03-21
  • 打赏
  • 举报
回复
Float.valueOf(str)
这样转啊
比如HQL
String sql = "form xxx where i > " + Float.valueOf(str)
+ "";

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧