Hello,
My company is switching from Custom web and persistence frameworks to the de-facto standards such as Hibernate and Spring. What I am trying to do is to figure out why I am seeing some odd behaviors.
I am getting
HibernateException:org.hibernate.HibernateException: /hibernate.cfg.xml not found exception on start up and yet I am able to execute queries. This confuses the heck out of me.
I have hibernate.cfg.xml in my resources folder. Here is the set up
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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:5432/postgres/social</property>
<property name="connection.username">blah</property>
<property name="connection.password">********</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<!--property name="dialect">org.hibernate.dialect.HSQLDialect</property-->
<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.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">update</property>
<mapping resource="/hbm/users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
After this first exception I got the next one
org.postgresql.util.PSQLException: ERROR: relation "users" does not exist. However when I directly call the class that utilizes mapping everything works! Here is my mapping.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dima.webapp.model">
<class name="User" table="users" schema="social">
<id name="userId" column="user_id">
<generator class="native"/>
</id>
<property name="userName" column="user_name"/>
<property name="password" column="user_password"/>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<property name="email" column="email"/>
<property name="dateCreated" column="date_created"/>
<property name="active" column="isActive"/>
</class>
</hibernate-mapping>
and finally here is a method that when called does what it needs to do and returns me a single user.
Quote:
public User getUser(String userName, String password){
User user=new User();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String sql="Select u from User u where user_name=:user_name and user_password=:password";
Query query=session.createQuery(sql);
query.setParameter("user_name", userName);
query.setParameter("password", password);
user=(User) query.uniqueResult();
session.flush();
session.close();
return user;
}
Why do I get exceptions on the start up and why then everything magically starts working? Please help to clarify this "magic".
Thanks.