Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Spring with Hibernate Integrating Hibernate with Spring Configure Spring for Hibernate

Stuck on Task 3/3 on Configure Spring with Hibernate Challenge...

I can't figure out what code is required in Task 3 of this exercise. When I run it, it tells me: "Oops! It looks like Task 2 is no longer passing." Except when I go back to Task 2 and re-run that code without changing anything, it still passes.

My code should be attached to this post. Any help or guidance would be appreciated.

com/teamtreehouse/contactmgr/config/DataConfig.java
package com.teamtreehouse.contactmgr.config;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;

@Configuration
@PropertySource("app.properties")
public class DataConfig {
  @Autowired
  private Environment env;

  @Bean
    public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
      sessionFactory.setPackagesToScan(env.getProperty("com.teamtreehouse.contactmgr.model"));
      sessionFactory.setDataSource(dataSource());
      return sessionFactory;
    }

  @Bean
    public DataSource dataSource() {
      BasicDataSource ds = new BasicDataSource();

      // Driver class name
      ds.setDriverClassName(env.getProperty("giflib.db.driver"));

      // Set URL
      ds.setUrl(env.getProperty("giflib.db.url"));

      return ds;
    }
}
app.properties
giflib.db.driver = org.h2.Driver
giflib.db.url = jdbc:h2:mem:contactmgr

2 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,161 Points

Remove "env.getProperty" from

sessionFactory.setPackagesToScan(env.getProperty("com.teamtreehouse.contactmgr.model"));

There is no such property. It is simple string that you should set:

sessionFactory.setPackagesToScan("com.teamtreehouse.contactmgr.model");

Should work...

Yes that did work! Thank you for your help Alexander!

package com.teamtreehouse.contactmgr.config;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;

@Configuration @PropertySource("app.properties") public class DataConfig { @Autowired private Environment env;

@Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setPackagesToScan("com.teamtreehouse.contactmgr.model"); sessionFactory.setDataSource(dataSource()); return sessionFactory; }

@Bean public DataSource dataSource() { BasicDataSource ds = new BasicDataSource();

  // Driver class name
  ds.setDriverClassName(env.getProperty("giflib.db.driver"));

  // Set URL
  ds.setUrl(env.getProperty("giflib.db.url"));

  return ds;
}

}