⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatetest.java

📁 Java 敏捷开发--使用Spring Hibernate 和 Eclipse源码
💻 JAVA
字号:
package com.visualpatterns.timex.test;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.visualpatterns.timex.model.Department;

//TODO: Add Javadoc comments
public class HibernateTest
{
    public static void main(String args[]) throws Exception
    {
        SessionFactory sessionFactory = new Configuration().configure()
                .buildSessionFactory();
        Session session = sessionFactory.openSession(); // changed to
        // openSession
        Transaction tx = session.beginTransaction();
        Department department;

        // Demo 1: Get single record
        department = (Department) session.get(Department.class, "IT");
        System.out.println("Name for IT = " + department.getName());

        // Demo 2: Get all records
        List departmentList = session.createQuery("from Department").list();
        for (int i = 0; i < departmentList.size(); i++)
        {
            department = (Department) departmentList.get(i);
            System.out.println("Row " + (i + 1) + "> " + department.getName()
                    + " (" + department.getDepartmentCode() + ")");
        }

        tx.commit();
        session.close();
        sessionFactory.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -