Friday 28 March 2008

Hibernate 3 - find() -> createQuery() -> createCriteria()

An example of various Hibernate query methods available, see Hibernate 3.2.2 Doc.

find()


List list = session.find(
"from RiskEvaluation where inherentRisk=? and residualRisk=?",
new Object[]{inherent.getLevel(),residual.getLevel()},
new Type[]{Hibernate.STRING,Hibernate.STRING});


createQuery()


List list = session.createQuery(
"from RiskEvaluation where inherentRisk = ? and residualRisk = ?")
.setString(0,inherent.getLevel())
.setString(1,residual.getLevel())
.list();


createCriteria()

  
Criteria criteria = session.createCriteria(RiskEvaluation.class);
criteria.add( Restrictions.eq("inherentRisk", inherent.getLevel()) );
criteria.add( Restrictions.eq("residualRisk", residual.getLevel()) );
List list = criteria.list();

Monday 17 March 2008

Monday 17th March 2008

St Paddy's Day - 3 hours, 65km

Harolds Cross -> Brittas -> Kilbride -> Sally's Gap -> Glencree -> Harolds Cross.

Wednesday 12 March 2008

Hangman with Generics

I'm implementing a hangman game, which uses a HashMap to store a character and result of the character guess.


public abstract class Guess{}

public class CorrectGuess extends Guess{}

public class FailedGuess extends Guess{}

public class Hangman {
private Map<Character,Guess> state = new HashMap<Character,Guess>();
}


I want to be able to get a list of the various correct and failed guesses, so the details can be displayed.

The nasty solution i've currently got is to use "instanceof", but i feel this misses the point of using generics.


// the calling code looks something like
void display()
{
hangmanDisplay.display(getFailedGuesses());
consoleDisplay.display(getCorrectGuesses());
}

public List<CorrectGuess> getCorrectGuesses()
{
List<CorrectGuess> list = new ArrayList<CorrectGuess>(secret.length());
for(Iterator iterator = state.values().iterator();iterator.hasNext();)
{
Guess guess = (Guess) iterator.next();
if(guess instanceof CorrectGuess)
list.add((CorrectGuess)guess);
}
return list;
}

public List<FailedGuess> getFailedGuesses()
{...}


I want to be able to use the generic parameter type to control what is added to the list.


// the calling code would then look something like
void display()
{
hangmanDisplay.display(getGuesses(new ArrayList<FailedGuess>()));
consoleDisplay.display(getGuesses(new ArrayList<CorrectGuess>());
}

/**
* get all values of generic parameter type T in the HashSet and add to the list.
*/
public void getGuesses(List<? super Guess> list)
{
state,values().iterator();
// how can i compare the type thats in the iterator.next() with the type defined by T in the input parameter?
}

Java Generics

I think the The Craftsman 44: Java Generics 2 gives the best exaplaination on when to use extends or super when defining Generics.

Basically use

you can widen the type of a list by using ? extends X, only if you
plan on reading from that list

and

If you plan on writing to that list, use

Tuesday 11 March 2008

The Open Closed Principle

The Open Closed Principle says that you should design modules that never change. When requirements change, you extend the behavior of such modules by adding new code, not by changing old code that already works.