使用hibernate和struts在数据库中恢复数据时,只有对象的地址被打印出来

时间:2016-06-22 07:25:31

标签: java hibernate jsp

持久课程:

/me/friends

这是我的DAO课程:

package com.sheeba.joshua;
import javax.persistence.*;

@Entity
@Table(name="DB_LEAVE")
public class DB_LEAVE {

     public String bank_id;
        public String emp_name;
        public String leaves;

        @Id
        @Column(name="BANK_ID")
        public String getBank_id() {
            return bank_id;
        }
        public void setBank_id(String bank_id) {
            this.bank_id = bank_id;
        }
         @Column(name="EMP_NAME")
        public String getEmp_name() {
            return emp_name;
        }
        public void setEmp_name(String emp_name) {
            this.emp_name = emp_name;
        }
         @Column(name="LEAVES_APPLIED")
        public String getLeaves() {
            return leaves;
        }
        public void setLeaves(String leaves) {
            this.leaves = leaves;
        }

        @Override
        public String toString() {
            return "DB_LEAVE [bank_id=" + bank_id + ", emp_name=" + emp_name + ", leaves=" + leaves + "]";
        }

}

这是我的服务类:

    package com.sheeba.joshua;
import com.sheeba.joshua.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class leaveDAO {
    DB_LEAVE modobj = new DB_LEAVE();
    leaveAction act=new leaveAction();
   List<DB_LEAVE> mylist;

    @SessionTarget
    Session session;

    @TransactionTarget
    Transaction t;

    public void saveDetails(DB_LEAVE modobj){
        System.out.println("inside DAO , before query");
        mylist = new ArrayList<DB_LEAVE>();

        System.out.println("name" + modobj.getBank_id());
        try {
        session.save(modobj);
        System.out.println("before retreving" +session.createQuery("from DB_LEAVE").list());
       List mylist = session.createQuery("from DB_LEAVE").list();
       for (Iterator iterator = 
               mylist.iterator(); iterator.hasNext();){
           modobj = (DB_LEAVE) iterator.next(); }
       System.out.println("before setting to action class list" +mylist);

        }

   catch(Exception e)
   {e.printStackTrace();}

        System.out.println("inside DAO , after query");

    }

JSP

    package com.sheeba.joshua;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@ParentPackage("hibernate-default")
@InterceptorRef("basicStackHibernate")
public class leaveService extends ActionSupport implements ModelDriven<DB_LEAVE> {
    private static final long serialVersionUID = -6659925652584240539L;
    leaveDAO daoobj = new leaveDAO();

    DB_LEAVE moddriven = new DB_LEAVE();
    public List<DB_LEAVE> myList = new ArrayList<DB_LEAVE>();

    public List<DB_LEAVE> getMyList() {
        return myList;
    }

    public void setMyList(List<DB_LEAVE> myList) {
        this.myList = myList;
    }


    public String test(){
        return"success";
    }

    public String saveDetService() {
        System.out.println("service entry point");
       daoobj.saveDetails(moddriven); 
        return SUCCESS;
    }

    @Override
    public DB_LEAVE getModel() {
        return moddriven;
    }

    public DB_LEAVE getDb_LEAVE(){
        return moddriven;
    }

    public void setDB_LEAVE(DB_LEAVE moddriven) {

        this.moddriven = moddriven;
   }

   /* public String retreiveData() {
        System.out.println("entry point retreive data");
        daoobj.retreiveFromDAO();
        System.out.println( daoobj.retreiveFromDAO().toString());
        System.out.println("after retreiving ");
        return"SUCCESS";
    }*/
}

它不是在JSP中打印。早些时候我得到了对象的参考值。但是我按照sanjeev的建议在控制台中获取值。它工作正常。然而,在JSP中,数据库中的所有值都不会被迭代。请帮忙

6 个答案:

答案 0 :(得分:2)

您需要覆盖DB_LEAVE类中的toString()方法,以可读格式打印数据。

默认情况下,toString()方法打印

getClass().getName() + '@' + Integer.toHexString(hashCode())

这不是可读格式。

您可以在DB_LEAVE类中覆盖如下: -

@Override
public String toString() {
    //Create a string with the property name and its corresponding value which is required in the logging.
}

答案 1 :(得分:2)

您在控制台中获得的不是地址,而是参考。当您调用System.out.println并将对象传递给它时,Java不知道如何表示该对象。因此,默认情况下,它调用对象的toString()方法。如果未指定自己的实现,则从Object类继承的默认实现将打印对象的引用。

因此,如果您想要自定义对象的表示,只需覆盖该方法。

示例:

 public class DB_LEAVE{

   //your fields and methods
   ...

   /**
   * Returns a String that is a representation of the current object.
   **/
   @Override
   public String toString(){
     return "DB_LEAVE #" + this.id;
   }

}

答案 2 :(得分:1)

在班级leaveDAO.java中,您会看到以下语句仅打印地址:

System.out.println("before retreving" +session.createQuery("from DB_LEAVE").list());
System.out.println("before setting to action class list" +mylist);

如果您在课程toString()中覆盖了DB_LEAVE,则会看到对象的属性被打印出来。

您可以按以下方式覆盖toString()中的DB_LEAVE方法并查看结果:

@Override
public String toString() {
    return "DB_LEAVE [bank_id=" + bank_id + ", emp_name=" + emp_name + ", leaves=" + leaves + "]";
}

我认为您的服务类即leaveService.java应该具有名称为mylist的实例级变量以及相应的getter和setter方法。应该从saveDetService()方法填充此列表,以便您可以从JSP页面检索列表的元素。

答案 3 :(得分:1)

尝试覆盖DB_LEAVE类中的toString()方法。

答案 4 :(得分:1)

在DAO中,您打印了列表对象,这就是为什么在控制台中打印对象。您想在控制台或JSP中打印对象值吗?

答案 5 :(得分:1)

按照Sanjeev的建议,我在服务类中添加了myList的getter setter,这也就是为什么它没有被发送到JSP.Now它的工作正常。谢谢。