Monthly Archives: March 2011

The myth of Galatea revisited / Le mythe de Galatée revisité

I don’t know if the picture has any title, but the myth of Galatea seems to fit.

Source: http://www.publiz.net/wp-content/uploads/2010/08/femme-sculpture-illustration-artistique.jpg

Advertisements

Hibernate Annotations and getter/setter (French and English versions)

L’utilisation du framework Hibernate pour gérer la persistence d’objets Java en base de données présente beaucoup d’avantages (par exemple, si je charge un objet Stage dans l’action Struts2 correspondant à ma page modifier_stage.jsp, les attributs de ma classe Stage sont automatiquement ventilés sur le bon field de mon formulaire), et quelques inconvénients, ou plutôt quelques subtilités.

Je pense notamment à l’emplacement des getter/setter dans les classes persistentes. Le code ci-dessous me produisait l’erreur Unknown column ‘stage.categorie’ in ‘field list’ :

@Column(name=”categorie”)
public void setCategorie(String categorie) {
    this.categorie = categorie;
}
public String getCategorie() {
    return categorie;
}

Après 2 bonnes heures de jurons, de bidouillage de la base, et de réécriture de classes Java, j’ai enfin vu d’où venait le bug.

@Column(name=”categorie”)
public String getCategorie() {
    return categorie;
}
public void setCategorie(String categorie) {
    this.categorie = categorie;
}

Yep, l’annotation Hibernate doit se situer au-dessus du getter (merci, Annso pour la précision : http://stackoverflow.com/questions/3286508/where-to-put-hibernate-annotations). Et après, on se demande pourquoi je passe mon temps à maudire Hibernate.


——————————————————————————————————————-


Using the Hibernate framework to persist Java objects can sometimes be very tricky. For instance, the following code took me 2 hours to get rid of the Unknown column ‘stage.categorie’ in ‘field list’ exception:

@Column(name=”categorie”)
public void setCategorie(String categorie) {
    this.categorie = categorie;
}
public String getCategorie() {
    return categorie;
}

After rewriting my whole Java class, I finally obtained a working code.
@Column(name=”categorie”)
public String getCategorie() {
    return categorie;
}
public void setCategorie(String categorie) {
    this.categorie = categorie;
}

Yep. All you have to do is put the getter right under the Hibernate annotation (http://stackoverflow.com/questions/3286508/where-to-put-hibernate-annotations). Now you get what knavish means.

JavaScript: using RegExp to check a form

First name: capital letters, no accent, possibly an apostrophe, a hyphen, or a space

    function checkFirstName() {
        document.forms[0].firstname.value = correctCaseAndAccents(document.forms[0].firstname.value);
        var isValid = true;
        var expression = new RegExp(“^[A-Z]+([\\-\\’ ][A-Z]+)*$”);
 
        if (!document.forms[0].firstname.value.match(expression)) {
            isValid = false;
            document.forms[0].firstname.style.backgroundColor = ‘#efcfcd’;
        } else document.forms[0].firstname.style.backgroundColor = ‘white’;
 
        return isValid;
    }



French phone number: 10 digits, possibly a dot, a hyphen, or a space – To be improved


    var expression = new RegExp(“^(0[1-9])([\\-\\. ]?)((\\d\\d)([\\-\\. ]?)){3}(\\d\\d)$”);


According to the regexp, the following strings can be accepted:
0125894748
02 48 11 45 35
03.78.54.25.70


This regexp is to be improved because a mix of the allowed delimiters should not be considered as a match: 04-78 2254.71




Check the date of birth (dd/mm/yyyy): to be improved because of leap years


    function checkDateBirth() {
        var isValid = true;
        var expression = new RegExp(“^(0[1-9]|[12][0-9]|3[01])[\\-\\/](0[1-9]|1[012])[\\-\\/](19|20)\\d\\d$”);

        if (!document.forms[0].dateBirth.value.match(expression)) {
            isValid = false;
            document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
        } else {
            if(RegExp.$1 == 31 && (RegExp.$2 == 4 || RegExp.$2 == 6 || RegExp.$2 == 9 || RegExp.$2 == 11)) {
                isValid = false;
                document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
            } else if(RegExp.$1 >= 30 && RegExp.$2 == 2) {
                isValid = false;
                document.forms[0].dateBirth.style.backgroundColor = ‘#efcfcd’;
            }
            else document.forms[0].dateBirth.style.backgroundColor = ‘white’;
        }

        return isValid;
    }


Check the INSEE number (French social security number)



15 digits:
– 1st digit: 1 if male, 2 if female
– 2 last digits: Verification key


    function checkInsee() {
        var isValid = true;    
        var expression = new RegExp(“^[12]\\d{14}$”);
        
        if (!document.forms[0].insee.value.match(expression)) {
            isValid = false;
            document.forms[0].insee.style.backgroundColor = ‘#efcfcd’;
        } else {
            var partialInsee = document.forms[0].insee.value.toString().substring(0, 13);
            var clef = 97 – (parseFloat(partialInsee) % 97);
            if(clef.toString() != document.forms[0].insee.value.toString().substring(13, 15)) {
                isValid = false;
                document.forms[0].insee.style.backgroundColor = ‘#efcfcd’;
            }
            else document.forms[0].insee.style.backgroundColor = ‘white’;
        }

        return isValid;
    }

JavaScript: transform a string to uppercase without accented vowel

<input id=”firstName” onblur=”checkFirstName();” />

function correct(stringToCorrect) {
    var correctedString = stringToCorrect.toUpperCase();
    correctedString = correctedString.replace(/[ÁÀÄÂ]/, “A”);
    correctedString = correctedString.replace(/[ÉÈËÊ]/, “E”);
    correctedString = correctedString.replace(/[ÍÌÏÎ]/, “I”);
    correctedString = correctedString.replace(/[ÓÒÖÔ]/, “O”);
    correctedString = correctedString.replace(/[ÚÙÜÛ]/, “U”);
    return correctedString;
}
  
function checkFirstName() {
    document.forms[0].firstName.value = correct(document.forms[0].firstName.value);
    // Then do stuff with the corrected string
}


This can be declined as follows:


<input id=”firstName” onblur=”correct_bis(this);” />


function correct_bis(myField) {
    var correctedString = myField.value.toUpperCase();
    correctedString = correctedString.replace(/[ÁÀÄÂ]/, “A”);
    correctedString = correctedString.replace(/[ÉÈËÊ]/, “E”);
    correctedString = correctedString.replace(/[ÍÌÏÎ]/, “I”);
    correctedString = correctedString.replace(/[ÓÒÖÔ]/, “O”);
    myField.value = correctedString.replace(/[ÚÙÜÛ]/, “U”);
}

You know you’re a geek when…

There are many articles around the web about what geeks are, what they look like, how to deal with them, and my favorites: how to date a geek and how to live with a geek.
These articles are pretty caricatural, but yesterday I couldn’t help but laugh when reading the list YouMightBe.com made and admitting that a part of it can be applied to me.
So I know I’m a geek because:
  • I know the square root of 65536 is 256 without having to do the math.
  • I e-mail myself notes rather than writing them…
  • …and I can justify the advantages of doing so…
  • … and I often reply to the note.
  • I’ve already written a useless program just for the “fun” of it.
  • and at last but not least, I can program in more languages than I can speak. 
%d bloggers like this: