background preloader

Dao

Facebook Twitter

Lesson: Generics (The Java™ Tutorials > Bonus) Introduced in J2SE 5.0, this long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. IntroductionDefining Simple GenericsGenerics and SubtypingWildcardsGeneric MethodsInteroperating with Legacy CodeThe Fine PrintClass Literals as Runtime-Type TokensMore Fun with WildcardsConverting Legacy Code to Use GenericsAcknowledgements.

Class Literals as Runtime-Type Tokens (The Java™ Tutorials > Bonus > Generics) One of the changes in JDK 5.0 is that the class java.lang.Class is generic. It's an interesting example of using genericity for something other than a container class. Now that Class has a type parameter T, you might well ask, what does T stand for? It stands for the type that the Class object is representing.

For example, the type of String.class is Class<String>, and the type of Serializable.class is Class<Serializable>. This can be used to improve the type safety of your reflection code. In particular, since the newInstance() method in Class now returns a T, you can get more precise types when creating objects reflectively. For example, suppose you need to write a utility method that performs a database query, given as a string of SQL, and returns a collection of objects in the database that match that query.

One way is to pass in a factory object explicitly, writing code like: You can call this either as or you can declare a class EmpInfoFactory to support the Factory interface and call it. Java Data Structure: A Generic Tree. A few weeks ago, I was talking to another Java programmer who was once a C++ programmer. The conversation gravitated towards data structures, and about how C++ programmers need to consider the best data structure for their particular application and then figure out the best and most optimal way to build it, while Java programmers typically just choose the most optimal data structure for their application from the standard java.util library.

In my opinion, this is both a good thing and a bad thing for Java programmers. Its good because you dont have to worry about implementing a Vector for each project (or remember which third party library to include or purchase), and its bad, because it makes you lazy. So lazy, in fact, that when the time comes to implement a data structure that is not available in the Java standard library, or cannot be synthesized by combining Maps and Lists, Java programmers are often at a loss for how to proceed.

The basic Tree Data Structure Usage. Reflecting generics. Code by Any Other NameReflecting genericsby Ian RobertsonJune 23, 2007 Summary Type arguments to generic classes are not available for reflection at runtime - or are they? The type arguments for statically declared types can be discovered at runtime. A look at how to do this, and why you might want to. Probably the most common complaint about generics in Java is that they are not reified - there is not a way to know at runtime that a List<String> is any different from a List<Long>. I've gotten so used to this that I was quite surprised to run across Neil Gafter's work on Super Type Tokens.

It turns out that while the JVM will not track the actual type arguments for instances of a generic class, it does track the actual type arguments for subclasses of generic classes. In other words, while a new ArrayList<String>() is really just a new ArrayList() at runtime, if a class extends ArrayList<String>, then the JVM knows that String is the actual type argument for List's type parameter. Hibernate DAO using Java 5 Generics | Nilesh's Space.

For one of my projects, I have used Java 5 Generics to simplify creation of Hibernate DAO’s. I no longer have to duplicate the typical CRUD operations in every DAO class. Note that I am using HibernateDaoSupport. If you want to see an example of a non-Spring generic DAO, the authors of Hibernate have posted one on their blog.

Though mine may not be the best practices for Hibernate, I thought I’d post it just to show an example of what can be done with Java 5 generics and how much code can be reduced with it. Here is my generic class: I have a generic interface to go along with this class: public interface AbstractDAO <DomainObject, KeyType> { public DomainObject load(KeyType id); public void update(DomainObject object); public void save(DomainObject object); public void delete(DomainObject object); public void deleteById(KeyType id); public List<DomainObject> getList(); public void deleteAll(); public int count(); } public interface PageCacheDAO extends AbstractDAO<PageCache, Integer> { }

Java Generics FAQs - Type Parameters - Angelika Langer Training/Consulting. Generic Data Access Objects. An Army of Solipsists » Blog Archive » Using Generics to Simplify Hibernate/Spring DAOs (update)