At line 5 removed 1 line. |
|
At line 8 changed 8 lines. |
SqlRowSet rset = getJdbcTemplate().queryForRowSet( |
"select id, name from mytest where id > ? and name < ?", |
new Object[] {new Long(1), "Z"}); |
int i = 0; |
while (rset.next()) { |
i++; |
System.out.println("Row" + i + " " + rset.getString(2)); |
} |
import javax.naming.Context; |
import javax.naming.InitialContext; |
At line 17 changed 1 line. |
}] |
import junit.framework.TestCase; |
At line 12 added 5 lines. |
import org.mockejb.MockContainer; |
import org.mockejb.SessionBeanDescriptor; |
import org.mockejb.jndi.MockContextFactory; |
import org.springframework.context.ApplicationContext; |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
At line 20 changed 1 line. |
;:''The DAO layer has the Hibernate sessionFactory injected (made available) to it through Spring and this entry in applicationContext-hibernate.xml: |
/** |
* Parent TestCase class for testing EJBs using MockEJB |
* |
* @author mraible |
* |
*/ |
public abstract class MockEJBTestCase extends TestCase { |
At line 22 changed 3 lines. |
{{{ |
<property name="sessionFactory"><ref local="sessionFactory"/></property> |
}}} |
/** |
* This method sets up a MockContainer and allows you to deploy an EJB to |
* it. Override <code>onSetUp()</code> to add custom set-up behavior. |
* |
* @see #onSetUp() |
*/ |
protected final void setUp() throws Exception { |
MockContextFactory.setAsInitial(); |
At line 26 changed 1 line. |
So to access the Hibernate session from within your DAO, place a method like this in your DAO: |
Context ctx = new InitialContext(); |
ApplicationContext appCtx = |
new ClassPathXmlApplicationContext(getConfigLocations()); |
|
ctx.bind("java:comp/env/jdbc/appDS", appCtx.getBean("dataSource")); |
|
MockContainer mc = new MockContainer(ctx); |
SessionBeanDescriptor dd = getDeploymentDescriptor(); |
mc.deploy(dd); |
onSetUp(); |
} |
At line 28 changed 1 line. |
{{{ |
protected String[] getConfigLocations() { |
return new String[] { "classpath:/applicationContext.xml" }; |
} |
At line 30 changed 1 line. |
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); |
protected void onSetUp() throws Exception {} |
At line 32 changed 1 line. |
}}} |
protected abstract SessionBeanDescriptor getDeploymentDescriptor(); |
} |
At line 34 changed 2 lines. |
* How do I get hold of the Hibernate session in my View code? |
;:''To get hold of the Session in the view code, you need to lookup the SessionFactory defined in the applicationContext-hibernate.xml: |
}] |
At line 37 changed 7 lines. |
{{{ |
private void createApplicationContext() { |
if (ctx == null) { |
ctx = |
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext()); |
} |
} |
[CreateDAO_zh] |
[CreateDAO_sp] |
At line 45 changed 14 lines. |
public net.sf.hibernate.Session getCurrentHibernateSession() { |
createApplicationContext(); |
|
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory"); |
SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory); |
net.sf.hibernate.Session currentSession = null; |
Transaction currentTransaction = null; |
|
if (sessionHolder!=null) { |
currentSession = sessionHolder.getSession(); |
currentTransaction = sessionHolder.getTransaction(); |
} |
return currentSession; |
} |
[CreateManager_es] |
At line 60 changed 1 line. |
}}} |
[QuickStart Guide_es] |
At line 65 added 1 line. |
[SpringControllerUnitTest] |
At line 64 changed 2 lines. |
|
|
[δΈζζε|Articles_zh] |