精通Hibernate:对象关系映射基础(1)(3)
分享于 点击 15356 次 点评:263
3、实例
本节的代码下载地址:http://down.51cto.com/data/326754
主要的BusinessService.java
- package mypack;
- import org.hibernate.*;
- import org.hibernate.cfg.Configuration;
- import java.util.*;
- public class BusinessService{
- public static SessionFactory sessionFactory;
- static{
- try{
- Configuration config = new Configuration()
- .setNamingStrategy( new MyNamingStrategy() )
- .configure(); //加载hibernate.cfg.xml文件中配置的信息
- sessionFactory = config.buildSessionFactory();
- }catch(RuntimeException e){e.printStackTrace();throw e;}
- }
- public Customer loadCustomer(long customer_id){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Customer customer=(Customer)session.get(Customer.class,new Long(customer_id));
- tx.commit();
- return customer;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void saveCustomer(Customer customer){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.save(customer);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void loadAndUpdateCustomer(long customerId) {
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Customer customer=(Customer)session.get(Customer.class,new Long(customerId));
- customer.setDescription("A lovely customer!");
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void updateCustomer(Customer customer){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.update(customer);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void saveDictionary(Dictionary dictionary) {
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.save(dictionary);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void updateDictionary(Dictionary dictionary){
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- session.update(dictionary);
- tx.commit();
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public Dictionary loadDictionary(long dictionary_id) {
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Dictionary dictionary=(Dictionary)session.get(Dictionary.class,new Long(dictionary_id));
- tx.commit();
- return dictionary;
- }catch (RuntimeException e) {
- if (tx != null) {
- tx.rollback();
- }
- throw e;
- } finally {
- session.close();
- }
- }
- public void printCustomer(Customer customer){
- System.out.println("name:"+customer.getName());
- System.out.println("sex:"+customer.getSex());
- System.out.println("description:"+customer.getDescription());
- System.out.println("avgPrice:"+customer.getAvgPrice());
- System.out.println("totalPrice:"+customer.getTotalPrice());
- }
- public void printDictionary(Dictionary dictionary){
- System.out.println("type:"+dictionary.getType());
- System.out.println("key:"+dictionary.getKey());
- System.out.println("value:"+dictionary.getValue());
- }
- public void test(){
- Customer customer=new Customer("Laosan","Zhang",'M',new HashSet(),"A good citizen!");
- Order order1=new Order("Order001",new Double(100),customer);
- Order order2=new Order("Order002",new Double(200),customer);
- customer.getOrders().add(order1);
- customer.getOrders().add(order2);
- saveCustomer(customer);
- customer=new Customer("Laowu","Wang",'M',new HashSet(),null);
- saveCustomer(customer);
- customer=loadCustomer(1);
- printCustomer(customer);
- customer.setDescription("An honest customer!");
- updateCustomer(customer);
- loadAndUpdateCustomer(1);
- Dictionary dictionary=new Dictionary("SEX","M","MALE");
- saveDictionary(dictionary);
- dictionary=loadDictionary(1);
- dictionary.setValue("MAN");
- updateDictionary(dictionary);
- dictionary=loadDictionary(1);
- printDictionary(dictionary);
- }
- public static void main(String args[]) {
- new BusinessService().test();
- sessionFactory.close();
- }
- }
用户点评