首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

hibernate查询时的一个小疑点,困扰小弟我一整天了,详细有关问题入下

2012-01-18 
hibernate查询时的一个小问题,困扰我一整天了,详细问题入下:问题就是:查询时候报错了,不多说,直接贴代码和

hibernate查询时的一个小问题,困扰我一整天了,详细问题入下:
问题就是:查询时候报错了,不多说,直接贴代码和错误吧!
这个是Email实体类:

Java code
package com.alan.easyoa.entity;import java.util.ArrayList;import java.util.List;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.SequenceGenerator;@Entity@SequenceGenerator(name="emailSe",sequenceName="emailSequence")public class Email {    private Integer id;    //id    private List<Employee> employees = new ArrayList<Employee>();    //发送给那些员工    private List<Department> Departments = new ArrayList<Department>();    //发送给那些部门    private String title;        //邮件标题    private String content;        //邮件内容    private String sendTime;        //邮件发送时间    private boolean isSend;        //true代表发送,false代表接收        /*********getter 和   setter 方法********************/        @Id    @GeneratedValue(generator="emailSe")    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    @ManyToMany    public List<Employee> getEmployees() {        return employees;    }    public void setEmployees(List<Employee> employees) {        this.employees = employees;    }    @ManyToMany    public List<Department> getDepartments() {        return Departments;    }    public void setDepartments(List<Department> departments) {        Departments = departments;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    public String getSendTime() {        return sendTime;    }    public void setSendTime(String sendTime) {        this.sendTime = sendTime;    }    public boolean isSend() {        return isSend;    }    public void setSend(boolean isSend) {        this.isSend = isSend;    }}


Employee实体代码:
Java code
package com.alan.easyoa.entity;import java.io.Serializable;import java.util.ArrayList;import java.util.List;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;/** *  * 员工实体类 * @author Alan * */@Entitypublic class Employee implements Serializable {    private static final long serialVersionUID = 760623822991124471L;    private String email;    //Email也就是登录账号    private String password;    //密码    private String name;    //姓名    private Integer age;    //年龄    private String sex;        //性别    private String contactInfo;    //联系方式    private Department department;    //部门    private List<Email> emails = new ArrayList<Email>();    //该员工发送的所有邮件,这个基本没用,如果用它的话一次性全取出来服务器受不了        public Employee(){}        /*********getter 和   setter 方法********************/    @Id    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getContactInfo() {        return contactInfo;    }    public void setContactInfo(String contactInfo) {        this.contactInfo = contactInfo;    }    @ManyToOne    public Department getDepartment() {        return department;    }    public void setDepartment(Department department) {        this.department = department;    }        @ManyToMany(mappedBy="employees")    public List<Email> getEmails() {        return emails;    }    public void setEmails(List<Email> emails) {        this.emails = emails;    }} 



下面是查询方法:
Employee 这个是实体类,PageUtil 这个是帮助类,具体代码就不贴出来了啦。

Java code
    private HibernateTemplate hibernateTemplate = new HibernateTemplate();    @SuppressWarnings("unchecked")    public List<Email> getEmailsByEmployee(final Employee e, final boolean isInbox,final PageUtil pageUtil) {        hibernateTemplate.setSessionFactory(new AnnotationConfiguration().configure().buildSessionFactory());        return (List<Email>)hibernateTemplate.execute(new HibernateCallback(){            public Object doInHibernate(Session session)                    throws HibernateException, SQLException {                session.beginTransaction();                //最多读取多少条记录                int max = (pageUtil.getCurrentPage()+1)*pageUtil.getPageSize();                //从那一条开始读取                int fister = pageUtil.getCurrentPage()*pageUtil.getPageSize();                pageUtil.setCurrentPage(pageUtil.getCurrentPage()+1);                //pageUtil.setAllRow((Integer)session.createQuery("count(*)from Email e ").uniqueResult());                Object object = session.createQuery("from Email e where e.employees = "+e).setFirstResult(fister).setMaxResults(max).list();                session.getTransaction().commit();                return object;            }});    }


Object object = session.createQuery("from Email e where e.employees = "+e).setFirstResult(fister).setMaxResults(max).list(); 这句话报错
错误如下:

Exception in thread "main" org.springframework.orm.hibernate3.HibernateQueryException: unexpected char: '@' [from com.alan.easyoa.entity.Email e where e.employees = com.alan.easyoa.entity.Employee@134eb84]; nested exception is org.hibernate.QueryException: unexpected char: '@' [from com.alan.easyoa.entity.Email e where e.employees = com.alan.easyoa.entity.Employee@134eb84]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:657)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
at com.alan.easyoa.dao.impl.EmailDaoImpl.getEmailsByEmployee(EmailDaoImpl.java:25)
at com.alan.easyoa.test.insertDate.testList(insertDate.java:78)
at com.alan.easyoa.test.insertDate.main(insertDate.java:83)
Caused by: org.hibernate.QueryException: unexpected char: '@' [from com.alan.easyoa.entity.Email e where e.employees = com.alan.easyoa.entity.Employee@134eb84]
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1293)
at $Proxy9.createQuery(Unknown Source)
at com.alan.easyoa.dao.impl.EmailDaoImpl$1.doInHibernate(EmailDaoImpl.java:35)


at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 4 more


junit测试代码如下:
@Test
public void testList(){
PageUtil pageUtil = new PageUtil();
pageUtil.setCurrentPage(1);
pageUtil.setFirstPage(true);
pageUtil.setPageSize(4);
EmailDaoImpl empl = new EmailDaoImpl();
Employee employee = new Employee();
employee.setEmail("alan@easyoa.com");
List<Email> emails = empl.getEmailsByEmployee(employee, true, pageUtil);
System.out.println(emails.size());
}

各位前辈能帮忙看看嘛?先谢谢了?小弟为这个东西困扰一整天了哦。

我总是觉得是这个段hsql语句写得有问题但是又不知道问题出在哪里:
Object object = session.createQuery("from Email e where e.employees = "+e).setFirstResult(fister).setMaxResults(max).list();
我把我的需求简单将一下吧,那个Email实体类对应一个email表,那个Employee也对应一个表employee,由于email和employee是多对多关系,所以hibernate自动给他们添加了一个中间表email_employee,其中有两个列,一个是email表的id一个是employee的email那个字段,假如这里面都有相应的数据,那么我现在想取employee表中的某个员工发送的邮件,每次只取固定的条数,那么此时我应该怎么写hsql语句呢?不知道我表达清楚了没有。
谢谢了。



[解决办法]
如果employee类中的email是一个员工的标示的话,你可以这样写hsql
Employee epl=session.createQuery("from Employee e where e.email=:eml").uniqueResult();


 List<Email> list= epl.getEmails();//返回该员工所对应发送的email集合
//可以选择操作集合 如果直接写hsql语句这样不知道可不可行没试过
createQuery("select e.emails from Employy e where e.email=:eml").setString("eml",值)
.setFirstResult(start).setMaxResult(end);

没有带电脑,只是个人想法,不知道可不可行,不过可以去试试

热点排行