如何使用Java和Hibernate从我的数据库打印整个表?

时间:2016-02-03 09:17:52

标签: java hibernate

如何使用Java和Hibernate从数据库及其内容中打印出整个表?

到目前为止,我的代码只打印了我需要的一行信息。它只打印作者:

package models;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dao.HibernateDataDAO;
import hibernate.Books;

public class TestModel {
    HibernateDataDAO data;
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void test() {
        System.out.println("Testing hibernate");
        //This Books is just a test, in the real implementation it will be added by the user.
        Books books = new Books();

        books.setTitle("A Game of Thrones");
        books.setAuthor("George RR Martin");

        data.insertBooks(books);

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        books = (Books) session.get(hibernate.Books.class, 1);
        System.out.println(books.getAuthor());


        //int id = 3;
        //We set this as 'increment' so that keys are set by database and not by users.
        //data.deleteBooks(books, id);
    }

    public HibernateDataDAO getData() {
        return data;
    }

    public void setData(HibernateDataDAO data) {
        this.data = data;
    }   
}

我的表名为 books 。其中有3列:booksKey,title和author。 booksKey是一个整数,title / author是字符串。如何打印整个表格以便打印出所有信息行?

我尝试修改上面的代码并执行此操作,但是,没有打印:

    Query query = session.createSQLQuery("select *from books");
    List<Books> list = query.list();
    for(Books test: list) {
        System.out.println(test.getAuthor());
    }

如果有人可以请求帮助 - 最好告诉我要添加/修复的代码,这将是非常棒的。我完全搞不清楚了!

HibernateDataDAO:

package dao;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import hibernate.Books;
import hibernate.Music;
import hibernate.Contacts;
import hibernate.Movies;

//DAO = data access object.
public class HibernateDataDAO {
    private SessionFactory sessionFactory;
    private Transaction transaction;

    public void insertBooks(Books books) {
        Session session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.save(books);
            transaction.commit();
        } 
        catch (Exception e) {
             if (transaction != null) transaction.rollback();
             throw e;
        }
        finally {
             session.close();
        }
    }

    public void deleteBooks(Books books, int booksKey){
        Session session = sessionFactory.openSession();
        try{
            transaction = session.beginTransaction();
            Object persistentInstance = session.load(hibernate.Books.class, booksKey);
            if (persistentInstance != null) {
                session.delete(persistentInstance);
            }
            transaction.commit();
        }catch(HibernateException e){
            if(transaction!= null) transaction.rollback();
            e.printStackTrace();
        }
        finally{
            session.close();
        }
    }

    public void insertMusic(Music music) {
        Session session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.save(music);
            transaction.commit();
        } 
        catch (Exception e) {
             if (transaction != null) transaction.rollback();
             throw e;
        }
        finally {
             session.close();
        }
    }

    public void deleteMusic(Music music, int musicKey){
        Session session = sessionFactory.openSession();
        try{
            transaction = session.beginTransaction();
            Object persistentInstance = session.load(hibernate.Music.class, musicKey);
            if (persistentInstance != null) {
                session.delete(persistentInstance);
            }
            transaction.commit();
        }catch(HibernateException e){
            if(transaction!= null) transaction.rollback();
            e.printStackTrace();
        }
        finally{
            session.close();
        }
    }

    public void insertMovies(Movies movies) {
        Session session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.save(movies);
            transaction.commit();
        } 
        catch (Exception e) {
             if (transaction != null) transaction.rollback();
             throw e;
        }
        finally {
             session.close();
        }
    }

    public void deleteMovies(Movies movies, int movieKey){
        Session session = sessionFactory.openSession();
        try{
            transaction = session.beginTransaction();
            Object persistentInstance = session.load(hibernate.Movies.class, movieKey);
            if (persistentInstance != null) {
                session.delete(persistentInstance);
            }
            transaction.commit();
        }catch(HibernateException e){
            if(transaction!= null) transaction.rollback();
            e.printStackTrace();
        }
        finally{
            session.close();
        }
    }

    public void insertContacts(Contacts contacts) {
        Session session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.save(contacts);
            transaction.commit();
        } 
        catch (Exception e) {
             if (transaction != null) transaction.rollback();
             throw e;
        }
        finally {
             session.close();
        }
    }

    public void deleteContacts(Contacts contacts, int contactKey){
        Session session = sessionFactory.openSession();
        try{
            transaction = session.beginTransaction();
            Object persistentInstance = session.load(hibernate.Contacts.class, contactKey);
            if (persistentInstance != null) {
                session.delete(persistentInstance);
            }
            transaction.commit();
        }catch(HibernateException e){
            if(transaction!= null) transaction.rollback();
            e.printStackTrace();
        }
        finally{
            session.close();
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }       
}

2 个答案:

答案 0 :(得分:1)

试试这个:

Criteria criteria = session.createCriteria(Books.class);
List<Books> list = criteria.list();
for(Books test: list) {
    System.out.println(test.getBooksKey + " " + test.getAuthor() + " " + test.getTitle());
}

如果您有任何错误,请告诉我相关信息。

答案 1 :(得分:0)

您应该像以下一样更改您的代码;

Query qry=session.createQuery("from  books");
List<Books> user=(List<Books>) qry.list();
session.getTransaction().commit();
session.close();
List<Books> list = query.list();
for(Books test: list) {
    System.out.println(test.getAuthor());
}