Advertisement
garfunkel

Lena's Hibernate

Apr 1st, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.io.Serializable;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.Id;
  5. import javax.persistence.Entity;
  6. import javax.persistence.Table;
  7.  
  8. @Entity
  9. @Table(name = "countries")
  10. public class Countries implements Serializable {
  11.     @Id
  12.     @Column(name= "ID")
  13.     private String id;
  14.  
  15.     @Column(name= "COUNTRY_NAME")
  16.     private String country_name;
  17.  
  18.     //@Column(name= "region_id")
  19.     //private int region_id;
  20.  
  21.     public String getId(){return id;}
  22.  
  23.     public String getName(){return country_name;}
  24.  
  25.     public void setId(String id) {
  26.         this.id = id;
  27.     }
  28.     public void setName(String name) {
  29.         this.country_name= name;
  30.     }
  31. }
  32.  
  33. import java.io.File;
  34. import java.util.Locale;
  35.  
  36. import org.hibernate.SessionFactory;
  37. import org.hibernate.cfg.Configuration;
  38.  
  39. public class HibernateUtil {
  40.     //private
  41.     static SessionFactory sessionFactory;
  42.     /*
  43. static{
  44.     try{
  45.     sessionFactory=new AnnotationConfiguration().addAnnotatedClass(Countries.class).configure().buildSessionFactory();
  46.     // AnnotationConfiguration aconf = new AnnotationConfiguration()
  47.     /// .addAnnotatedClass(Countries.class);
  48.     // Configuration conf = aconf.configure();
  49.  
  50.     // sessionFactory=conf.buildSessionFactory();
  51.     }catch(Throwable e){
  52.         e.printStackTrace();
  53.     }
  54. }
  55.     */
  56.  
  57. // private static final Logger LOG = Logger.getLogger(Factory.class);
  58.  
  59. static {
  60.     // This line is needed for right working
  61.     Locale.setDefault(Locale.ENGLISH);
  62.     try {
  63.         // File config = new File("hibernate.cfg.xml");
  64.         sessionFactory=new Configuration().addAnnotatedClass(Countries.class).configure().buildSessionFactory();
  65.         // sessionFactory = new Configuration().configure(config).buildSessionFactory();
  66.     } catch (Throwable e) {
  67.         e.printStackTrace();
  68.     }
  69. }
  70.  
  71.     public static SessionFactory getSessionFactory() {
  72.         return sessionFactory;
  73.     }
  74. }
  75.  
  76.  
  77.  
  78. import org.hibernate.Session;
  79.  
  80. import org.hibernate.SessionFactory;
  81.  
  82. public class Main {
  83.     public static void main(String[] args) {
  84.     Session session = null;
  85.     try{
  86.         SessionFactory factory = HibernateUtil.getSessionFactory();
  87.         session = factory.openSession();
  88.         // session.beginTransaction();//error
  89.         Countries c = (Countries)session.get(Countries.class, new String("RU"));
  90.         System.out.println(c.getName());
  91.         session.close();
  92.         factory.close();
  93.     }catch(Exception e){
  94.         e.printStackTrace();
  95.     }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement