gpt4 book ai didi

java - 在使用 hibernate 和 struts 检索数据时,控制台中只会打印对象的地址

转载 作者:行者123 更新时间:2023-11-29 10:10:33 25 4
gpt4 key购买 nike

持久类:

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 + "]";
}

}

这是我的 DAO 类:

    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 retrieving" +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");

}

这是我的服务类:

    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 retrieveData() {
System.out.println("entry point retrieve data");
daoobj.retrieveFromDAO();
System.out.println( daoobj.retrieveFromDAO().toString());
System.out.println("after retrieving ");
return"SUCCESS";
}*/
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>LEAVE MANAGEMENT</title>
<h1>DATA SAVED SUCCESSFULLY!! CHECK THE DATABASE...........</h1>
</head>
<body>

<table border="1">
<tr>
<th>BANK ID</th>
<th>EMPNAMEE</th>
<th>LEAVES</th>

</tr>
<s:iterator value="mylist">
<tr>
<td><s:property value="bank_id"/></td>
<td><s:property value="emp_name"/></td>
<td><s:property value="leaves"/></td>

</tr>
</s:iterator>
</table>

它不在 JSP 中打印。早些时候我得到了对象的引用值。但是现在我按照 sanjeev 的建议在控制台中得到了值。它运作良好。然而,在 JSP 中,数据库中的所有值都没有迭代。请帮忙

最佳答案

您在控制台中得到的不是地址,而是引用。当您调用 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;
}

}

关于java - 在使用 hibernate 和 struts 检索数据时,控制台中只会打印对象的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37961224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com