package ru golodnyj days import org hibernate SessionFactory import or

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ru.golodnyj.days;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;
import ru.golodnyj.days.beans.User;
/**
* User: golodnyj
* Date: 12.07.2009
* Time: 11:16:20
*/
public class Console {
private static final SessionFactory ourSessionFactory;
static {
try {
AnnotationConfiguration aconf = new AnnotationConfiguration();
ourSessionFactory = aconf.
configure("hibernate.cfg.xml").
buildSessionFactory();
}
catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(String[] args) {
final Session session = getSession();
session.beginTransaction();
User u = new User();
u.setUlogin("Login");
u.setUpassword("Password");
session.save(u);
session.getTransaction().commit();
session.flush();
session.close();
}
}