Metadata alternatives in Hibernate with JDK by R4R Team

Meta Data Alternatives in Hibernate Using JDK: We know that in the Hibernate XML does not suit all users so there are some alternative ways to define O/R mapping metadata in Hibernate.


1. Using XDoclet markup:


When we create the Hibernate application that time Many Hibernate users prefer to embed mapping information directly in sourcecode using XDoclet @hibernate.tags. But we can't cover to this approach in this reference guide since it is considered part of the XDoclet. So for this we include the following example of the Cater class with XDoclet mappings:


package HibernateMapping; 

import java.util.Set; 

import java.util.Date; 

public class Cater

private Long id; 

private Date birthdate; 

private Cat mother; 

private Set kittens private Color color; 

private char sex; 

private float weight; 

public Long getId() 

return id; 

private void setId(Long id) 

this.id=id; 

public Cat getMother() 

return mother; 

void setMother(Cat mother) 

this.mother = mother; 

public Date getBirthdate() 

return birthdate; 

void setBirthdate(Date date) 

birthdate = date; 

public float getWeight() 

return weight; 

void setWeight(float weight) 

this.weight = weight; 

public Color getColor() 

return color; 

void setColor(Color color) 

this.color = color; 

}  

public Set getKittens() 

return kittens; 

void setKittens(Set kittens) 

this.kittens = kittens; 

public void addKitten(Cat kitten) 

kittens.add(kitten); 

public char getSex() 

return sex; 

void setSex(char sex) 

this.sex=sex; 

}


2. Using JDK 5.0 Annotations:


In Java JDK 5.0 introduced XDoclet-style annotations at the language level. So we learn there that are type-safe and checked at compile time. This mechanism is more powerful than XDoclet annotations and better supported by tools and IDEs. 


Example:


supports auto-completion and syntax highlighting of JDK 5.0 annotations. The new revision of the EJB specification (JSR-220) uses JDK 5.0 annotations as the primary metadata mechanism for entity beans. Hibernate3 implements the EntityManager of JSR-220 (the persistence API). Support for mapping metadata is available via the Hibernate Annotations package as a separate download. Both EJB3 (JSR-220) and Hibernate3 metadata is supported.


This is an example of a POJO class annotated as an EJB entity bean:


@Entity(access = AccessType.FIELD) 

public class Worker implements Serializable 

@Id; 

Long id; 

String firstName; 

String lastName; 

Date birthday; 

@Transient Integer age; 

@Embedded private Address homeAddress; 

@OneToMany(cascade=CascadeType.ALL) 

@JoinColumn(name="CUSTOMER_ID") 

Set<Order> orders; 

// Getter/setter and business methods 

}


We need to remember one thing that is: Support for JDK 5.0 Annotations (and JSR-220) is currently under development.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!