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

error help me please Error creating bean with name 'sessionFactory'

@Configuration @PropertySource("app.properties") public class DataConfig {

@Autowired
Environment env;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    Resource resource = new ClassPathResource("hibernate.cfg.xml");
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setConfigLocation(resource);
    sessionFactory.setPackagesToScan(env.getProperty("giflib.entity.package"));
    return sessionFactory;
}

// public DataConfig(){}

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

    // SetDriver
    ds.setDriverClassName("diflib.db.driver");

    // Url database
    ds.setUrl(env.getProperty("giflib.db.url"));

    // Login database
    ds.setUsername(env.getProperty("giflib.db.username"));

    // Password database
    ds.setUsername(env.getProperty("giflib.db.password"));

    return ds;
}

}

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 org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in URL file:/home/alex/JavaProjects/giflib-hibernate/build/resources/main/hibernate.cfg.xml. Message: cvc-complex-type.3.2.2: Attribute 'value' is not allowed to appear in element 'property'.

2 Answers

Judging by the error

Unable to perform unmarshalling at line number -1 and column -1 in URL file:/home/alex/JavaProjects/giflib-hibernate/build/resources/main/hibernate.cfg.xml. Message: cvc-complex-type.3.2.2: Attribute 'value' is not allowed to appear in element 'property'

You have error in hibernate.cfg.xml, but looking at the code you posted it looks to me that error is the result of many factors...

In order to find the error faster, could you push your project on GitHub?

Can be done in couple of clicks using intellijidea:

https://www.jetbrains.com/help/idea/2016.2/share-project-on-github-dialog.html

Posting just hibernate.cfg.xml will not help, because I also need app.properties as well as other files, because when I look at Chris's 'giflib-hibernate':

https://github.com/treehouse/giflib-hibernate/blob/master/src/main/java/com/teamtreehouse/giflib/config/DataConfig.java

His DataConfig is totally different:

First of all you forgot to set data source in sessionFactory() method

        sessionFactory.setDataSource(dataSource());

Second, you have a typo in dataSource()

    // SetDriver 
   // ds.setDriverClassName("diflib.db.driver"); // wrong
  // should be
  ds.setDriverClassName(env.getProperty("giflib.db.driver"));

Make sure you always compare your code with Chris's code : that helps a lot.

thank you :)