Techspace

IT happens only in IT

Problem- java.lang.NoClassDefFoundError: javax/el/ValueExpression Tomcat

Problem- java.lang.NoClassDefFoundError: javax/el/ValueExpression

Possibly
you are using different set of jars at runtime and compile time. In
case of tomcat you might be using some of the jars. Try adding jars
like el-api.jar, javaee.jar, jstl-1.2.jar from your development
environment to the Tomcat/common/lib folder, one by one and it should
solve the problem

Powered by ScribeFire.

January 31, 2008 Posted by Paras | JSP, Tomcat | | No Comments Yet

org.hibernate.TransientObjectException Revisited

This is continuation of my earlier post on org.hibernate.TransientObjectException. Let me write the scenario again

<class name=”com.xxx.A” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
</id>

…………………..
some more mapping elements
…………………..
…………………..
<many-to-one name=”bId” class=”com.xxx.B” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />
</many-to-one>

…………………..

A is referring to B using a primary key column of bId of B.

In that post I have mentioned that if B is a transient object and you don’t want to persist the value of B to A then just tell the hibernate to ignore that value by saying
update=”false” insert=”false” in the many to one mapping.

But what if you want to persist the value of foreign key in A.

Then the approach is different. You have to make sure that instance B is persistent not transient.

That is if your code says something like

A a = new A();
B b = new B();
a.setB(b);
…..
…..

session.save(a);

you are in trouble. Because B is in transient state. You have to attach b to the session.
There may be other ways of attaching this transient object to session. The approach I am following is simple. I am reading the value of B from the database using Hibernate. That way, hibernate attaches B to session and it is then a persistent object.

That is I do something like

A a = new A();
B b = session.get(B.class, new Long(1));
a.setB(b);
…..
…..

session.save(a);

I won’t get any exception because this time there is no transient object to save. All the objects are persistent.

Powered by ScribeFire.

January 28, 2008 Posted by Paras | Hibernate | | 5 Comments

Book Review – Spring in Action

January 25, 2008 Posted by Paras | Book Reviews | | No Comments Yet

MyEclipse Hibernate Spring tutorial – Managing Hibernate transaction in Spring

Spring transaction management, Hibernate transaction management in Spring

There was a problem/defect in the MyEclipse tutorial on Hibernate Spring. It was not a major problem. The tutorial demonstrates the Spring and Hibernate functionality pretty well. The only problem was that the code does seems to work properly. Because of absence of proper transaction management in code the data was not getting persisted in the database. Because of the caching in Hibernate it seems to the user that the data is being written to the database and then read back.

There are two solutions to the problem. One is to write the transaction management code in the java class itself. Another is to manage transaction via Spring’s transaction management. The second solution makes more sense because it shows of the Spring’s capability of managing transaction in Hibernate. You can see both the solutions here.

I am writing the Spring’s configuration for Hibernate transaction management again.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="userDAOTarget" class="com.myeclipse.hibernatespring.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userDAOService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target"><ref local="persistenceLayer"/></property>
</bean>

<bean id="persistenceLayer"
class="com.myeclipse.hibernatespring.PersistenceLayer"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="userDAO">
<ref bean="userDAOTarget" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
</beans>

January 20, 2008 Posted by Paras | Hibernate, Spring | , , | 2 Comments

10061 error while installing MySQL on Windows Vista

I really had a very tough time today while installing MySQL on vista. All the steps in the installation are simple and self-explanatory. I just want to mention two things from my experience

Before starting installation

1) If you want to install MySQL after uninstall, delete your current MySQL fully and properly. After uninstall you should manually delete the installation folder at c:\Program Files\MySQL or at any location your MySQL is installed.

2) Turn off UAC(the nasty part of Windows Vista)

3) Turn off Windows Firewall.

4) Restart your PC.

5) Try now. This time the installation should be smooth and easy

January 20, 2008 Posted by Paras | MySQL, Windows Vista | | 1 Comment

org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:

I was getting this error today.
org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:This is the problemTable A was referring to the talbe B using a foreign key

hbm.xml for A looked like this

<class name=”com.xxx.A” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
</id>…………………..
some more mapping elements
…………………..
…………………..
<many-to-one name=”bId” class=”com.xxx.B” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />
</many-to-one>

…………………..

I just want to save A with some new value and A will refer to B using some foreign key. But in my transaction I am not going to change B and save the changes. Therefore the many-to-one mapping above is wrong. It does not tell hibernate that class b is immutant. That is it is not going to change. Hibernate thinks that the referred table is not saved therefore it complains and says

org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:

Please note that in this case B is transient object. If you don’t really want to save the value of B(transient object) in A , then to get rid of this problem change the many-to-one mapping as

update=”false” insert=”false” fetch=”select”>

But if you want to save the value of B then see my other post

That is, tell hibernate that you are not going to change the values of B, you are just fetching it.

 

January 14, 2008 Posted by Paras | Hibernate | , | 6 Comments

ORA-01747: invalid user.table.column, table.column, or column specification

Today I got this error.

ORA-01747: invalid user.table.column, table.column, or column specification.

There could be many reasons why you can get this error. But the basic reason is the query which is finally getting executed to your database is wrong in some way. In my case, I had a column with column name as DESC. I got rid of this problem by simply dropping the table and creating a new one with column name DESCRIPTION instead of DESC. You may not be able to do that if the table is already existing with many records in it. :-)

Other reason could be that, if you are using named query your named query is not syntactically right.

No matter how the you get this error or for that matter any DB error in Hibernate, the best approach is to enable the show_sql variable in hibernate configuration and get the final query which hibernate is firing against the database.

Get this query and try running it directly in your database client. You can easily find out the culprit part of the query from there. Once you find out root cause of the problem you can think of workarounds or solution for this problem.

And yes, don’t forget to turn off the show_sql parameter after you are done with it. It will unnecessarily clutter your logs.

January 9, 2008 Posted by Paras | Hibernate | , , | 7 Comments

org.hibernate.QueryParameterException: could not locate named parameter

When you query the database in Hibernate using NamedQuery you may run into this error

org.hibernate.QueryParameterException: could not locate named parameter [parm1]

Chances are that you are either trying to set a parmeter to the query which infact doesn’t exist in your query. For example

you are trying to do

query.setParameter(“parm1″, new Long(parm1Value));

whereas, in query there is no parameter like parm1

i.e. query might be just “from Item item”

Or you might be committing some spelling mistake in your code

Eg. query.setParameter(“pram1″, new Long(parm1Value));

and query is “from Item item item.desc like :parm1″

Do note that your parameter name in Java Class is misspelt as pram1 instead of parm1.

January 8, 2008 Posted by Paras | Hibernate | , , | 2 Comments