Hibernate单表映射

单表操作

books表

create table books(
b_id varchar(32)primary key,
b_name varchar(40)
)

 

BookDAO

package dao;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;

import po.only.Books;

import config.HibernateSessionFactory;

public class BookDAO {

	/**
	 * @param args
	 */
	public void add(String name) { // 添加信息
		Books books = new Books();
		Session session = null;
		try {

			books.setBName(name);
			session = HibernateSessionFactory.getSession();
			session.beginTransaction();
			session.save(books);
			session.beginTransaction().commit();
			session.close();
		} catch (Exception e) {
			if (session != null)
				// TODO: handle exception
				session.close();
		}
	}

	public void update(String name, String reName) { // 更细信息
		Books books = new Books();
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery("from Books where BName=? ");
			query.setString(0, name);

			if (query.list().size() != 0) {
				books = (Books) query.list().get(0);
				books.setBName(reName);

				session.save(books);
				session.beginTransaction().commit();
				session.close();
			} else {
				System.out.println("nll");
			}

		} catch (Exception e) {
			if (session != null)
				// TODO: handle exception
				session.close();
			e.printStackTrace();
			System.out.println("ddd");
		}
	}

	public void delete(String name) {// 删除信息
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery("from Books where BName=?");
			query.setString(0, name);
			if (query.list().size() != 0) {
				Books books = (Books) query.list().get(0);

				session.beginTransaction();
				session.delete(books);
				session.beginTransaction().commit();
				session.close();
			} else {
				System.out.println("NULl");
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if (session != null) {
				session.close();
			}
		}
	}

	public void deleteAll(String name) {// 删除信息
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			session.beginTransaction();
			Query query = session
					.createQuery("delete from Books where BName=?");
		
				query.setString(0, name);
				System.out.println(query.executeUpdate());
				
				session.beginTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if (session != null) {
				session.close();
			}
		}
	}

	public void select(String name) {// 查新信息
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery("from Books where BName=? ");
			query.setString(0, name);
			Iterator iterator = query.iterate();
			while (iterator.hasNext()) {
				Books books = (Books) iterator.next();
				System.out.println(books.getBId());
				System.out.println(books.getBName());
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//new BookDAO().add("麦兜");
		 new BookDAO().update("Simon", "newName");
		//new BookDAO().deleteAll("麦兜");
		// new BookDAO().select("newname");
	}
}

 

 

 

阅读更多

没有更多推荐了,返回首页