Nico Heid's picture

@OneToMany Relationship with Java, Hibernate and Annotations

NOTE: There is a follow up here: Using an EntityManagerFactory with JPA, Hibernate and Wicket which fixes some design flaws.

If you followed the first article on hibernate: Java persistence with Hibernate and Annotations you might be interested in how to create relationship between classes (or sql tables).

Let's pretend we're writing a little blog software. We only need articles and comments. To keep it simple, one article can have many comments. Comments can have no comments.

The database, once filled, should look like this:

mysql> select * from BlogPost;
+-------------+-----------------------------+---------------------+--------------------------+-----------+----------------+Read more

Phillip Steffensen's picture

Spring Module OXM – A new feature of Spring Framework 3.0

Since a few days Spring 3.0 is out. The frameworks core APIs (e.g. the BeanFactory) have been updated for Java 1.5. But there are also some new features in Spring 3.0. Today I will take a look on Springs new OXM-feature and see how it can be used. Naturally all features added to the Spring framework are easy to use. Let's see if Springs simplicity still exists...

Read more

Matthias Reuter's picture

Circumvention of Opera's Upload Field Path Protection

If you have a form with a file upload field, in some browsers you cannot extract the path to the chosen file. This is meant as a security measure, because it might reveal some information about the user, e.g. the username.

In earlier versions of Opera, if you tried to read the upload field's value, only the file name was given:

var uploadField = document.getElementById("upload");
var path = uploadField.value; // was "foo.jpg"

In the recent version, Opera for some reason reveals a full path, but it's a fake path:

var uploadFIeld = document.getElementById("upload");
var path = uploadField.value; // now "C:\fake_path\foo.jpg"
Read more

Christian Harms's picture

Escaping examples and the worst test data

After the fine and long article about escaping from Matthias here some examples for special characters in a simple web application. This article should be only an inspiration, I will describe some code samples with python/javascript and explain why [<"@%'&_\?/:;,>কী €] is the ultimate input to test input in web applications.

The demo application offers a simple form with name and message field for an one-line guest book.Read more

Matthias Reuter's picture

The art of escaping

Escaping is the art of transforming a text into a transport format from which it can be extracted again without any modification.

That's something every developer does - to a certain level. For example take the sentence

Matthias says: "I love Javascript".

Now put this sentence in a Java source code:

String s = "Matthias says: "I love Javascript".";

If you don't complain about this, your compiler will. In Java (any many other programming languages) the quotation mark " has a special meaning, it defines a string. So if you have a string containing the quotation mark, you need to escape it:

String s = "Matthias says: \"I love Javascript\".";

If you print out s, the original text is shown:

System.out.print(s); // Matthias says: "I love Javascript".
Read more

Christian Harms's picture

TOP 7 Exceptions in my google app engine described

After running the IP-geolocation application in the google app engine some weeks I gained some experience while fetching data and saving into the storage. Looking at the logs with the dash board some interesting errors appeared and that's why I write this article as a notepad for me (or for other python developers). If possible python snipplets will solve the problem cases of errors like "ApplicationError: 5" or "CapabilityDisabledError".Read more

Nico Heid's picture

Java Challenge: Dropping Balloons in Java

Recently I read about a nice article in one of my monthly magazines. I won't give you the author right now, otherwise googling for the solution would be too easy.
He stated it as a possible interview question, but I really like it for the algorithm side of it. It reminded my strongly of some of the
examples given in the book How Would You Move Mount Fuji? Which is excellent by the way.

So your task is to write a Java function which computes the following:Read more

Matthias Reuter's picture

Javascript Challenge: Lotto Number Generator

The German lottery currently holds a jackpot of about 30 million Euro. A friend of mine took the bait and participated yesterday. Since he is a software developer, he wrote a small program to get him six random numbers in the range of 1 and 49.

Well, it's not difficult to write such a programm. The challenge is to do so in little bytes. So I challenge you:

Write a JavaScript function that generates random lotto numbers. This function has to return an array of six different numbers from 1 to 49 (including both) in ascending order. You may use features of ECMA-262 only, that means no Array.contains and stuff. You must not induce global variables.

The function has to look like this

var getRandomLottoNumbers = function () {
    // your implementation here
};
Read more

Christian Harms's picture

IP address regex example - not in java

Finding an IP address in text or string with python is a simpler task than in java. Only the regex is not shorter than in the java regex example!

First an example with python: build a RegExp-Object for faster matching and than loop over the result iterator.

  1. import re
  2. logText =  'asdfesgewg 215.2.125.32 alkejo 234 oij8982jldkja.lkjwech . 24.33.125.234 kadfjeladfjeladkj'
  3. bytePattern = "([01]?\d\d?|2[0-4]\d|25[0-5])"
  4. regObj = re.compile("\.".join([bytePattern]*4))
  5. for match in regObj.finditer(logText):
  6.     print match.group()

A regex like /\d+\.\d+\.\d+\.\d+/ wont work, because there match "999.999.111.000" too. But for the usage in python - that is it! Using a regular expression is more native in python than in java. Or in javascript or in perl or asp.net... Read more

Nico Heid's picture

Regular Expression examples in Java

When I first had to use regular expressions in Java I made some fairly common mistakes.
Let's start out with a simple search.

simple string matching

We want to search the string: asdfdfdasdfdfdf for occurences of dfd. I can find it four times in the String.
Let's evaluate what our little Java program says.

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class RegexCoding {
  5.  
  6.         public static void main(String[] args) {
  7.  
  8.                 String source = "asdfdfdasdfdfdf";
  9.                 Pattern pattern = Pattern.compile("dfd");
  10.                 Matcher matcher = pattern.matcher(source);
  11.                 int hits = 0;
  12.                 while (matcher.find()) {
  13.                         hits++;
  14.                 }
  15.                 System.out.println(hits);
  16.  
  17.         }
  18. }

The result should be 2. So either our code is wrong, or the logic works differently than expected. And indeed, it does.Read more

Syndicate content