📄 productdao.java
字号:
package com.lideedu.yame.tree.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.lideedu.yame.tree.db.HibernateSessionFactory;
import com.lideedu.yame.tree.pojos.Product;
public class ProductDAO {
private Session session= null;
private Transaction tx = null;
public void save(Product product){
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
session.save(product);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if(tx != null)
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
tx = null;
}
}
public List queryAll(){
List list = null;
try {
session = HibernateSessionFactory.getSession();
list = session.createQuery("from Product p left join fetch p.productCategory").list();
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
public List queryByProductId(int productId){
List list = null;
try {
session = HibernateSessionFactory.getSession();
list = session.createQuery("from Product p left join fetch p.productCategory where p.productId="+productId).list();
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
public List queryByCategoryId(int categoryId){
List list = null;
try {
session = HibernateSessionFactory.getSession();
list = session.createQuery("from Product p left join fetch p.productCategory where p.productCategory.categoryId="+categoryId).list();
} catch (Exception e) {
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -