Component ID id:compid has already been found in the view JSF error

This error is quite explanatory in JSF.

I got once in a while when I work with dynamic generation of the components.

If you have this, the most common cause of this error would be you are trying to attach the an html component from your bean again.

Especially, if you have session scoped managed bean and you are attaching dynamically elements, may be one of your methods has already attached the component to the view (like the grid you are using for your component) and the other method is trying to attach it again.

If that is the case you might need to check the existence of the component in the grid (or any component you are using) before attaching it.

Also: see how to add session bean to request

Adding Unicode character to mysql from Java-hibernate

How to enable/add unicode character in hibernate with mysql

Unicode is ruling, it would come handy when working with non ASCII characters like when dealing with Chinese or Ethiopian alphabet amharic characters.

With internalization being the main concern in softwares, bit it website or mobile application, there is a chance you will need unicode in your application.

Hibernate is an ORM, object relational mapper, that is being used with Java and .net. It will allow to abstract all the database related stuffs with simple interface. In this unicode hibernate tutorial, I will show how you can insert, select update and delete records with unicode.

Why do I get No provider for Entity manager Error and how should I fix it

I will show the fix from the hibernate side for MySQL. But keep in mind that unicode characters have to be enabled on the database side as well with proper encoding.

See how you can avoid could not open hibernate session error

The solution for having unicode characters to be recognized as they are passing though the hibernate world would be done on the config file.

Enabling unicode character in hibernate

Here are the steps to follow:

1. go to your hibernate.cfg.xml file

2. make your the connection url something like: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8 – make sure the the ampersand is the html-encoded ampersand.

Do you know you can have boilerplate java app with maven?

Find k complementary numbers – algorithm in Java

Eclipse Helios Classpath Error While Working With Maven

I usually work on both terminal and Eclipse for my development.
Like compiling, creating of the war using maven and usage of Tomcat from terminal, and for coding Eclipse.
But, there are times I might need Eclipse like for debugging. And I want to run Tomcat from Eclipse itself.

I was using Helios, the project runs great from terminal. But, when I try it from Eclipse, it is not publishing it b/c it can’t get the jars.
I have updated the maven dependency and everything seems there. Like when I am developing, Eclipse knows about the imported files and I had no problem.

In short, eclipse can’t import the jars from maven or eclipse class path error was there while reading from maven.

Solution to how to import dependencies from maven to eclipse or how to import jars from maven to eclipse

1. Make sure you have the M2E plugin on your eclipse. To check that:
1.a Go to Eclipse–Help–Install new software
1.b On the work with text box insert Sonatype – http://m2eclipse.sonatype.org/sites/m2e and follow the wizard
When you are done restart your eclipse

2. Check if M2_REPO class path variable is there. To check that:
2.a Window–Preferences–Java–Build Path–Classpath Variable in this there should be M2_REPO variable being set. If it is not set, click the New button and add the repository of your maven path there.

3. Go to Window–preference–Maven–User Settings and check if there are not any warnings there. Mostly on User Settings. If there is one, update with the correct one. I am on Ubuntu and mine is on /etc/maven2/settings.xml

Go to your terminal and issue mvn eclipse:eclipse
then, right click on your project–maven and hit update dependencies.
Here is the fun part::
right click on your project, select build path and look at the library tab your dependencies would be listed there with M2_REPO path at the beginning
Then, Click on Deployment Assesment, it is right above Java Build Path, Select Java Build Path Entries
Hit next, select all the listed ones and hit finish

Refresh the project, clear the tomcat and all should be fine.

This should fix your eclipse class not found exception only because it not reading from maven correctly problem

Cannot forward after response has been committed JSP Error J2EE

This would happen if you try to forward the request twice.
Say you are forwarding to page result.jsp like this

      		 getServletConfig().getServletContext().getRequestDispatcher(
 	        "/result.jsp").forward(request,response);

if you have the same call down the line, BINGO.. you are forwarding it twice.. Have your variables to be sent multiple times but forward them at once.

Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml maven on war

You might come across this error when building a web application on maven using the command

mvn war:war

This would be caused if maven is not aware of where the web.xml file is located.
If you follow the standard maven directory structure as

src/main/java
src/main/resources
src/main/webapp

You would have to copy the contents of WebContent [WEB-INF AND META-INF] to src/main/webapp folder – there maven would get it as expected.

Maven J2EE Eclipse: Hello world in J2EE and Maven on Eclipse

Step by step tutorial on J2EE and Maven on IDE Eclipse

Here is a simple yet basic tutorial for a J2EE application using maven and Eclipse from the scratch showing you step by step.

This tutorial assumes you know the basics of Maven and J2EE. This is just a step by step tutorial to show the marriage of the two.

Also, it is assumed that you have installed maven and tomcat on your machine which are fairly easy to do with simple googling.

Lets Start

1. Create simple Hello world module on Eclipse
1.a Open Eclipse Go to file->new->Dynamic Web Project
1.b Name the project as MavenEclipseJ2EE and hit Finish

We have finished the first step.

Now, maven expects some kind of folder structure. Well, you can do it in your own way and make extra configuration, or you can accept what is accepted as standard and have fun.. I go with the standard maven directory structure.

Here is the maven structure


src/main/java
src/main/resource
src/main/webapp

These are going to the source folders. So lets create them.

2. Creating source folders that are acceptable by maven

2.a Go to terminal or how ever you would create folders and create the folders as:

MavenEclipseJ2EE/src$ mkdir main
MavenEclipseJ2EE/src$ mkdir main/java
MavenEclipseJ2EE/src$ mkdir main/resources
MavenEclipseJ2EE/src$ mkdir main/webapp

2.b Go back to Eclipse, select the project and hit F5 or right click on Project and hit refresh. You will see the newly created folders under src

2.c Right click on the project, select properties, select build path and hit source tab

2.d You would see the previous source folder there. Select it and hit remove to remove it. We do this since we are changing the source folders

2.e Then hit the button add folder, expand src, expand main and select the java, resources and webapps only. Hit OK and hit OK again to finish creating source files.

Cool, so our folder structure is ready for Maven. Now lets move to the maven itself.

Maven relies highly on the pom.xml file that we would put on the project root folder. Lets do that.

Continue to part two

The BASEDIR environment variable is not defined correctly This environment variable is needed to run this program

BASEDIR environment not define error and its solution

Are you working on J2EE with Tomcat?

Trying to run Tomcat from the command line and got this problem?

I got this while working on apache-tomcat 7.

Here is how I solved it.

The basedir environment will complain if you have either the JAVA_HOME and/or the CATALINA_HOME paths are not correctly set on the environment variables.

where to get the environment variable? -> in /etc/environment.
Once you make sure paths are set correctly, the next would be allowing the shell scripts to have the executable permissions.

Basically I would do
sudo chmod 777 -R /PATH/TO/TOMCAT/bin

This, as you know is not advisable if for production or networked environments as it will allow all to execute.

Now, you should be able to startup tomcat as
./startup.sh by going into the tomcat’s bin folder.

Recommended

What is can not access member of class with modifiers and its solution?

Change port in tomcat

See what the no persistence provider error mean for entity manager and how to solve it

re-structre

Change default source directory src/java in eclipse for java project

Prepare directory structure for maven by changing the default src

Probably you are using maven and wanted to change the directory structure.

Since maven recommends the standard src/main/java src/main/resource you might want to change it that way

Here is how to do it in eclipse.

First create the folder structure on the project in which ever way you would want to do it. I would use simple command like mkdir folderName to create it.

1. right click on the project, and hit refresh, make sure the folders you created are listed there.

2. Right click on the project, select properties and select java build path

3. Go to source tab

4. select and remove the current source folder, by default it would be src folder

5. Hit the Add newthen select your folder structure there.

maven directory structure

maven directory structure

This would change the original structure of the your java project from src to the one maven compatible.

See how to use maven to create starter boilerplate application
Get started with step by step j2ee and maven tutorial using eclipse

Can you solve these cool algorithm problems? See their solution as well

How do you find the maximum consecutive sum from the given array

You are given billion numbers and to look for a couple of missing numbers

From list of numbers in array, find those that are complementary to the given number

NonUniqueObjectException: a different object with the same identifier value was already associated with the session

The error different object with the same identifier value was already associated with the session would happen quite sometime while working on hibernate with java web application

It occurs when the hibernate session contains object to be updated and when the application tries to update another object with the same id as that of the session owned one.

Let’s assume there is an object Can with properties material and volume as follows for simple illustration


public class Can{
  private Integer canId;
  private String material;
  private Double volume;
  //getters; setters;
}

Assume we have DAO for this object named CanDao.

So, being on your jsf dataTable you selected one ‘can’ object to be updated or deleted. I just put the CanDao object directly into the Bean for the illustration purpose – it is good to wrap this object in the service object, say CanService, for easier manupulation.

Lets have the bean as follows:


class CanBean{
   private HtmlDataTable canTable; //for binding list of can objects from jsf list
   private CanDao canDao; //dao object;
   private List cans; //list jsf datatable would use it.
   .
   .
   .
   public String update(){
      //get the selected object from data table
      ICan selectedCan = (ICan)this.canTable.getRowData();
      this.canDao.saveOrUpdate(selectdCan);
      return "Update";
   }
}

Here the line calling saveOrUpdate would be responsible for throwing the error of different object with the same identifier value was already associated with the session

Here is how to fix it
Since the session contain the same identifier, let’s get the actual or original object from the session it self


  //corrected update method for Can Bean
   public String update(){
      //get the selected object from data table
      ICan selectedCan = (ICan)this.canTable.getRowData();
      ICan original = this.canDao.getById(selectedCan.getCanId()); //
      original.setVolume(selectedCan.getVolume());      
      original.setMaterial(selectedCan.getMaterial());
      this.canDao.saveOrUpdate(original);
      return "Update";
   }

This would fix the problem. But, there are other ways of preventing the session problem from occurring – googling a bit might help.

If you are J2EE developer:

Hello world with maven step by step

See how you can add session bean

Why do I get can not forward after response error and how to solve it

Bean CurrentlyInCreationException: Error creating bean with name : Requested bean is currently in creation: Is there an unresolvable circular reference?

This CurrentlyInCreationExceptionexception happens during the construction of the bean.

If the bean has reference to other beans, and if those beans which are referenced have the this bean as property or constructor parameter, then it would be cyclic and hence this exception would be thrown.

So, check if you have beans that are referring to each other on you hbm/annotations files.