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

📄 productoperation.java

📁 彩铃小项目的原代码 下载
💻 JAVA
字号:
package hibernate.iml;

import java.io.File;

import common.HibernateSessionFactory;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;

public class ProductOperation extends DBOperationParent {
	/**
	 * method update 更新记录
	 * @param object
	 * @param key
	 * @return 是否更新成功
	 * @throws HibernateException 
	 */
	public boolean update(Object objectTmp, String key)
			throws HibernateException {
		boolean returnCode = true;
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			Transaction tx = session.beginTransaction();
			Product object = (Product) session.load(Product.class,key);	//这一行要修改
			Product inObject = (Product) objectTmp;						//这一行要修改
			if(object==null) returnCode = false;
			else{
				//赋值语句要修改
				object.setAccessory(inObject.getAccessory());
				object.setInDate(inObject.getInDate());
				object.setMusicname(inObject.getMusicname());
				object.setUnitName(inObject.getUnitName());
				session.update(object);
				tx.commit();
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return returnCode;
	}

	/**
	 * method delete 删除指定记录,进行特殊处理,删除文件
	 * @param key
	 * @throws HibernateException 
	 */
	public void delete(String path,String key) throws HibernateException {
		try {
			if(!session.isOpen()) session = HibernateSessionFactory.currentSession();
			Transaction tx = session.beginTransaction();
			Product object = (Product) session.load(Product.class,key);
			//删除文件
			String fileName = object.getAccessory();
			if(fileName.length()>8){
				path += "\\" + fileName.substring(0,8) + "\\" + fileName;
				File fileDelete = new File(path);
				if(fileDelete.exists()) fileDelete.delete();
			}
			session.delete(object);
			tx.commit();
			HibernateSessionFactory.closeSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}		
	}

}

⌨️ 快捷键说明

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