| 
 | 1 | +package com.iluwatar;  | 
 | 2 | + | 
 | 3 | +import java.util.List;  | 
 | 4 | + | 
 | 5 | +import org.hibernate.Query;  | 
 | 6 | +import org.hibernate.Session;  | 
 | 7 | +import org.hibernate.SessionFactory;  | 
 | 8 | +import org.hibernate.cfg.Configuration;  | 
 | 9 | + | 
 | 10 | +public class App   | 
 | 11 | +{  | 
 | 12 | +    public static void main( String[] args ) {  | 
 | 13 | +    	  | 
 | 14 | +    	SessionFactory sessionFactory = new Configuration()  | 
 | 15 | +    	  .addAnnotatedClass(Wizard.class)  | 
 | 16 | +    	  .addAnnotatedClass(Spellbook.class)  | 
 | 17 | +    	  .addAnnotatedClass(Spell.class)  | 
 | 18 | +    	  .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")  | 
 | 19 | +    	  .setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")  | 
 | 20 | +    	  .setProperty("hibernate.current_session_context_class", "thread")  | 
 | 21 | +    	  .setProperty("hibernate.show_sql", "true")  | 
 | 22 | +    	  .setProperty("hibernate.hbm2ddl.auto", "create")  | 
 | 23 | +    	  .buildSessionFactory();  | 
 | 24 | +    	    | 
 | 25 | +    	  Session session = sessionFactory.getCurrentSession();  | 
 | 26 | +    	  session.beginTransaction();  | 
 | 27 | +    	  Wizard wizard = new Wizard();  | 
 | 28 | +    	  wizard.setFirstName("Jugga");  | 
 | 29 | +    	  Spellbook spellbook = new Spellbook();  | 
 | 30 | +    	  Spell spell = new Spell();  | 
 | 31 | +    	  spellbook.getSpells().add(spell);  | 
 | 32 | +    	  wizard.getSpellbooks().add(spellbook);  | 
 | 33 | +    	  session.persist(wizard);  | 
 | 34 | +    	  Query query = session.createQuery("from Wizard");  | 
 | 35 | +    	  List<?> list = query.list();  | 
 | 36 | +    	  Wizard p1 = (Wizard) list.get(0);  | 
 | 37 | +    	  System.out.println(String.format("id=%d firstName=%s", p1.getId(), p1.getFirstName()));  | 
 | 38 | +    	  session.getTransaction().commit();    	  | 
 | 39 | +    }  | 
 | 40 | +}  | 
0 commit comments