hibernate 同一个类中多对一,一对多,不会用,请指教,讲解一下,最好是告诉我如何遍历

<p>hibernate 同一个类中多对一,一对多,不会用,请指教,讲解一下,最好是告诉我如何遍历userlist = userService.searchAll();这个结果集的容器的内容,用System.out.println(userlist);输出的是[com.test.bean.User@1ee5806, com.test.bean.User@708d23, com.test.bean.User@12bc86d, com.test.bean.User@1738d88, com.test.bean.User@150ece0, com.test.bean.User@1ef7b50, com.test.bean.User@18902ce] ,我要怎么遍历这个list

 

这个是我转出的SQL文件,t_user表里有7条数据,名字是汉字,随便打的

/*
Navicat MySQL Data Transfer
Source Host     : localhost:3306
Source Database : dhtmlxtree
Target Host     : localhost:3306
Target Database : dhtmlxtree
Date: 2012-04-06 16:24:29
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `pid` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FKCB63CCB6FA18D2A5` (`pid`),
  CONSTRAINT `FKCB63CCB6FA18D2A5` FOREIGN KEY (`pid`) REFERENCES `t_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', '闃块樋', null);
INSERT INTO `t_user` VALUES ('2', '鍚栧悥', '1');
INSERT INTO `t_user` VALUES ('3', '浣愮綏', '1');
INSERT INTO `t_user` VALUES ('4', '闆ⅷ', '2');
INSERT INTO `t_user` VALUES ('5', '绾风悍', '2');
INSERT INTO `t_user` VALUES ('6', '澶╁ぉ', '4');
INSERT INTO `t_user` VALUES ('7', '鍝堝搱', null);

 

这个是User.class的代码:

package com.test.bean; 
import java.util.Set; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue;
 import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import org.hibernate.annotations.LazyCollection; 
import org.hibernate.annotations.LazyCollectionOption; 

@Entity 
@Table(name="t_user") 
public class User { 
@Id 
@GeneratedValue 
private int id; 
private String name; 

@ManyToOne 
@JoinColumn(name="pid") 
private User parent; 
@OneToMany(mappedBy = "parent") 
@LazyCollection(LazyCollectionOption.EXTRA) 
private Set sons; 

public int getId() { return id; } 
public void setId(int id) { this.id = id; } 
public String getName() { return name; } 
public void setName(String name) { this.name = name; } 
public User getParent() { return parent; } 
public void setParent(User parent) { this.parent = parent; } 
public Set getSons() { return sons; } 
public void setSons(Set sons) { this.sons = sons; } } 

 UserAction.class代码: 

package com.test.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Student;
import com.test.bean.User;
import com.test.service.StudentService;
import com.test.service.UserService;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {

    private List<User> userlist;
    
    private User user= new User();
    @Resource
    private UserService userService;
    
    @Override
    public String execute() throws Exception {
    
        userlist = userService.searchAll();

        ActionContext.getContext().put("userlist", userlist);
        System.out.println(userlist);
        return SUCCESS;
    }
    
}

  

1个回答

用ForEach来遍历不就好了
for(User u:userlist){
System.out.println(u.getName());
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!