datatable/component not updated after deleting and adding new one

During working on tabular data, it is common to have edit and delete command buttons for each row.

And, after we update or delete we want the table to be updated. Let’s looks some simple example.

The table on the JSF might look like


<form id="tablularForm">
<h:datatable id="tabularData" value="#{someBean.list}"
binding="#{someBean.htmlDataTable}" var="data">
<h:column>
<h:outputText id="colum1" value="#{data.column}" />
</h:column>
<h:column>
<h:CommandLink id="edit" value="Edit" action="#{someBean.editAction}" reRender="tabularData"/>
</h:column>
<h:column>
<h:CommandLink id="delete" value="Delete" action="#{someBean.deleteAction}" reRender="tabularData"/>
</h:column>
</h:datatable>
<h:CommandButton id="add" value="add" action="#{someBean.addAction}" />
</h:form>

The above ideal table would work fine.

The datatable would store values by reading from bean and would be updated on Edit and Delete actions accordingly.

This is b/c of the reRender attribute of the components.

The glitch comes when there is no any data in the table. Say you daleted all the data and you want to add a new one.

Or it is the brand new table to be added.

The row would be added but it wont be shown on the table. In short the table would not be updated.

The thing is when we use reRender, we have to use the enclosing component not the component to be safe!.

When it tries to rerender if the component was not there initially, it wont rerender it.
So, here we can use the id of the form for rerendering the datatable and we would be OK.

See why jsf apps are slow in cases

test

Wanted but not invoked: However, there were other interactions with this mock: ->

Wanted but not Invoked Error in mockito and its solution

This happens with mockito when trying to verify the invocation on an object with specific method.

But what happens is you have interacted with other method of that object but not the one mentioned.

If you have an object named CustomerService and say it has two methods named saveCustomer() and verifyExistingCustomer()

And mockito looks something like

verify(customerService, atleast(1)).verifyExistingCustomer(customer), 

But in your actual service you called the saveCustomer() at least once.. BINGO you would get that error.

Also Recommended for you

See how you can get started with mockito with simple example

Do you like Algorithms?

Find K complementary numbers from given array

Can you form the longest palindrome from the given random characters?

eraser

deleted object would be re-saved by cascade (remove deleted object from associations):

Kind of oxymoron ha?!.

You try to delete but you are told it would be re-saved.

Actually it is a safe guard by hibernate that this action is meaningless unless you do the actual stuff.

Two things to check:

  • make sure any collection that holds the object is cleared before deleting the object
  • Clear the child object from the parent object before deleting the child if you have cascade="all"

See also Why jsf applications get slow when using a jpa connection

Learn How to add unicode characters to database from hibernate application

Action in bean is never triggered/called from jsf page

One plus side of the compiled languages would be telling us every error during/after compilation which would save a lot time from chicanery. But, in JSF you may get unexpected error which is a silent.

The page would run and the action linked to, say command button or command link, may not be triggered.

If you got that, just check your form fields – especially if you are working with datatable, make sure all the conversion and validations are working as expected. If the validation/conversion is not working properly, then the framework would exit before reaching the backend.

One way to troubleshoot this would be to have <h:messages /> with appropriate attributes, like adding showDetail=”false” showSummary=”true”, on your form – that, definitely, would help you to debug the problem.

JSF navigation error because of invalid path in faces config

javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)     at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:544)

A continuous and erratic error you would see on the console of this title might come across your JSF development once in a while.

Also you may see the browser is taking long while this error happens.

It has to do with navigation in the faces-config.xml file. in my case, I had forgotten to add the forward slash in front of my file – that is

   <navigation-case>
     <from-outcome>hint</from-outcome>
     <to-view-id>/index.jsp</to-view-id>
   </navigation-case>

The above is snippet from the faces-config.xml file. As you can see, the index.jsp file is without the forward slash – That was the problem

Happy JSFing.

java.lang.NoClassDefFoundError – javax/servlet/jsp/jstl/core/Config

A kind of friendly error that would bade a visit while working on JSF and Tomcat would be: java.lang.NoClassDefFoundError – javax/servlet/jsp/jstl/core/Config.

Thankfully, this has a very simple remedy of adding JSTL jars of standard.jar and jstl.jar.

You can get those jars from http://jakarta.apache.org/site/downloads/downloads_taglibs.html

Invocation of init method failed – hibernate exception of caching

While working with hibernate you might come up with this error, as I do :(
Fortunately, here is how to make your java happy:

On your hibernate.xml [ or maybe you might named it other ], there is a part describing property:

<property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 <prop key="hibernate.show_sql">true</prop>
 <prop key="hibernate.c3p0.minPoolSize">5</prop>
 <prop key="hibernate.c3p0.maxPoolSize">200</prop>
 <prop key="hibernate.c3p0.timeout">1800</prop>
 <prop key="hibernate.c3p0.max_statement">50</prop>
 <prop key="hibernate.generate_statistics">true</prop>
 <prop key="hibernate.cache.use_second_level_cache">true</prop>
 <prop key="hibernate.cache.use_query_cache">true</prop>
 <prop key="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</prop>
 </props>
 </property>

The last two props are the ones we want to blame!!
One way to get rid of this problem would be by just making the values of those to false – hmm… would work but we would miss the capability of caching … Here is the next

Download the ehcache from this location and put it in your classpath.
Update the hibernate.xml file’s property section by adding the following:

<prop key="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</prop>

Now you+java+hibernate+me HAPPY..
hope would help someone..

by pass invalid/self signed certificate in java/glassfish – linux

The following would be handy if one wants to by pass the self signed certificate in java – for app server glassfish.
1. Go to browser and paste the url on it. Let’s assume we are using https://www.sometestcert.com/thispage.asmx
2. On the page double click the lock image [ on firefox you can get it on the right bottom corner ]. Make sure you have https not http to get that lock thing

3. Assuming you are on firefox, goto security -> view certificate -> details-> and Click Export and save it as certificate.pem – choosing x.509 certificate with chain is a good one.
4. then add the key to the glassfish’s keystore.. – say on local machine the default location for key store would be
/usr/local/glassfish/domains/domain1/config.
5. Once you go to the appropriate directory, being on terminalcd , type the following.
keytool -import -alias “sometestcert.com” -keystore cacerts.jks -file /location/to/certificate.pem
6. This time it might ask you a password, if you have specific password for the cacerts then insert it, if not the default would be changeit
7. type yes when asked if you trust the site – of course :)

enjoy certicating..

PHP is displaying questionmark (???) for unicode

One of the fun things in php-mysql is working with Unicode characters. It is so cooked that, we should almost eat those with a little bit adjustment.
I am not going to tutor on how to use php-mysql unicode stuff – there are a bunch of ’em out there.
Rather I would discuss an error that would be common while doing your stuff..
Once you configure everything and put your unicode to mysql, you might see the output of it being questionmark on the browser – but you see the correct unicode in the database through the phpmyadmin.. it is this single line of code you would ever need…
mysql_query(“SET NAMES ‘utf8′”, conn); where the conn is the result of your connection – mysql_connect();
You can modify it accordingly, for example I am using an object oriented one so I can issue the query from my object – you would need to do this only once.

Happy unicoding..

Cannot find .frm file – mysql error

Especially when you move mysql from Window [see related] to Linux using the data file copy, you might face such and error telling you one of your table’s .frm file is not found.
In this case either of the two can solve the problem.
1. be aware that the .frm, for that matter both MDI and MYI files are case sensitive to linux – as it is for all files. So, if on windows you have test.frm and your application access them as TesT.frm, you may not have problem, but it is a major one on Linux.
2. The other is group and ownership issue, make sure the files are under mysql on both ownership and group – unless you have other configuration for that.

TO change ownership use
sudo chmod -R mysql /path/to/data/files
and for group

sudo chgrp -R mysql /path/to/data/files

Hope it would help somebody.