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 Integrating Hibernate with Spring Configure Spring for Hibernate

Kliti Bushamau
Kliti Bushamau
244 Points

Can somebody help me with this task?

I do not know what to do

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
public class DataConfig {
@Autowired
  private Environment env;

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    Resource config = new ClassPathResource("hibernate.cfg.xml");
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setConfigLocation(config);
    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("org.h2.Driver");

        // Set URL
        ds.setUrl("jdbc:h2:mem:contactmgr");

    return ds;
  }

}
app.properties

1 Answer

Daniel Vargas
Daniel Vargas
29,184 Points

Hi, I hope the answer is not too late.

Remember the challenges are like the code in the classes, however they're not exactly the same.

You're calling evironment variables that don't exist (In the challenge) and the hibernate configuration file (which is not present in the challenge neither). You could just simplify your code and it should be fine:

@Configuration
public class DataConfig {

  @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();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:mem:contactmgr");
    return bds;
  }   
}

You had everything fine, you just had too much hahaha

Kliti Bushamau
Kliti Bushamau
244 Points

it is in fact, but thank you so much.