Apache Maven 3 Cookbook
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 5<br />
Finally, we create the main application to use our entity in App.java:<br />
package net.srirangan.packt.maven.TestHibernateApp.app;<br />
import net.srirangan.packt.maven.TestHibernateApp.domain.User;<br />
import org.hibernate.Session;<br />
import org.hibernate.cfg.AnnotationConfiguration;<br />
public class App {<br />
public static void main( String[] args ) {<br />
AnnotationConfiguration configuration = new<br />
AnnotationConfiguration()<br />
.addPackage("net.srirangan.packt.maven.TestHibernateApp.<br />
domain")<br />
.addAnnotatedClass(User.class);<br />
Session session = configuration.buildSessionFactory().<br />
openSession();<br />
User user1 = new User();<br />
user1.setUsername("hello");<br />
user1.setPassword("world");<br />
session.save(user1);<br />
session.close();<br />
}<br />
}<br />
It's a very simple example that starts a session, creates a user, and saves it in the database.<br />
When you execute this application, the console will show a log of Hibernate starting up and an<br />
insert query to save the User that was created.<br />
If you check your database, a new table named user was created with one record inserted<br />
with the username as hello and password as world while id would be 1.<br />
mysql >> show tables;<br />
|| *Tables_in_testdatabase* ||<br />
|| user ||<br />
mysql >> select * from user;<br />
|| *id* || *password* || *username* ||<br />
|| 1 || world || hello ||<br />
117