Note that there are some explanatory texts on larger screens.

plurals
  1. POtesting dao with hibernate genericdao pattern with spring.Headache
    primarykey
    data
    text
    <p>in my journey of learning hibernate i came across an article on <a href="https://www.hibernate.org/328.html" rel="nofollow noreferrer">hibernate site</a>. i' learning spring too and wanted to do certain things to discover the flexibility of spring by letting you implement you own session.yes i don't want to use the hibernateTemplate(for experiment). and i'm now having a problem and even the test class.I followed the article on the hibernate site especially the section an "implementation with hibernate" so we have the generic dao interface :</p> <pre><code>public interface GenericDAO&lt;T, ID extends Serializable&gt; { T findById(ID id, boolean lock); List&lt;T&gt; findAll(); List&lt;T&gt; findByExample(T exampleInstance); T makePersistent(T entity); void makeTransient(T entity); </code></pre> <p>}</p> <p>it's implementation in an abstract class that is the same as the one on the web site.Please refer to it from the link i provide.i'll like to save this post to be too long</p> <p>now come my dao's messagedao interface</p> <pre><code>package com.project.core.dao; import com.project.core.model.MessageDetails; import java.util.List; public interface MessageDAO extends GenericDAO&lt;MessageDetails, Long&gt;{ //Message class is on of my pojo public List&lt;Message&gt; GetAllByStatus(String status); } </code></pre> <p>its implementation is messagedaoimpl: </p> <pre><code>public class MessageDAOImpl extends GenericDAOImpl &lt;Message, Long&gt; implements MessageDAO { // mySContainer is an interface which my HibernateUtils implement mySContainer sessionManager; /** * */ public MessageDAOImpl(){} /** * * @param sessionManager */ public MessageDAOImpl(HibernateUtils sessionManager){ this.sessionManager = sessionManager; } //........ plus other methods } </code></pre> <p>here is my HibernatUtils</p> <pre><code>public class HibernateUtils implements SessionContainer { private final SessionFactory sessionFactory; private Session session; public HibernateUtils() { this.sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } public HibernateUtils(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * * this is the function that return a session.So i'm free to implements any type of session in here. */ public Session requestSession() { // if (session != null || session.isOpen()) { // return session; // } else { session = sessionFactory.openSession(); // } return session; } } </code></pre> <blockquote> <p>So in my understanding while using spring(will provide the conf), i'ld wire sessionFactory to my HiberbernateUtils and then wire its method RequestSession to the Session Property of the GenericDAOImpl (the one from the link provided).</p> </blockquote> <p>here is my spring config core.xml</p> <pre><code>&lt;bean id="sessionManager" class="com.project.core.dao.hibernate.HibernateUtils"&gt; &lt;constructor-arg ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;bean id="messageDao" class="com.project.core.dao.hibernate.MessageDAOImpl"&gt; &lt;constructor-arg ref="sessionManager"/&gt; &lt;/bean&gt; &lt;bean id="genericDAOimpl" class="com.project.core.dao.GenericDAO"&gt; &lt;property name="session" ref="mySession"/&gt; &lt;/bean&gt; &lt;bean id="mySession" factory-bean="com.project.core.dao.SessionContainer" factory-method="requestSession"/&gt; </code></pre> <p>now my test is this</p> <pre><code>public class MessageDetailsDAOImplTest extends AbstractDependencyInjectionSpringContextTests{ HibernateUtils sessionManager = (HibernateUtils) applicationContext.getBean("sessionManager"); MessageDAO messagedao =(MessageDAO) applicationContext.getBean("messageDao"); static Message[] message = new Message[] { new Message("text",1,"test for dummies 1","1234567890","Pending",new Date()), new Message("text",2,"test for dummies 2","334455669990","Delivered",new Date()) }; public MessageDAOImplTest() { } @Override protected String[] getConfigLocations(){ return new String[]{"file:src/main/resources/core.xml"}; } @Test public void testMakePersistent() { System.out.println("MakePersistent"); messagedao.makePersistent(message[0]); Session session = sessionManager.RequestSession(); session.beginTransaction(); MessageDetails fromdb = ( Message) session.load(Message.class, message[0].getMessageId()); assertEquals(fromdb.getMessageId(), message[0].getMessageId()); assertEquals(fromdb.getDateSent(),message.getDateSent()); assertEquals(fromdb.getGlobalStatus(),message.getGlobalStatus()); assertEquals(fromdb.getNumberOfPages(),message.getNumberOfPages()); } </code></pre> <p>i'm having this error </p> <blockquote> <p>exception in constructor testMakePersistent(java.lang.NullPointerException at com.project.core.dao.hibernate.MessageDAOImplTest) </p> </blockquote> <p>with this stack : at com.project.core.dao.hibernate.MessageDAOImplTest.(MessageDAOImplTest.java:28) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at junit.framework.TestSuite.createTest(TestSuite.java:61) at junit.framework.TestSuite.addTestMethod(TestSuite.java:283) at junit.framework.TestSuite.(TestSuite.java:146) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:481) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888) ))</p> <p>How to actually make this one work.I know this is a lot to stuffs and i'm thanking you for reading it.Please give me a solution.How would you do this? thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload