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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![dragongugu smith](https://secure.gravatar.com/avatar/1c9421130a233577fccb4ac5535a6e12?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
dragongugu smith
Full Stack JavaScript Techdegree Student 196 Pointswhy not build like this
why not do it like this
Configuration config =new Configuration(); SessionFactory=config.buildSessionFactory
2 Answers
![Alexander Nikiforov](https://uploads.teamtreehouse.com/production/profile-photos/5494222/micro_IMG_0409.jpg)
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsChris is following 'Hibernate Getting Started Guide'. Please take a look here (I took the latest 5.2):
http://docs.jboss.org/hibernate/orm/5.2/quickstart/html_single/#hibernate-gsg-tutorial-basic-test
They are saying:
The setUp method first builds a org.hibernate.boot.registry.StandardServiceRegistry
instance which incorporates configuration information into a working set of Services for use by the SessionFactory
. In this tutorial we defined all configuration information in hibernate.cfg.xml
so there is not much interesting to see here.
Using the StandardServiceRegistry
we create the org.hibernate.boot.MetadataSources
which is the start point for telling Hibernate about your domain model. Again, since we defined that in hibernate.cfg.xml
so there is not much interesting to see here.
org.hibernate.boot.Metadata
represents the complete, partially validated view of the application domain model which the SessionFactory will be based on.
The final step in the bootstrap process is to build the SessionFactory
. The SessionFactory
is a thread-safe object that is instantiated once to serve the entire application.
The SessionFactory
acts as a factory for org.hibernate.Session
instances, which should be thought of as a corollary to a "unit of work".
And the Java Code there:
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
}
P.S. Your code will probably work, I guess. I found examples at http://www.tutorialspoint.com/hibernate/hibernate_examples.htm for example. But I think we should follow the pattern presented in documentation, because all additional classes like StandardRegistryBuider
are important. And it is always a good policy to follow documentation, right ? :)
![dragongugu smith](https://secure.gravatar.com/avatar/1c9421130a233577fccb4ac5535a6e12?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
dragongugu smith
Full Stack JavaScript Techdegree Student 196 PointsThank u very much ,i learn a lot