hibernate SQLQuery 查询问题
初学hibernate,今天试着用SQLQuery去查询指定字段,可是总是报列名无效,请各位高手帮忙看下是什么问题。。。
以下是代码:
测试:
String sql = "SELECT NAME as {per.name} from PERSON per";
SQLQuery sqlQuery = session.createSQLQuery(sql);
sqlQuery.addEntity("per", Person.class);
List<Person> persons = sqlQuery.list();
for (Person person : persons) {
System.err.println(person.getName());
}
Person类
@Entity
public class Person {
private Integer pno;
private String name;
private String email;
private Integer age;
private Date birthDate;
private String yourName;
@Transient
public String getYourName() {
return yourName;
}
public void setYourName(String yourName) {
this.yourName = yourName;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getPno() {
return pno;
}
public void setPno(Integer pno) {
this.pno = pno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Temporal(TemporalType.DATE)
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
异常:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2108)
at org.hibernate.loader.Loader.list(Loader.java:2103)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1696)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at org.alen.util.OperPerson.main(OperPerson.java:31)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 列名 pno0_0_ 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2217)
... 7 more
String sql = " select 表列名 as 实体属性 from 表 ";
createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(实体.class)).list();
SELECT NAME AS name FROM STUDENT
String sql = "SELECT 表别名.字段 as {实体别名.属性} from 表 as 表别名";
Query query = session.createSQLQuery(sql);
query.addEntity("实体别名", 实体.class);
query.list();
Session session = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
String hql = "select new Student(s.name) from Student s";
Query query = session.createQuery(hql);
List<Student> list = query.list();
//假设你的数据库表叫: TN_STUDENT 里面的字段列名为: CN_NAME
String sql = "SELECT table.CN_NAME as {s.name} from TN_STUDENT as table";
Query query = session.createSQLQuery(sql);
query.addEntity("s", Student.class);
query.list();
String sql = "SELECT stu.NAME as {s.name} from STUDENT stu";
SQLQuery sqlQuery = session.createSQLQuery(sql);
sqlQuery.addEntity("s",Student.class);
List<Student> list = sqlQuery.list();
for (Student student : list) {
System.err.println(student.getName());
}
异常:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 列名 id0_0_ 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)