Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Data-Driven Application Design Add a DAO

vincent batteast
vincent batteast
51,094 Points

In the ContactDao interface, add a findAll method that returns a List of Contact objects. Then, in ContactDaoImpl.java i

Bummer! 2 out of 2 tests failed. See preview for more details.

com/teamtreehouse/contactmgr/dao/ContactDao.java
package com.teamtreehouse.contactmgr.dao;



import com.teamtreehouse.contactmgr.model.Contact;



import java.util.List;



public interface ContactDao {


  List<Contact> findAll();



}
com/teamtreehouse/contactmgr/dao/ContactDaoImpl.java
package com.teamtreehouse.contactmgr.dao;



import com.teamtreehouse.contactmgr.model.Contact;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;



import java.util.List;

@Repository
public class ContactDaoImpl implements ContactDao{

  @Autowired
  private SessionFactory sessionFactory;


  @Override
  @SuppressWarnings("unchecked")

    public List<Contact> findAll(){

      Session session = sessionFactory.openSession();

     List<Contact> Contact = session.createCriteria(Contact.class).list();

      session.close();

      return null;

    }

}

4 Answers

The following code passed the challenge for me.

Interface

package com.teamtreehouse.contactmgr.dao;

import com.teamtreehouse.contactmgr.model.Contact;

import java.util.List;

public interface ContactDao {
  List<Contact> findAll();
}

Impl class

package com.teamtreehouse.contactmgr.dao;

import com.teamtreehouse.contactmgr.model.Contact;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ContactDaoImpl implements ContactDao {
    @Override
    @SuppressWarnings("unchecked")
    public List<Contact> findAll() {
        // Open a session
/*        Session session = sessionFactory.openSession();

        // DEPRECATED as of Hibernate 5.2.0
        // List<Category> categories = session.createCriteria(Category.class).list();

        // Create CriteriaBuilder
        CriteriaBuilder builder = session.getCriteriaBuilder();

        // Create CriteriaQuery
        CriteriaQuery<Category> criteria = builder.createQuery(Category.class);

        // Specify criteria root
        criteria.from(Category.class);

        // Execute query
        List<Category> categories = session.createQuery(criteria).getResultList();

        // Close session
        session.close();*/

        return null;
    }
}
vincent batteast
vincent batteast
51,094 Points

I don't see a different but it work form me too. ??

Without seeing your error output I could only make a guess. It could be the @Autowired annotation that I omitted.

When I added the two lines of code for autowired I get the following compiler error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.teamtreehouse.contactmgr.dao.ContactDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)

It's also explained here: https://teamtreehouse.com/community/spring-hibernate-quiz-problem

Seth Kroger
Seth Kroger
56,413 Points

The main thing you still need to do is return the list of contacts you retrieve. You are currently just returning null. It's probably a good idea to rename the variable for that so it's not the same name as the class.

vincent batteast
vincent batteast
51,094 Points

it didn't work

package com.teamtreehouse.contactmgr.dao;

import com.teamtreehouse.contactmgr.model.Contact;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class ContactDaoImpl implements ContactDao{

  @Autowired
  private SessionFactory sessionFactory;


  @Override
  @SuppressWarnings("unchecked")
  public List<Contact> findAll(){

      Session session = sessionFactory.openSession();

     List<Contact> Contact = session.createCriteria(Contact.class).list();

      session.close();

      return Contact;
  }

}

The instruction states for now, it can return "null". So it should work.

vincent batteast
vincent batteast
51,094 Points

I have been trying to work through this but it not working

I think how I solved it was comment out everything in the method except for the return statement. I realized they were asking to create the method structure, not the individual statements in the method.

vincent batteast
vincent batteast
51,094 Points

still now working

package com.teamtreehouse.contactmgr.dao;

import com.teamtreehouse.contactmgr.model.Contact;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class ContactDaoImpl implements ContactDao{

  @Autowired
  private SessionFactory sessionFactory;


  @Override
  @SuppressWarnings("unchecked")
  public List<Contact> findAll(){

     // Session session = sessionFactory.openSession();

    // List<Contact> Contact = session.createCriteria(Contact.class).list();

  //    session.close();

      return null;
  }

}