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 Add JPA Annotations to Entities

Joe Rowe
Joe Rowe
2,225 Points

BeanCreationException (Spring with Hibernate)

I have followed this course so far and have double checked my work to that hosted in the github repo for this course. However, I encounter an exception when using the gradle bootRun task at the end of this video.

The specific exception is: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/teamtreehouse/giflib/config/DataConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Class name must not be null

The only suggestions I have found is that this can come from version mismatching, although all versions I am using are from the build.gradle included (and checked against!) the githut repo

Any ideas or suggestions for how to troubleshoot this exception would be gratefully received!

4 Answers

Joe Rowe
Joe Rowe
2,225 Points

SOLVED!

I had not used the correct property name, which in turn meant the bean did not find the property referencing the packages to scan, and as a result could not be created!

So there you have it - I hope this helps anyone else who has this problem in the future

So, I had the exact same problem. And like the OP said compare the entity names between the app.properties and DataConfig.java:

giflib.entity.package = com.teamtreehouse.giflib.model
giflib.db.driver = org.h2.Driver
giflib.db.url = jdbc:h2:tcp://localhost/~/Desktop/giflib-hibernate/data/giflib
giflib.db.username = sa
giflib.db.password =
J llama
J llama
12,631 Points

thanks for absolutely nothing

Einar Brandsson
Einar Brandsson
354 Points

package com.teamtreehouse.giflib.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() {
    Resource config = new ClassPathResource("hibernate.cfg.xml");
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setConfigLocation(config);
    sessionFactory.setPackagesToScan(env.getProperty("giflib.entity.package"));
    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"));

    // Set username & password
    ds.setUsername(env.getProperty("giflib.db.username"));
    ds.setPassword(env.getProperty("giflib.db.password"));

    return ds;
}

}

Thanks in advance :)

Ivailo Hristov
Ivailo Hristov
6,297 Points

I have the same problem

shekhar bhardwaj
seal-mask
.a{fill-rule:evenodd;}techdegree
shekhar bhardwaj
Full Stack JavaScript Techdegree Student 12,373 Points

I am not sure if it's too late but check all your properties and make sure the keys are correct and nothing is returning null when you are doing env.getProperty, especially the model pkg key is correct for pkg scan.

and you do not have to specifically configure @PropertySource("app.properties"), just @Autowired Environment env that should suffice and make sure you properties files are under resource folder and naming convention should be application.properties.

let me know if it helps.

Einar Brandsson
Einar Brandsson
354 Points

What do you mean the correct property name? I have the same error and I can't figure it out.

Joe Rowe
Joe Rowe
2,225 Points

Hey, i cant remember specifically actually, but if you post your DataConfig class id be happy to take a look.

(Difficult without the project in front of me!)