Hibernate: why do we use inverse=”true” in many to many

I was going through the hibernate tutorial, I couldn’t understand why they used inverse=”true” in the examples. The tutorial wasn’t clear enough.

Consider the case given in the example: Event and Person are two entities and there is a many to many mapping between  them. So when you add a event to a person using this code:

Event e=(Event)session.load(Event.class,eid);
Person p=(Person)session.load(Person.class,pid);
p.getEvents().add(e);
return p;//End of method

As in the above code, if you are making changes to only one end of the relationship (the Person end) then you don’t have to set inverse=”true” in the hbm file. But if you want the addtion of Events to a Person reflect on the Events entity (so that you can get persons attending an event using e.getPersons() )  with out causing hibernate to query the database when you do a event.getPersons(), you can do some thing like this

p.getEvents().add(e);//line a
e.getPersons().add(p);//line b
return p;//End of method

In this case, if you don’t set an inverse=”true”, you will get an error when hibernate tries to insert twice in the join table PERSON_EVENTS, once for line a and once for line b. Setting inverse=”true” on one side of the relationship tells hibernate to ignore changes to the relationship for this ‘many to many’ property. So if you set set inverse to true on one end of the relationship, you’ll see only one insert statement in hibernate log. If you set it on both ends, then hibernate won’t even insert a record in thte join table. If you still didn’t understand, please post your question and I’ll try to answer it.

WordPress with Google gears

I was trying Google Gears with WordPress using Turbo option. The improvement in page load times is remarkable. I used to wait atleast 10 secs for the post page to open up before. Now it just loads in under 3 seconds. Awesome!!!!!!

I love Google Code.

I was looking for a free online SVN repository to store my code. Then I found Google Code. This offers free SVN repository with 100MB space. Now I can access my code any where in the world. It has additional features like wiki, issue tracker etc. Check it out.

initializer block

HashMap map = new HashMap() {
{
put(“item1”, “This is item1”);
put(“item2”, “This is item2”);
}};

This is some syntax I found while googling for something.  Well, if you look at it closely, all we are doing is extending the HashMap and creating a anonymous class that has a initializer block the puts stuff into the hashmap. Simple!

If you are not famliar, an Initializer block is executed before executing the constructor. You may also be interested in static initializer block. (Just google!)

using List ArrayList in Struts Form Bean

I’ve been facing issue with this feature. I think it would be better to use arrays instead of collections / lists. Here is a post that reports the problems I’m facing.

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg29235.html

does http request forwarding preserve request parameters?

Yes.

Consider 3 servlets A, B, C. Client browser submits a request to servlet A with a parameter ‘myParam’ with a value ‘myVal’. A then process the request and forwards the request to servlet B. Now B should be able to access the request parameter ‘myParam’ using request.getParameter(“myParam”). B then forwards to C. Even C should be able to find that parameter in request. So as long as you are not doing a redirect, you should be able to access request parameters in the entire forward chain.

using logic:iterate for displaying text field and other html input fields

<logic:iterate name=”productGroupDetailForm” property=”personalizationTxt” id=”pText” indexId=”pIdx”>
<tr>
<td colspan=”2″>
<html:text property=”personalizationTxt[${pIdx}]” maxlength=”${productGroupDetailForm.productGroupData.productDataList[0].charactersPerLine}”></html:text>
</td>
</tr>
</logic:iterate>

finding Nth element in a list using JSTL

Here is the code:
<c:out value=”${productGroupDetailForm.productGroupData.productDataList[0].charactersPerLine}”/>

It is just like accessing an element from an array. Make sure you don’t exceed the list size.

context-param in web.xml

The parameters set in this element can be accessed by getServeletContext().getInitParameter(“paramName”) in a servlet. Read more about this at:

http://wiki.metawerx.net/wiki/Web.xml.ContextParam