日期:2014-05-19  浏览次数:20632 次

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. 新手学习,关于赋值比较的有关问题