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.
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;
}
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.
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
No Dialect mapping for JDBC type: -1; nested exception is org.hibernate.Hibern开发者_高级运维ateException
How do you query for a specific column on a row in hibernate?
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();
Then that should work. Use the Type hibernate annotation.
Add
@Type(type="text")
To your mapping.
精彩评论