Eclipse IDE 3.7 Indigo and JBoss Tools 3.3.0.M2 / JBoss AS7 (From version 3x – 7x) Installation Steps

By default JBoss AS 5 is the most recent, supported version of JBoss AS in Eclipse IDE. You need an Eclipse server adapter for JBoss AS 7 and that’s what JBoss Tools tooling provides.

Installing JBoss Tools 3.3.0.M2

JBoss Tools 3.3.0.M2’s installation is a simple affair and boils down to a few clicks in the Help > Install New Software… wizard.

According to JBoss Tools 3.3.0.M2 Development Release page, you should use http://download.jboss.org/jbosstools/updates/development/indigo/ for the latest development release of the Core tools.

Plik:jbossas7-osgi-jboss-tools-3.3m2-installation-available-software.png

Select Web and Java EE Development category which includes JBossAS Tools and the required adapter (although the Details section below may have implied that it would not).

Plik:jbossas7-osgi-jboss-tools-3.3m2-installation-install-details.png

Once JBoss Tools is installed, you will see JBoss AS 7.0 server available in the Servers view.

Plik:jbossas7-osgi-jbossas7-define-new-server.png

Registering JBoss AS 7 server

Eclipse IDE has to know about the server location and other configuration settings. You use the Servers view of Eclipse to configure one.

Plik:jbossas7-osgi-jbossas7-jboss-runtime.png

Use an appropriate value for Home Directory where JBoss AS 7 CR1 resides.

The final result is as depicted on the screenshot.

Plik:jbossas7-osgi-eclipse-servers-view-jbossas7-stopped.png

You can manage the server instance easily. Start and stop it to ensure its proper working.

Source: http://www.jaceklaskowski.pl/wiki/OSGi_Development_with_Eclipse_IDE_and_JBoss_AS_7

Equals, Equals Equals, or Equals Equals Equals (=,==,=== Operators) in Java Script

I am sure that you have used the equals sign in your javascript code since it is just about impossible to write any meaningful code without it. Whenever you want to set a variable to some particular value you use an equals like this:

var x = y + z;

In this particular example we have three variables x, y, and z and we either add y and z together (if they both contain numbers) or concatenate their values together (if either of them are strings) and save the result in x. Rather trivial and does exactly what anyone who has been programming with Javascript for more than a few minutes expects. Here’s another example:

if (x = y + z) {alert(‘true’);}

This is in fact the same example as before except that we have combined it with an if statement. Again y and z are added together or concatenated and the result is saved in x for later use. Where this differs from our first example is that the sum or concatenation of y and z is also evaluated to see whether it is true or false (with false, zero and an empty string all being considered to be false while all other numbers and strings are considered to be true. Unless y + z equals 0 or “” the if statement evaluates to true and the alert is run.

A very different situation applies if you add a second equals sign into our if statement (and one which you are more likely to expect.

if (x == y + z) {alert(‘true’);}

No longer is the result of adding y and z together saved in x. Instead the result is compared to x and if they are considered to be equal then the if statement evaluates to true and the alert is run. If x is not equal to y + z then the comparison evaluates to false.

What values are considered to be equal when you compare them though. Let’s say that x is astring containing “3”, y is 5 and z is minus 2. Since 5 + -2 gives 3 the comparison is between a string containing three and a number containing three. In order to properly compare the two fields the number first gets converted to a string so as to give two strings to be compared. Since two strings both of which contain a three are the same the comparison evaluates to true.

Suppose though that we don’t want to allow a string containing a three to match with a number three. Lat’s add another equals.

if (x === y + z) {alert(‘true’);}

This still does a comparison between x and the result of adding y and z but now no conversion of numbers into strings (in fact no type conversions at all) is permitted when doing the actual comparison. Now the values being compared must be of the same data type as well as have the same value. A string containing a three is not of the same type as a number containing a three and so the comparison evaluates as false.

Note that all three of the above if statements are perfectly valid Javascript code, they just do different things. The difference between using two or three equals signs only makes a difference where the values being compared can be either the same or different data types. This means that omitting the third equals will not normally make a difference to the evaluated result making it harder to run a test that will show up the difference. Leaving out the second equals makes a much bigger difference to how the statement works. This means that were your if statement doesn’t work most of the time then a likely cause is the missing second equals.

To minimize the chance of your code not working through a missing equals sign you should always use three equals signs for a comparison unless you specifically wish to allow data conversion. Even then you are better off including the code that deliberately converts the data type rather than allowing it to happen automatically. So if x is a string and y and z are numbers we can make sure that our comparison works as well as make it clear that the right number of equals are present by using:

if (x === String(y + z)) {alert(‘true’);}

Another way of easily detecting whether we have left out an equals where one of the values to be compared is a constant is to place the constant value on the left.

if (3 === y + z) {alert(‘true’);}

The above statement will still work if one of the equals is left out and unless one or other of y and z is a string containing a three and the other is an empty string the same result will be obtained if we accidentally leave out one of the equals. If we leave out two equals this statement generates a syntax error since 3 is not a variable and we therefore can’t assign a value to it.

If we really want an assignment statement then lets make two statements out of it instead of one so as to make it clearer to anyone else reading the code (or ourselves at a later date) that the single equals is intended to be an assignment statement and not a comparison that is missing an equals.

x = y + z; if (x) {alert(‘true’);}

Now it is perfectly clear that we are assigning the result of adding y and z to x and then testing if that result is true rather than that we were trying to compare x with y+z. The code isn’t even all that much longer than if we coded it the other way.

Writing statements that contain one two or even three equals signs can all be valid statements. How you use them is what will lead to either easy to maintain code or a nightmare of trying to analyse how the code is meant to work in order to determine if a second or third equals sign is really missing from a statement or not.

Source: http://javascript.about.com/library/blequals.htm

Hibernate – Non Unique Object Exception

Non Unique Object Exception : a different object with the same identifier value was already associated with the session

Scenario:

When we try to save an object in hibernate, we may get this error under following cases:

sessionObject

 

//SAMPLE 1

MyObject someObjectICreateThatIsNotManaged = new MyObject();
someObjectICreateThatIsNotManaged.setId(1); //Yes setId should not be changable by code, but this is just for demonstration purposed
//(Now within a Transaction/Session)
MyObject objectWithID1 = em.find(1,MyObject.class));
em.saveOrUpdate(someObjectICreateThatIsNotManaged); // This will get that exception thrown because objectWithID1 was loaded and is managed by the EntityManager, and when you then tried to “persist” someObjectICreateThatIsNotManaged, that object has the same id of 1 in it, and someObjectICreateThatIsNotManaged != objectWithID1

//SAMPLE 2

MyObject objX1 = hibernateTemplate.get(objX2.class,new Integer(1));

MyObject objX2 = new MyObject(1);

objX2.setValue(b);

hibernateTemplate.saveOrUpdate(objX2); //throws error

 

Reason for Error:

Here this save or update will throw you NonUniqueObjectException because, in hibernate session, you are creating an object with an ID 1 initially. Then you are taking fetching another object of same type with the same ID 1. As Hibernate Session’s understanding it considers both objects as same object or object that is already associated with the session.

In sample 2 you can see that Hibernate Session is fetching instance of objX1 having ID 1. In this case, if we try to save the objX2 having same ID 1 the exception will be thrown as both the objects are similar in terms of ID (Key that should be unique).

Solution:

We can use different strategies to solve this issue.

  • We can use merge() method available in Hibernate Session instead of using save or update method.
  • Programmatic way

MyObject objX2 = hibernateTemplate.get(objX2.class,new Integer(1));

if (objX2 == null){

objX2 = new MyObject(1);

}

objX2.setValue(b);

hibernateTemplate.saveOrUpdate(objX2);

 

FYI:: Automatic State Detection

According to “Automatic state detection”

saveOrUpdate() does the following:

  • if the object is already persistent in this session, do nothing
  • if another object associated with the session has the same identifier, throw an exception (NonUniqueObjectException in our case)
  • if the object has no identifier property, save() it
  • if the object’s identifier has the value assigned to a newly instantiated object, save() it
  • if the object is versioned (by a <version> or <timestamp>), and the version property value is the same value assigned to a newly instantiated object, save() it
  • otherwise update() the object

and merge() is very different:

  • if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance
  • if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance
  • the persistent instance is returned
  • the given instance does not become associated with the session, it remains detached

 

Reference: Coderanch.com, pro-programmers.blogspot.com