学习hibernate中的一些问题及知识(2)

来源:互联网 发布:软件行业职位分类 编辑:程序博客网 时间:2024/06/07 17:24

ThreadLocal是什么? 

ThreadLocal是解决线程安全问题一个很好的思路,ThreadLocal类中有一个Map,用于存储每一个线程的变量副本,Map中元素的键为线程对象,而值对应线程的变量副本,由于Key值不可重复,每一个“线程对象”对应线程的“变量副本”,而到达了线程安全。 

几大查询的区别:

get查询:


只能根据主键查,如果找不到,就返回一个空对象,就会报空指针错误


/**
* get查询
*/
public void getMethod() {
// session
Session session = HibernateUtil.currentSession();
// 如果找不到,就返回一个空对象
ProjectInfo p = session.get(ProjectInfo.class, "2B0493E89966420EB3BE97265BB0F01");
System.out.println(p.getProjectId());
HibernateUtil.closeSession();
}

load查询:


只能根据主键查,如果找不到,就报错.
/**
* load查询
*/
public void loadMethod() {
// session
Session session = HibernateUtil.currentSession();
// 如果找不到,就报错No row with the given identifier exists
ProjectInfo p = session.load(ProjectInfo.class, "2B0493E89966420EB3BE97265BB0F01");
System.out.println(p);
HibernateUtil.closeSession();
}

HQL:

/**
* hql查询
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void hqlMethod2() {
// session
Session session = HibernateUtil.currentSession();
// 查询所有的数据,这里操作的是实体对象的名字,不是表名,区分大小写,占位符从0开始,要区分和jdbc的位置
String hql = "from ProjectInfo where projectId = ?";
// 创建一个Query
Query query = session.createQuery(hql);
query.setString(0, "2B0493E89966420EB3BE97265BB0F011");
// 返回查询结果,只查询一条数据
ProjectInfo p = (ProjectInfo)query.uniqueResult();
System.out.println(p.getProjectName());
Hi


不操作表名,操作实体类的类名,返回的是List集合,要查某行数据时,加where后面跟实体类的属性


只查询一条数据:


ProjectInfo p = (ProjectInfo)query.uniqueResult();

分页查询:
/**
* hql分页查询
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void hqlMethod5() {
// session
Session session = HibernateUtil.currentSession();
// 查询所有的数据,这里操作的是实体对象的名字,不是表名,区分大小写
String hql = "from ProjectInfo ";
// 创建一个Query
Query query = session.createQuery(hql);
// 设置分页信息
// 从哪条数据开始
query.setFirstResult(10);
// 每页显示的数据条数
query.setMaxResults(10);
// 返回查询结果,只查询一条数据,只有一个字段
List<ProjectInfo> list = query.list();
System.out.println(list.size());
HibernateUtil.closeSession();
}

日常报错:
/**
* 多表转查字段
*/
public void sql5() {

Session session = HibernateUtil.currentSession();
String sql = "select t.*,h.hpc_id from hwua_product_category t,hwua_product h where t.hpc_id = h.hpc_id ";
Query query = session.createNativeQuery(sql);
List<Object[]> list = query.list();
for (Object[] objects : list) {
System.out.println(objects[1]);
}
HibernateUtil.closeSession();
}
public static void main(String[] args) {
ProjectDao pro = new ProjectDaoImpl();
ProjectDaoImpl p = new ProjectDaoImpl();
p.sql5();
}

错误如下:
Hibernate: select t.*,h.hpc_id from hwua_product_category t,hwua_product h where t.hpc_id = h.hpc_id 
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [HPC_ID] during auto-discovery of a native-sql query
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1419)
at com.xl.dao.impl.ProjectDaoImpl.sql5(ProjectDaoImpl.java:282)
at com.xl.dao.impl.ProjectDaoImpl.main(ProjectDaoImpl.java:291)
Caused by: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [HPC_ID] during auto-discovery of a native-sql query
at org.hibernate.loader.custom.CustomLoader.validateAliases(CustomLoader.java:512)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:489)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2131)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1911)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)
at org.hibernate.loader.Loader.doQuery(Loader.java:932)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
at org.hibernate.loader.Loader.doList(Loader.java:2615)
at org.hibernate.loader.Loader.doList(Loader.java:2598)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430)
at org.hibernate.loader.Loader.list(Loader.java:2425)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2153)
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:987)
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:148)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1410)
... 2 more

错误原因:
String sql = "select t.*,h.hpc_id from hwua_product_category t,hwua_product h where t.hpc_id = h.hpc_id ";
应改为:
String sql = "select t.*,h.hpc_id hi from hwua_product_category t,hwua_product h where t.hpc_id = h.hpc_id ";
没有给h.hpc_id弄别名导致出错.

原创粉丝点击