dimanche 28 juin 2015

Understand 'detached entity passed to persist' error with @ManyToMany JPA relation

In a JPA many to many relationship, if cascade type has been set at CascadeType.PERSIST (or CascadeType.ALL, which includes CascadeType.PERSIST), then while saving the parent and updating it with references of the child, it will try to save the child again. 

@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH},fetch = FetchType.EAGER)
Following issues can appear
  1. Child is already in persistence store (A detached instance has been passed) -in this case it will throw an exception “org.hibernate.PersistentObjectException: detached entity passed to persist”
  2. Child is a new entry, then it will be added in its table and operation will complete successfully
Following are some solutions
  1. If requirement is that Parent should add a existing but detached child(creating an instance with same ID and passing on from the client) -it needs to be reattached to session first. Hmm -for this there is NO way in JPA -Nope, merge does not reattach and entity. If you are using hibernate, you are lucky, merge can do the trick
    For JPA, best option would be to query for entity on server side before trying to save it.
  2. If its sure that only new child will be added, and not an detached instance from DB, CascadeType.PERSIST will take care of
  3. On the other hand, if requirement is never to add a new child if its not alredy in DB then CascadeType.PERSIST should be removed and cascade={CascadeType.MERGE,CascadeType.REFRESH} should be used

samedi 12 juillet 2014

Custom lazy loading indicator for ajax processing with WICKET APACHE


One of the things we must take care of when we use AJAX is to notify user when an AJAX request is already in progress. This is usually done displaying an animated picture as activity indicator while the AJAX request is running.

Wicket comes with a variant of components that display a default activity indicator during AJAX request processing.These components are respectively IndicatingAjaxButton, IndicatingAjaxLink and
IndicatingAjaxFallbackLink. you can check the official documentation for more details.

Here i will show you my prefered way of making ajax lazy loading indicator : first i want to make global ajax indicator that will be displayed when any component in that page make an ajax process, then i want to display a specific ajax indicator for specific component like submit button, of course with custom indicator picture and styling.

Global AJAX indicator

if you want to show an ajax indicator when any component in a page does an ajax process just make the wicket page implement the interface IAjaxIndicatorAware then override the method getAjaxIndicatorMarkupId(), this method should return the markup id of div panel which contain the custom loading picture.

JAVA code

public class pageWithAjaxProcess extends WebPage implements IAjaxIndicatorAware { 
...
       @Override
public String getAjaxIndicatorMarkupId() {
              return "loadingIndicator_id";
}
....

HTML code

<div id="loadingIndicator_id" style="display:none;">
<img src="assets/lazyLoading.gif" width="48" height="48" alt="loading"/>
</div>

like this you can make your custom loading indicator, it is just a question of css styling.

Specific Ajax indicator

Sometimes we want to display a loading picture only  for submit button which has a long processing time, so we can use IndicatingAjaxButton component and we override its getAjaxIndicatorMarkupId() method for customizing its loading panel.




vendredi 24 janvier 2014

Example syntax for Secure Copy (scp)


Example syntax for Secure Copy (scp)

What is Secure Copy?

scp allows files to be copied to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and same level of security as ssh.

Examples


Copy the file "foobar.txt" from a remote host to the local host

    $ scp your_username@remotehost.edu:foobar.txt /some/local/directory

Copy the file "foobar.txt" from the local host to a remote host

    $ scp foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy the directory "foo" from the local host to a remote host's directory "bar"

    $ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

lundi 20 janvier 2014

How to Install Oracle Java JDK 6/7/8 on Ubuntu 13.04 / 12.10 / 12.04



In this article I will show you how to install the Oracle Java (JDK) 8, Oracle Java (JDK + JRE) 7 or Oracle Java (JDK) 6 on Ubuntu 13.04, Ubuntu 12.10 and Ubuntu 12.04.
The Oracla Java has been removed from the official Ubuntu repositories due to some Java licence issues.
Before you install it, remove OpenJDK, if you have it installed
$ sudo apt-get purge openjdk*

To install Java 8/7/6, do this:
In order not to get issues with the add-apt-repository command, install the following package:

$ sudo apt-get install software-properties-common

Add the PPA:
$ sudo add-apt-repository ppa:webupd8team/java

Update the repo index:
$ sudo apt-get update

install Java 8:
$ sudo apt-get install oracle-java8-installer

Or, install Java 7:
$ sudo apt-get install oracle-java7-installer

Or, install Java 6:
$ sudo apt-get install oracle-java6-installer

mercredi 11 décembre 2013

How to add git remote repository on server

first you connect ssh to your server, then you can set up an empty repository for them by running git init with the --bare option, which initializes the repository without a working directory:

$ cd /var/www/GIT_REPOSITORY
$ mkdir newRemoteProject
$ cd newRemoteProject
$ git --bare init


then you should change the permission into this new remote repository
$chmod -R 777 /var/www/GIT_REPOSITORY/newRemoteProject

Then,all team can push the first version of their project into that repository by adding it as a remote and pushing up a branch. Note that someone must shell onto the machine and create a bare repository every time you want to add a project.

# on Johns computer
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin http://Server_Adresse/GIT_REPOSITORY/newRemoteProject
$ git push origin master

Note : the path to the remote repository is under /www because i have used gitweb with git over http.

samedi 7 décembre 2013

How to disable JavaScript Validation from Eclipse Project ?



  1. Right click your project
  2. Select Properties -> JavaScript -> Include Path
  3. Select Source tab. ( It looks identical to Java Build Path Source tab )
  4. Expand JavaScript source folder
  5. Highlight Excluded pattern
  6. Click Edit button
  7. Click Add button next to Exclusion patterns box.
  8. You may either type Ant-style wildcard pattern, or click Browse button to mention the JavaScript source by name.
The information about JavaScript source inclusion/exclusion is saved into .settings/.jsdtscope file. Do not forget to add it to your SCM.
Here is how configuration looks with jquery files removed from validation