在以下程序中,我将输出作为Hibernate.UserDetails@224b4d61
我该如何解决这个问题?
以下是我正在使用的两个程序,我需要打印username
HibernateTest:
public class HibernateTest {
public static void main(String[] args)
{
SessionFactory sessionfactory = new Configuration().configure("Hibernate/hibernate.cfg.xml").buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(UserDetails.class);
criteria.add(Restrictions.eq("UserName","User5"));
List<UserDetails> user =(List<UserDetails>) criteria.list();
session.getTransaction().commit();
session.close();
for(UserDetails u : user)
{
System.out.println("Name of User is : "+u);
}
}
}
的UserDetails:
@Entity
public class UserDetails
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int UserId;
private String UserName;
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
}
答案 0 :(得分:0)
您目前正在尝试打印整个UserDetails对象,但我认为您只是尝试打印用户名...
System.out.println("Name of User is : " + u.getUserName());
答案 1 :(得分:0)
您应该覆盖toString method。
@Entity
public class UserDetails
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int UserId;
private String UserName;
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String toString() {
return getUserName() + " (id = " + getUserId().toString() + ")"
}