Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Hibernate Session.close problem

Options
  • 06-01-2015 2:32pm
    #1
    Registered Users Posts: 1,551 ✭✭✭


    Hi I'm trying to learn Hibernate from Koushiks youtube tutorial but unfortunately a lot of the code he is using is deprecated so I've had to change some stuff and this has caused problems.
    I'm using eclipse to write my code and I'm connecting to postgresSql database.
    I find when I use session.close it causes me to have to close eclipse and reopen it in order for to get any of my code to work.
    It's very frustrating and I don't know why it's happening.
    Can someone help?
    Here's my code.
    HibernateTest.java
    package org.koushik.hibernate;
    
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.koushik.javabrains.dto.UserDetails;
    
    public class HibernateTest {
    	public static void main(String[] args)
    	{
    		UserDetails user = new UserDetails();
    		user.setUserName("First User");
    		
    		UserDetails user2 = new UserDetails();
    		user2.setUserName("Second User");
    		//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    		Configuration cfg = new Configuration();
    		cfg.configure();
    
    		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    
    		SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
    		
    		
    		Session session = sessionFactory.openSession();
    		session.beginTransaction();
    		session.save(user);
    		session.save(user2);
    		session.getTransaction().commit();
    		session.close();
    		
    		
    		
    	}
    
    }
    
    
    UserDetails.java
    package org.koushik.javabrains.dto;
    
    import java.util.Date;
    
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Lob;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.Transient;
    
    @Entity 
    @Table (name="USER_DETAILS")
    public class UserDetails {
    	@Id	@GeneratedValue
    	private int userId;
    	private String userName;
    	
    	
    
    	public int getUserId() {
    		return userId;
    	}
    	public void setUserId(int userId) {
    		this.userId = userId;
    	}
    	
    	public String getUserName() {
    		return userName ;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	
    
    }
    
    
    hibernate.cfg.xml
    <?xml version='1.0' encoding='utf-8'?>
    
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    
    <hibernate-configuration>
           
      <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">password</property>
    
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
    
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
    
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
    
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
        
        <mapping class="org.koushik.javabrains.dto.UserDetails"/>
      </session-factory>
    </hibernate-configuration>
    


Comments

  • Registered Users Posts: 44 john locke


    What exactly isn't working? Are both users not getting saved to the database?

    Does it work the first time and not subsequent times?

    Are you getting any errors or warnings in the console window?


  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    It just would say it was deleting the table everytime I ran it.
    In order for it to save the data I had to exit eclipse and run the program again and then it would work.
    If I ran it again it would just delete the table again.
    I think it may have something to do with session.close();
    I don't know I was hoping someone would have some sort of idea what was going wrong.


  • Closed Accounts Posts: 433 ✭✭MaggotBrain


    quinnd6 wrote: »
    It just would say it was deleting the table everytime I ran it.
    In order for it to save the data I had to exit eclipse and run the program again and then it would work.
    If I ran it again it would just delete the table again.
    I think it may have something to do with session.close();
    I don't know I was hoping someone would have some sort of idea what was going wrong.

    Change the hbm2ddl property to update.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    quinnd6 wrote: »
    It just would say it was deleting the table everytime I ran it.
    In order for it to save the data I had to exit eclipse and run the program again and then it would work.
    If I ran it again it would just delete the table again.
    I think it may have something to do with session.close();
    I don't know I was hoping someone would have some sort of idea what was going wrong.

    Sounds like a schema update is running every time recreating everything, it's a config issue. I've only nHibernate experience configured through code, so I image the hbm2ddl answer above is the key.


  • Registered Users Posts: 1,551 ✭✭✭quinnd6


    Possibly that is the problem.
    I've moved on in the tutorials to some other examples anyway which are working fine.
    What you guys said makes sense I might go back and look at that example again at a later date.
    Thanks guys.


  • Advertisement
Advertisement