HQL查询无法显示

hibernate-release-5.2.9.Final

junit4.9

mysql使用5.1.29



package hibernate;


import java.util.Date;

import java.util.List;


import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;


public class StudentsTest {

  private SessionFactory sessionFactory;

  private Session session;

  private Transaction transaction;

@Before

public void init(){

//创建配置对象

Configuration config=new Configuration().configure();

config.addClass(Student.class);

//创建服务注册对象

ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

//创建会话工厂对象

sessionFactory=config.buildSessionFactory(serviceRegistry);

//会话对象

session=sessionFactory.openSession();

//开启事物

transaction=session.beginTransaction();

}

@After

public void destroy(){

transaction.commit();//提交事物

session.close();//关闭会话

sessionFactory.close();//关闭会话工厂

}

 @Test

        public void teststudent(){

     String sql="from Student";

     Query<Student> query=session.createQuery(sql);

     

     List<Student> stu=query.list();

     

     for(Student s:stu)

     {

     System.out.println("name"+stu);

     

     }

     

  

      }

http://img.mukewang.com/58da74e600016acd12920317.jpg

好好认真学习
浏览 1136回答 1
1回答

尧叔

重写Student 的toString()方法, 然后这样写for(Student s:stu)      {      System.out.println("name"+stu.toString());      }谢谢采纳!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java