Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. Session session = HiberanteUtil.getSessionFactory().openSession();
  2.  
  3. org.hibernate.HibernateException: /hibernate.cfg.xml not found
  4.  
  5. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  6.  
  7. SessionFactory sessionFactory = new Configuration()
  8. .addAnnotatedClass(User.class).buildSessionFactory();
  9.  
  10. import java.util.Properties;
  11. import org.hibernate.HibernateException;
  12. import org.hibernate.Session;
  13. import org.hibernate.SessionFactory;
  14. import org.hibernate.cfg.AnnotationConfiguration;
  15. import com.concretepage.persistence.User;
  16.  
  17. public class HibernateUtil {
  18. private static final SessionFactory concreteSessionFactory;
  19. static {
  20. try {
  21. Properties prop= new Properties();
  22. prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
  23. prop.setProperty("hibernate.connection.username", "root");
  24. prop.setProperty("hibernate.connection.password", "");
  25. prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
  26.  
  27. concreteSessionFactory = new AnnotationConfiguration()
  28. .addPackage("com.concretepage.persistence")
  29. .addProperties(prop)
  30. .addAnnotatedClass(User.class)
  31. .buildSessionFactory();
  32. } catch (Throwable ex) {
  33. throw new ExceptionInInitializerError(ex);
  34. }
  35. }
  36. public static Session getSession()
  37. throws HibernateException {
  38. return concreteSessionFactory.openSession();
  39. }
  40.  
  41. public static void main(String... args){
  42. Session session=getSession();
  43. session.beginTransaction();
  44. User user=(User)session.get(User.class, new Integer(1));
  45. System.out.println(user.getName());
  46. session.close();
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement