Hi ALL,
I started working on GWT 1.5. GWT generics are great. Now it wont
throw errors for annotations.
I created on utility function to get serialized pojos from hibernated
beans which are written based on ejb 3.0 annotations.
How it works:
---------------
It will clone full hibernate proxy object to a bean pojo (relation
ships also). And the pojo is of type hibernate bean. It will copy the
relationships recursively based on depth.
Advantages:
--------------
1)No need to create any dtos
2)No need of hibernate4gwt (I never used it, but I read few blogs)
3)It will support hibernate object tree (means in client side i can
get the tree)
4)for LazyInitialization it will fill the lazy objects with null.
5) No need of 3rd party libs
Disadvantages:
------------------------
1) pojos from Client to Servce ,service to hibernate session not yet
done.
How to Use :
-------------------
1) create your hibernate bean class with only default constructor no
parametrized constructor.
2) All fields of type public .(If u want private ,you can modify this
method slitly)
3) All hibernate beans should be of type IsSerializable
4) The bean class can contain ejb 3.0 annotations, Generics
5) no field should be primitives. (you can use Long in place of long ,
jdk 1.5 will take care of autoboxing thisng..
Util Function:
==================
public static Object toPOJO(Object fromObj,int depth){
Class c=fromObj.getClass();
Object to=null;
System.out.println("POJOConverter.toPOJO"+c);
String className=null;
if(c.getSimpleName().indexOf('$')>0)
className=c.getName().substring(0,c.getName().indexOf('$'));
else
className=c.getName();
try {
System.out.println("POJOConverter.toPOJO"+className);
to=Class.forName(className).newInstance();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Field fields[]=to.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String
fieldName=Character.toUpperCase(fields[i].getName().charAt(0))
+fields[i].getName().substring(1);
try {
Field fromField=fromObj.getClass().getField(fields[i].getName());
if(Modifier.isStatic(fromField.getModifiers())||
Modifier.isFinal(fromField.getModifiers())||
Modifier.isPrivate((fromField.getModifiers())))
continue;
Object
n=fromObj.getClass().getMethod("get"+fieldName).invoke(fromObj, null);
if(n==null)
fields[i].set(to, null);
else if(n instanceof IsSerializable ){
if(depth==0)
continue;
fields[i].set(to, toPOJO(n,depth-1));
}
else if(n instanceof Collection){
if(depth==0)
continue;
Collection fromCol=(Collection)n;
Collection toCol=null;
if(fields[i].getType()==Set.class){
toCol=new HashSet(0);
}else{
toCol=new ArrayList(0);
}
for (Object object : fromCol) {
toCol.add(toPOJO(object,depth-1));
}
fields[i].set(to, toCol);
}
else
fields[i].set(to, n);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return to;
}
}
Sample Bean:
=============
@Entity @Table(name = "message") public class Message2 implements
IsSerializable { public Long id; public Thread thread; public Set s;
public Set t;
public Double rating;
public Long ratingCount;
public String messageText;
public Long creationDate;
public Message2() {
s=new HashSet();
s.add("Hi");
s.add("Hi2");
t=new HashSet();
Thread p=new Thread();
p.id=90l;
t.add(p);
Thread pp=new Thread();
p.id=908l;
t.add(pp);
}
public Long getId() {
return
this.id;
}
public void setId(Long id) {
this.id = id;
}
public Double getRating() {
return this.rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public String getMessageText() {
return this.messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public Long getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
public Long getRatingCount() {
return ratingCount;
}
public void setRatingCount(Long ratingCount) {
this.ratingCount = ratingCount;
}
}
Please post your comments..........
thanks
raghunath
http://gwtwinner.blogspot.com/