簡體   English   中英

獲取org.hibernate.HibernateException:未配置CurrentSessionContext! 嘗試連接到heroku-postgresql Hibernate時

[英]getting org.hibernate.HibernateException: No CurrentSessionContext configured! when trying to connect to heroku-postgresql Hibernate

我收到此錯誤: org.hibernate.HibernateException: No CurrentSessionContext configured!

嘗試連接到Heroku Postgres DB時。

這是我的休眠配置(我稱之為: hibernateUser.cfg.xml ):

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

   <session-factory>

    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://ec2-54-243-213-188.compute-1.amazonaws.com:5432/dess6n165jarrv</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password">password</property>

    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    <property name="hibernate.default_schema">dess6n165jarrv</property>
    <property name= "hbm2ddl.auto">update</property>

    <property name="hibernate.current_session_context_class">thread</property>

   </session-factory>

</hibernate-configuration>

在這里輸入代碼

我將其實現為單例:

public static HibernateUserDAO getInstance(HttpServletResponse response) throws IOException 
    {   

        instance = new HibernateUserDAO();
        if (instance == null)
        {
            userFactory = new Configuration().configure("hibernateUser.cfg.xml").addAnnotatedClass(AppUser.class).buildSessionFactory();
        }
        return instance;
    }


    public void addNewUser(AppUser user) throws UserExceptionHandler, IOException 
    {   

        Session session = null; 

        int id = 0;
        try
        {
            session = userFactory.getCurrentSession();

            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();


        }catch (HibernateException e) 
        {
            if (session.getTransaction() != null) session.getTransaction().rollback();
            throw new HibernateException (e);
        }finally 
        {
            try 
            {session.close();} 
            catch (HibernateException e)
            {
                throw new UserExceptionHandler("Warnning!! connection did'nt close properly");
            } 
        }
        if (id != 0) 
            System.out.println("User created successfully");    

    }

enter code here

我在這條線上出現錯誤:

session = userFactory.getCurrentSession();

我認為錯誤出在我的配置文件中,但我看不出問題出在哪里(我讀了幾個示例,然后復制了它)

您需要像這樣動態設置連接參數:

Map<String,String> jdbcUrlSettings = new HashMap<>();
String jdbcDbUrl = System.getenv("JDBC_DATABASE_URL");
if (null != jdbcDbUrl) {
  jdbcUrlSettings.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL"));
}

registry = new StandardServiceRegistryBuilder().
    configure("hibernate.cfg.xml").
    applySettings(jdbcUrlSettings).
    build();

這樣,它們就不會進行硬編碼,並且可以提取平台所做的更改。 有關更多信息,請參見將DATABASE_URL與Hibernate一起使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM