Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Id;
- import javax.persistence.Entity;
- import javax.persistence.Table;
- @Entity
- @Table(name = "countries")
- public class Countries implements Serializable {
- @Id
- @Column(name= "ID")
- private String id;
- @Column(name= "COUNTRY_NAME")
- private String country_name;
- //@Column(name= "region_id")
- //private int region_id;
- public String getId(){return id;}
- public String getName(){return country_name;}
- public void setId(String id) {
- this.id = id;
- }
- public void setName(String name) {
- this.country_name= name;
- }
- }
- import java.io.File;
- import java.util.Locale;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil {
- //private
- static SessionFactory sessionFactory;
- /*
- static{
- try{
- sessionFactory=new AnnotationConfiguration().addAnnotatedClass(Countries.class).configure().buildSessionFactory();
- // AnnotationConfiguration aconf = new AnnotationConfiguration()
- /// .addAnnotatedClass(Countries.class);
- // Configuration conf = aconf.configure();
- // sessionFactory=conf.buildSessionFactory();
- }catch(Throwable e){
- e.printStackTrace();
- }
- }
- */
- // private static final Logger LOG = Logger.getLogger(Factory.class);
- static {
- // This line is needed for right working
- Locale.setDefault(Locale.ENGLISH);
- try {
- // File config = new File("hibernate.cfg.xml");
- sessionFactory=new Configuration().addAnnotatedClass(Countries.class).configure().buildSessionFactory();
- // sessionFactory = new Configuration().configure(config).buildSessionFactory();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- }
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- public class Main {
- public static void main(String[] args) {
- Session session = null;
- try{
- SessionFactory factory = HibernateUtil.getSessionFactory();
- session = factory.openSession();
- // session.beginTransaction();//error
- Countries c = (Countries)session.get(Countries.class, new String("RU"));
- System.out.println(c.getName());
- session.close();
- factory.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement