Archive for December, 2006
December 21, 2006
key words: sql server,单用户,master恢复
作者:jankie
日期:2006-12-1
一、MASTER数据库备份与恢复
1、数据库master正常情况下对master进行(完全)备份;
若要恢复:
2、停止SQL Server服务;
3、以单用户实例的方式启动SQL Server,启动时不要把窗口关闭;(具体看二)
4、然后再对MASTE数据库进行恢复即可;
二、MSSQL单用户实例的启动方法:
如何在单用户模式下启动SQL Server的命名实例(命令提示符)
在单用户模式下从命令提示符启动SQL Server 的命名实例
从命令提示符输入:
sqlservr.exe -c -m -s {instancename}
例1:
1、sqlserver.exe -c -m 回车(默认实例)
2、sqlserver.exe -c -m -s benet (实例名为benet)
说明在启动 sqlservr.exe之前,必须在命令窗口中切换到适当的目录
如:c:\program files\microsoft sql server\mssql\bin目录下
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 20, 2006
workshop studio震撼你的开发效率 [bea world2006 归来,谈感受,发牢骚]
key words: bea world2006 ,workshop studio, 快速开发
bea world 2006 北京已经过去好几天了,早就想记录一下一直忙
这次参会之行还是有所收获,主题虽然是SOA,但是我对这个现在倒是没什么特别的关注,毕竟这个和公司选用的产品有关系,我们这边公司现在用的Oracle的比较多。
最吸引我的是它的一款开发工具 : workshop studio 3.2
这个工具基于Eclipse,同时集成了Spring,hibernate,Struts,JSF,Ajax,关键的是开发界面效果很棒,支持自动生成,运用Xray扫描技术,在页面可以做到自动提醒。
我个人一直使用IntelliJ IDEA 5,感觉IDEA的编写java代码确实很强,包括xp能力,但是看他的jsp编写能力实在是太弱了,虽然也支持语法级别的提示,但是这个是远远不够的,我们需要的是WYSIWYG,这方面workshop studio给了我一个满意的效果.
可以通过ResourceBundle切换界面语言

也可以在界面切换到中文:

可以在DbXplorer界面里根据schema自动生成hibernate的O/R mapping文件,包括Domain,也包括dao接口以及实现哦:

看一下向导生成的dao实现 :

public class UserRoleSpringService implements IUserRoleService
{

/** *//**
* The dao instance injected by Spring.
*/
private IUserRoleDao dao;

/** *//**
* The service Spring bean id, used in the applicationContext.xml file.
*/
private static final String SERVICE_BEAN_ID = “UserRoleService“;


public UserRoleSpringService()
{
super();
}

/** *//**
* Returns the singleton <code>IUserRoleService</code> instance.
*/

public static IUserRoleService getInstance(ApplicationContext context)
{
return (IUserRoleService)context.getBean(SERVICE_BEAN_ID);
}


/** *//**
* Find an entity by its id (primary key).
* @return The found entity instance or null if the entity does not exist.
*/

public UserRole findUserRoleById(Integer id) throws MyException
{

try
{
return getDao().findUserRoleById(id);

} catch (RuntimeException e)
{
throw new MyException(“findUserRoleById failed with the id “ + id + “: “ + e.getMessage());
}
}

/** *//**
* Return all persistent instances of the <code>UserRole</code> entity.
*/

public List findAllUserRoles() throws MyException
{

try
{
return getDao().findAllUserRoles();

} catch (RuntimeException e)
{
throw new MyException(“findAllUserRoles failed: “ + e.getMessage());
}
}

/** *//**
* Return the persistent entities matching the given example entity.
*/

public List findUserRolesByExample(UserRole userRole) throws MyException
{

try
{
return getDao().findByExample(userRole);

} catch (RuntimeException e)
{
throw new MyException(“findUserRolesByExample failed: “ + e.getMessage());
}
}

/** *//**
* Make the given instance managed and persistent.
*/

public void persistUserRole(UserRole userRole) throws MyException
{

try
{
getDao().persistUserRole(userRole);

} catch (RuntimeException e)
{
throw new MyException(“persistUserRole failed: “ + e.getMessage());
}
}

/** *//**
* Remove the given persistent instance.
*/

public void removeUserRole(UserRole userRole) throws MyException
{

try
{
getDao().removeUserRole(userRole);

} catch (RuntimeException e)
{
throw new MyException(“removeUserRole failed: “ + e.getMessage());
}
}


/** *//**
* Find an entity by its id (primary key).
* @return The found entity instance or null if the entity does not exist.
*/

public ZionRole findZionRoleById(Integer id) throws MyException
{

try
{
return getDao().findZionRoleById(id);

} catch (RuntimeException e)
{
throw new MyException(“findZionRoleById failed with the id “ + id + “: “ + e.getMessage());
}
}

/** *//**
* Return all persistent instances of the <code>ZionRole</code> entity.
*/

public List findAllZionRoles() throws MyException
{

try
{
return getDao().findAllZionRoles();

} catch (RuntimeException e)
{
throw new MyException(“findAllZionRoles failed: “ + e.getMessage());
}
}

/** *//**
* Return the persistent entities matching the given example entity.
*/

public List findZionRolesByExample(ZionRole zionRole) throws MyException
{

try
{
return getDao().findByExample(zionRole);

} catch (RuntimeException e)
{
throw new MyException(“findZionRolesByExample failed: “ + e.getMessage());
}
}

/** *//**
* Make the given instance managed and persistent.
*/

public void persistZionRole(ZionRole zionRole) throws MyException
{

try
{
getDao().persistZionRole(zionRole);

} catch (RuntimeException e)
{
throw new MyException(“persistZionRole failed: “ + e.getMessage());
}
}

/** *//**
* Remove the given persistent instance.
*/

public void removeZionRole(ZionRole zionRole) throws MyException
{

try
{
getDao().removeZionRole(zionRole);

} catch (RuntimeException e)
{
throw new MyException(“removeZionRole failed: “ + e.getMessage());
}
}


/** *//**
* Find an entity by its id (primary key).
* @return The found entity instance or null if the entity does not exist.
*/

public ZionUser findZionUserById(Integer id) throws MyException
{

try
{
return getDao().findZionUserById(id);

} catch (RuntimeException e)
{
throw new MyException(“findZionUserById failed with the id “ + id + “: “ + e.getMessage());
}
}

/** *//**
* Return all persistent instances of the <code>ZionUser</code> entity.
*/

public List findAllZionUsers() throws MyException
{

try
{
return getDao().findAllZionUsers();

} catch (RuntimeException e)
{
throw new MyException(“findAllZionUsers failed: “ + e.getMessage());
}
}

/** *//**
* Return the persistent entities matching the given example entity.
*/

public List findZionUsersByExample(ZionUser zionUser) throws MyException
{

try
{
return getDao().findByExample(zionUser);

} catch (RuntimeException e)
{
throw new MyException(“findZionUsersByExample failed: “ + e.getMessage());
}
}

/** *//**
* Make the given instance managed and persistent.
*/

public void persistZionUser(ZionUser zionUser) throws MyException
{

try
{
getDao().persistZionUser(zionUser);

} catch (RuntimeException e)
{
throw new MyException(“persistZionUser failed: “ + e.getMessage());
}
}

/** *//**
* Remove the given persistent instance.
*/

public void removeZionUser(ZionUser zionUser) throws MyException
{

try
{
getDao().removeZionUser(zionUser);

} catch (RuntimeException e)
{
throw new MyException(“removeZionUser failed: “ + e.getMessage());
}
}


/** *//**
* Called by Spring using the injection rules specified in
* the Spring beans file ”applicationContext.xml”.
*/

public void setDao(IUserRoleDao dao)
{
this.dao = dao;
}

public IUserRoleDao getDao()
{
return this.dao;
}
}
我现在的策略基本上就是用IDEA开发后台代码,而pojo,dao,daoImpl的自动生成,以及web/jsp/ajax都是通过workshop studio来做,感觉不错哦,你还不心动?
详情请参看官方网站 : http://workshopstudio.bea.com/index.html
再来一张在现场Bea的工程师Peter的一张照片,左边的是me

Peter给我留下了深刻的印象,技术娴熟,为人热情,互动也棒,说到兴奋处来一个响指,让你感觉到作开发也是如此的享受,和国内的开发氛围截然不可同日而语。说到这就多罗嗦几句,国内的开发氛围我感觉就是半死不活,用如下几个关键字做一个概括就是 : 不敬业,冷漠,不专业,不规范,无体系,不受重视,自己看不起自己,别人也看不起自己,自己认为自己像个傻B,结果自己真的变为一个傻B.
记住,主动权永远在你自己手里,你想成为什么你就会变成什么.
当寒流到来时你是什么心态? 强者的逻辑是:来得更猛烈些,来年春天地上会堆满尸体.
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 20, 2006
key words: bea world2006 ,workshop studio, 快速开发 bea world 2006 北京已经过去好几天了,早就想记录一下一直忙
这次参会之行还是有所收获,主题虽然是SOA,但是我对这个现在倒是没什么特别的关注,毕竟这个和公司选用的产品有关系,我们这边公司现在用的Oracle的比较多。 最吸引我的是它的一款开发工具 : workshop studio 3.2 这个工具基于Eclipse,同时集成了Spring,hibernate,Struts,JSF,Ajax,关键的是开发界面效果很棒,支持自动生成,运用Xray扫描技术,在页面可以做到自动提醒。 我个人一直使用IntelliJ IDEA 5,感觉IDEA的编写java代码确实很强,包括xp能力,但是看他的jsp编写能力实在是太弱了,虽然也支持语法级别的提示,但是这个是远远不够的,我们需要的是WYSIWYG,这方面workshop studio给了我一个满意的效果. 可以通过ResourceBundle切换界面语言 也可以在界面切换到中文: 可以在DbXplorer界面里根据schema自动生成hibernate的O/R mapping文件,包括Domain,也包括dao接口以及实现哦: 看一下向导生成的dao实现 : public class UserRoleSpringService implements IUserRoleService { /** *//** * The dao instance injected by Spring. */ private IUserRoleDao dao; /** *//** * The service Spring bean id, used in the applicationContext.xml file. */ private static final String SERVICE_BEAN_ID = “UserRoleService”; public UserRoleSpringService() { super(); } /** *//** * Returns the singleton IUserRoleService instance. */ public static IUserRoleService getInstance(ApplicationContext context) { return (IUserRoleService)context.getBean(SERVICE_BEAN_ID); } /** *//** * Find an entity by its id (primary key). * @return The found entity instance or null if the entity does not exist. */ public UserRole findUserRoleById(Integer id) throws MyException { try { return getDao().findUserRoleById(id); } catch (RuntimeException e) { throw new MyException(“findUserRoleById failed with the id ” + id + “: ” + e.getMessage()); } } /** *//** * Return all persistent instances of the UserRole entity. */ public List findAllUserRoles() throws MyException { try { return getDao().findAllUserRoles(); } catch (RuntimeException e) { throw new MyException(“findAllUserRoles failed: ” + e.getMessage()); } } /** *//** * Return the persistent entities matching the given example entity. */ public List findUserRolesByExample(UserRole userRole) throws MyException { try { return getDao().findByExample(userRole); } catch (RuntimeException e) { throw new MyException(“findUserRolesByExample failed: ” + e.getMessage()); } } /** *//** * Make the given instance managed and persistent. */ public void persistUserRole(UserRole userRole) throws MyException { try { getDao().persistUserRole(userRole); } catch (RuntimeException e) { throw new MyException(“persistUserRole failed: ” + e.getMessage()); } } /** *//** * Remove the given persistent instance. */ public void removeUserRole(UserRole userRole) throws MyException { try { getDao().removeUserRole(userRole); } catch (RuntimeException e) { throw new MyException(“removeUserRole failed: ” + e.getMessage()); } } /** *//** * Find an entity by its id (primary key). * @return The found entity instance or null if the entity does not exist. */ public ZionRole findZionRoleById(Integer id) throws MyException { try { return getDao().findZionRoleById(id); } catch (RuntimeException e) { throw new MyException(“findZionRoleById failed with the id ” + id + “: ” + e.getMessage()); } } /** *//** * Return all persistent instances of the ZionRole entity. */ public List findAllZionRoles() throws MyException { try { return getDao().findAllZionRoles(); } catch (RuntimeException e) { throw new MyException(“findAllZionRoles failed: ” + e.getMessage()); } } /** *//** * Return the persistent entities matching the given example entity. */ public List findZionRolesByExample(ZionRole zionRole) throws MyException { try { return getDao().findByExample(zionRole); } catch (RuntimeException e) { throw new MyException(“findZionRolesByExample failed: ” + e.getMessage()); } } /** *//** * Make the given instance managed and persistent. */ public void persistZionRole(ZionRole zionRole) throws MyException { try { getDao().persistZionRole(zionRole); } catch (RuntimeException e) { throw new MyException(“persistZionRole failed: ” + e.getMessage()); } } /** *//** * Remove the given persistent instance. */ public void removeZionRole(ZionRole zionRole) throws MyException { try { getDao().removeZionRole(zionRole); } catch (RuntimeException e) { throw new MyException(“removeZionRole failed: ” + e.getMessage()); } } /** *//** * Find an entity by its id (primary key). * @return The found entity instance or null if the entity does not exist. */ public ZionUser findZionUserById(Integer id) throws MyException { try { return getDao().findZionUserById(id); } catch (RuntimeException e) { throw new MyException(“findZionUserById failed with the id ” + id + “: ” + e.getMessage()); } } /** *//** * Return all persistent instances of the ZionUser entity. */ public List findAllZionUsers() throws MyException { try { return getDao().findAllZionUsers(); } catch (RuntimeException e) { throw new MyException(“findAllZionUsers failed: ” + e.getMessage()); } } /** *//** * Return the persistent entities matching the given example entity. */ public List findZionUsersByExample(ZionUser zionUser) throws MyException { try { return getDao().findByExample(zionUser); } catch (RuntimeException e) { throw new MyException(“findZionUsersByExample failed: ” + e.getMessage()); } } /** *//** * Make the given instance managed and persistent. */ public void persistZionUser(ZionUser zionUser) throws MyException { try { getDao().persistZionUser(zionUser); } catch (RuntimeException e) { throw new MyException(“persistZionUser failed: ” + e.getMessage()); } } /** *//** * Remove the given persistent instance. */ public void removeZionUser(ZionUser zionUser) throws MyException { try { getDao().removeZionUser(zionUser); } catch (RuntimeException e) { throw new MyException(“removeZionUser failed: ” + e.getMessage()); } } /** *//** * Called by Spring using the injection rules specified in * the Spring beans file “applicationContext.xml”. */ public void setDao(IUserRoleDao dao) { this.dao = dao; } public IUserRoleDao getDao() { return this.dao; } } 我现在的策略基本上就是用IDEA开发后台代码,而pojo,dao,daoImpl的自动生成,以及web/jsp/ajax都是通过workshop studio来做,感觉不错哦,你还不心动?
详情请参看官方网站 : http://workshopstudio.bea.com/index.html 再来一张在现场Bea的工程师Peter的一张照片,左边的是me
Peter给我留下了深刻的印象,技术娴熟,为人热情,互动也棒,说到兴奋处来一个响指,让你感觉到作开发也是如此的享受,和国内的开发氛围截然不可同日而语。说到这就多罗嗦几句,国内的开发氛围我感觉就是半死不活,用如下几个关键字做一个概括就是 : 不敬业,冷漠,不专业,不规范,无体系,不受重视,自己看不起自己,别人也看不起自己,自己认为自己像个傻B,结果自己真的变为一个傻B. 记住,主动权永远在你自己手里,你想成为什么你就会变成什么. 当寒流到来时你是什么心态? 强者的逻辑是:来得更猛烈些,来年春天地上会堆满尸体.
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 20, 2006
key words: dll,ocx,注册控件
最近项目中用到了收银,需要在顾显上显示金额.
1.将.dll、.ocx等考到system32下。
2.用命令注册regsvr32.exe .dll。(eg:regsvr32.exe OWC11.dll)
3.在注册表中查看:利用名称或与其唯一对应的clsid(“clsid:0002E55D-0000-0000-C000- 000000000046″)可查找到相关信息
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 18, 2006
key words: commons log,log4j,apache log
前言: 对于log4j虽然在用,但是也存在一个疑问,怎么有的用apache的commons logging有的直接用log4j,下面的这篇文章解释了我的疑问.
转自 here
Apache组织开发了一套用于支持Logging的Log4J,Java 1.4版本也引入了一套内置的 Logging框架,如果开发者想在这两套Logging系统之间自由的切换,该怎么办呢?答案就是,使用Commons Logging。 Commons Logging定义了一套抽象的Logging接口,用户可以通过配置,使这些接口指向任何一个已存在的Logging系统。
•使用抽象Logging接口
问题:
你在编写一个可以重复使用的库,需要写入Log信息,但你不想使你的Logging功能绑定在Apache Log4J或者JDK 1.4 Logging框架上。
解决方案:
| public static void main(String[] args) {//自己替换[]
System.setProperty(“org.apache.commons.logging.Log”,
“org.apache.commons.logging.impl.Jdk14Logger”);
Log log = LogFactory.getLog(“com.discursive.jccook.SomeApp”);
if (log.isTraceEnabled()) {
log.trace(“This is a trace message”);
}
if (log.isDebugEnabled()) {
log.debug(“This is a debug message”);
}
log.info(“This is an informational message”);
log.warn(“This is a warning”);
log.error(“This is an error”);
log.fatal(“This is fatal”);
} |
LogFactory.getLog方法会根据底层环境返回一个适当的Log实现。如果用户想指定一个具体的Logging系统实现,可以设置org.apache.commons.logging.Log系统属性。例如:
System.setProperty(“org.apache.commons.logging.Log”,
“org.apache.commons.logging.impl.Log4JLogger”);
这样就会使用Log4J作为Logging系统。
org.apache.commons.logging.Log可以设定为:
•org.apache.commons.logging.impl.Log4JLogger 使用Log4J
•org.apache.commons.logging.impl.Jdk14Logger 使用JDK 1.4 Logging框架
•org.apache.commons.logging.impl.SimpleLog 使用Commons Logging内置的简单Log实现
其他:
总结一下,Commons Logging会按照下列顺序来指定具体的Log实现。
•如果定义了org.apache.commons.logging.Log系统参数,实用指定的Logging实现。
•如果在CLASSPATH里发现了Log4J,使用Log4J。
•如果使用的是JDK1.4,使用JDK1.4内置的Logging框架。
•如果都没有找到,则使用Commons Logging内置的简单Log实现。
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 17, 2006
key words : 职业规划 高薪
原文见robbin的pdf,我整理了一下脑图

VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 11, 2006
key words: jsp,jstl,1.0,1.1,fn函数
原来一直用struts,最近项目里有人用jstl,我也就顺便拿来用,感觉还是不错。
过程中碰到一些小的问题总结如下:
一。版本问题
jstl存在1.0和1.1的差异问题,用EL建议需要在1.1的版本下,1.1的URI的标志为:
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core“ prefix=“c“%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/functions“ prefix=“fn“%>
注意,1.0版本没有/jsp/.
如果用的1.0会出现如下异常
org.apache.jasper.JasperException: /public/left_tree.jsp(100,24) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
com.wellsoon.zfzw.webapp.common.VabAccessFilter.doFilter(VabAccessFilter.java:43)
root cause
二。打开EL
<%@ page contentType=“text/html;charset=gb2312“ language=“java“ isELIgnored=“false“ %>
前提是容器支持2.0,即使支持默认也未必打开,最安全的方式就是显示打开 isELIgnored=“false“
三.fn的用法
对于Vo里的一个List作length的计算或判断很方便
<table width=”160″ border=”0″ cellspacing=”0″ cellpadding=”4″>
<c:forEach items=”${menuInfos}” var=”m”>
<c:if test=”${fn:length(m.subMenus)>0}”>
<tr>
<td>
<script language=”JavaScript”>
var tObj = new treeClass(“A1“)
tObj.start();
tObj.m_start(“<c:out value=“${m.menuModule}“/>“,0);
<c:forEach items=“${m.subMenus}“ var=“sub“>
tObj.add_Sub(“<c:out value=“${sub.menuName}“/>“,“<%=path%>
<c:out value=“${sub.url}“/>“,“mymain“);
</c:forEach>
tObj.m_end();
tObj.end();
tObj.print();
</script>
</td>
</tr>
</c:if>
</c:forEach>
</table>
四.tld的声明可以在web.xml作显式声明也可以用http作直接URL声明
推荐用本地的方式.
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 7, 2006
key words: MiddleGen,hibernate,many to many,多对多 如果你有如下表结构 user(user_id,user_name) role(role_id,role_name) user_role(id,user_id,role_id) 那么默认MiddleGen生成的是两个一对多,但我们更多的情况是用many to many 需要修改middlegen的build.xml文件
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]
December 1, 2006
越来越发现其实掌握 hibernate并不容易,Spring用起来其实简单多了,但是在用hibernate的时候真的是需要一定的时间积累,对一个项目组来说如果采用hibernate最好有一个对hibernate比较清楚的人否则碰到问题就会成为项目的风险。
我想告诉各位的是,掌握hibernate可能比你预期的难多了,当你轻松的告诉我,hibernate很简单的时候该是你自己多反省了. (只有一种情况例外,你是一个牛人)
好了,一个引子废话那么多,其实今天只是想先说一说hibernate里的Fetch的作用.
大家都知道,在hibernate里为了性能考虑,引进了lazy的概念,这里我们以Parent和Child为模型来说明,
public class Parent implements Serializable {
/** identifier field */
private Long id;
/** persistent field */
private List childs;
//skip all getter/setter method
}
public class Child implements Serializable {
/** identifier field */
private Long id;
/** persistent field */
private net.foxlog.model.Parent parent;
//skip all getter/setter method
}
在我们查询Parent对象的时候,默认只有Parent的内容,并不包含childs的信息,如果在Parent.hbm.xml里设置lazy=”false”的话才同时取出关联的所有childs内容.
问题是我既想要hibernate默认的性能又想要临时的灵活性该怎么办? 这就是fetch的功能。我们可以把fetch与lazy=”true”的关系类比为事务当中的编程式事务与声明式事务,不太准确,但是大概是这个意思。
总值,fetch就是在代码这一层给你一个主动抓取得机会.
Parent parent = (Parent)hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(
“from Parent as parent “+
“ left outer join fetch parent.childs “ +
“ where parent.id = :id“
);
q.setParameter(“id“,new Long(15));
return (Parent)q.uniqueResult();
}
});
Assert.assertTrue(parent.getChilds().size() > 0);
你可以在lazy=”true”的情况下把fetch去掉,就会报异常. 当然,如果lazy=”false”就不需要fetch了
有一个问题,使用Fetch会有重复记录的现象发生,我们可以理解为Fetch实际上不是为Parent服务的,而是为Child服务的.所以直接取Parent会有不匹配的问题.
参考一下下面的这篇文章
Hibernate集合初始化
======================================================================
update:以上有些结论错误,实际上在hibernate3.2.1版本下测试,可以不出现重复记录,
public void testNPlusOne() throws Exception{
List list = (List)hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(
“select distinct p from net.foxlog.model.Parent p inner join fetch p.childs“
);
return q.list();
}
});
//((Parent)(list.get(0))).getChilds();
System.out.println(“list size = “ + list.size());
for(int i=0;i<list.size();i++){
Parent p = (Parent)list.get(i);
System.out.println(“===parent = “ + p);
System.out.println(“===parent’s child’s length = “ + p.getChilds().size());
}
}
打印结果如下:
Hibernate: select distinct parent0_.id as id2_0_, childs1_.id as id0_1_, childs1_.parent_id as parent2_0_1_, childs1_.parent_id as parent2_0__, childs1_.id as id0__ from parent parent0_ inner join child childs1_ on parent0_.id=childs1_.parent_id
list size = 3
===parent = net.foxlog.model.Parent@1401d28[id=14]
===parent‘s child‘s length = 1
===parent = net.foxlog.model.Parent@14e0e90[id=15]
===parent‘s child‘s length = 2
===parent = net.foxlog.model.Parent@62610b[id=17]
===parent‘s child‘s length = 3
另外,如果用open session in view模式的话一般不用fetch,但首先推荐fetch,如果非用的话因为有N+1的现象,所以可以结合batch模式来改善下性能.
VN:F [1.6.3_896]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.3_896]