在休眠中从一行中选择一列 [英] Select one column from a row in hibernate
问题描述
I am trying to do a simple query to get a unique result back in hibernate here is my code.
我试图做一个简单的查询来获取hibernate中的唯一结果。 getName(Integer id){
Session session = getSessionFactory()。openSession();
String name =(String)session.createSQLQuery(SELECT name FROM users WHERE user_id =:userId)。setParameter(userId,id).uniqueResult();
返回名称;
$ / code>
public String getName(Integer id) {
Session session = getSessionFactory().openSession();
String name = (String)session.createSQLQuery("SELECT name FROM users WHERE user_id = :userId").setParameter("userId", id).uniqueResult();
return name;
}
返回的名称以HTML文本形式存储,其中包含html syntacx语言。我认为这是导致问题的原因,但它没有任何意义,我只想将它作为字符串返回。
The name that is being returned is stored as HTML text that includes html syntacx language. I think this is what is causing the problem but it doesnt make sense I just want to return it as a string.
它只发生在这个字段名称上,I可以得到行中的所有其他字段,但这一个它给了我错误。
It is only happening on this one field name, I can get every other field in the row but this one it gives me the error.
我得到一个异常。我得到的异常是:
I am getting an exception. The exception I am getting is
否JDBC类型的Dialect映射:-1;嵌套异常是org.hibernate.HibernateException
No Dialect mapping for JDBC type: -1; nested exception is org.hibernate.HibernateException
如何查询hibernate中某行的特定列?
How do you query for a specific column on a row in hibernate?
推荐答案
我想出了解决方案,显然Java没有可以映射到文本字段的类型,所以您必须在下面添加一个标量解决方案:谢谢帮助
I figured out the solution, apparently Java has not type that can be mapped to Text fields so you have to add a scalar solution below: Thanks for the help
session.createSQLQuery("SELECT name FROM users WHERE user_id = :userId").addScalar("name", Hibernate.TEXT).setParameter("userId", id).uniqueResult();
这篇关于在休眠中从一行中选择一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!