Vous êtes sur la page 1sur 7

1

Annotations: Annotations are the Java statements which can be used to perform meta data
operations and resource configurations.
1. Annotations are alternate for XML files towards performing resource configuration. Data
about data is called metadata.
2. Configuring resource in XML file and passing more details about that resource to the
underlying executing environment is called as Metadata operations or resource
configuration.
3. Configuring servlet program in web.xml file is called as resource configuration or
metadata operation. From servlet 2.5 api onwards this work can be done in .java files
using annotations.
From Hibernate 3.3 onwards the Hibernate software is coming based on the commond rules and
guide lines of JPA specifications given by SUN Microsystem. So that we can use JPA supplied
annotations in Hibernate programming. Hibernate also supplies it own annotations along with
JPA annotations.
Javax.persistence package represents JPA annotations.
org.hibernate.annotations package represents annotations.
Bsic annotations in Hibernate programming:
@id For singular ID filed configuration.
@column to map member variable with database column table
@table To map Hibernate POJO class with Database table
@Entry To configure Java class as Hibernate POJO class etc., for Hibernate annotations refer
Hibernate API documentation.
For JPA annotations JAVAEE or 5 docs.
In Annotations based Hibernate programming the hibernate mapping file wont be there but cfg
file is required. In this programming Annotations will be added in Hibernate POJO classes will
be specified in configuration file set up required Hibernate programming jdk1.5+ Hibernate 3.3+
The Hibernate POJO class property on which @Transient annotation is applied wont
participates in any persistence operations(CURD operations)
@namedQuery is given as named H!L queries @named NativeQuery is given to make native
SQL queries as namedNativeSQL queries.


2

Note: For example Application please refer Hiberate execution\NamedandNativeNamedQuery
project.
Note: Dont do this application in IDE because IDEs are not giving support to Hibernate 3.6.5.
Note: If we apply Transient annotation sometimes log messages will give wrong details. So
Check once in Database.
For Example program please refer Hibernate execution\Student (Annotations)
EX1: keep Student(Annotations)/TestClient ready
2. Write following two Annotations as additional annotations on the top of student class in
student.java
@NamedQuery(name="p1",query="from Student st where st.sno>=? and st.sno<=?")
@NamedNativeQuery(name="p2",query="select * from Student st where st.addr like :t1",
resultClass=Student.class)
Write the following code in client application.
In TestClient.java:
Query q1=ses.getNamedQuery("p1");
q1.setInteger(0,100);
q1.setInteger(1,105);

List l1=q1.list();
for(int i=0;i<l1.size();i++)
{
Student s1=(Student)l1.get(i);
{
System.out.println(s1.getSno()+" "+s1.getSname()+" "+s1.getAddr());
}
}


3


Query q2=ses.getNamedQuery("p2");
q2.setString("t1","K%");
List l2=q2.list();
for(int i=0;i<l2.size();i++)
{
Student s2=(Student)l2.get(i);
{
System.out.println(s2.getSno()+" "+s2.getSname()+" "+s2.getAddr());
}
}
For Complete program Please refer Hibernate
execution\NamedNativeStudent(Annotations)
While working with XML based named Queries we need to face problems with <(less than)
syntaxes here (Annotations based Named Queries) those problems are not there.
Some important things about Annotations:
1. We cant keep same Annotation for multiple times in single resource/method/field.
2. As the value of one Annotation parameter we can specify other Annotations. Since cant write
@NamedQuery, @NamedNativeQuery Annotations for multiple times we use @NamedQueries
and @NamedNativeQueries Annotations top place multiple Named HQL, NativeSQL queries in
Hibernate POJO classes.
EX: @Enttity
@Table(name=Student)
@NamedQueries((NamedQuery(name=p1, query=from Student st where st.sno>=? And
st.sno<=?)))
@NamedQuery(name=p1,query=delete from student where st.sno=?))


4

Class student
{
------
------
}
For complete program please refer Hibernate execution\NamedQueries and
NamedNativeQueries folder
Versioning in Hibernate: To perform versioning in Hibernate through Annotations use
@version Annotation.
EX:
Step1: Keep Student Application ready
2. Add special member variable in Hibernate POJO class having getter and setter methods.
In Student.java:
int ver;
public void setVer(int ver)
{
this.ver=ver;
}
@Version
@Column(name="vercol")
public int getVer()
{
return ver;
}



5

Add ver_col column in Student table having 0 as the initial value in all records. Write following
code in client application to select and update the records.
In TestClient.java:
Student st1=(Student)ses.get(Student.class,10);
//modifying the record
Transaction tx=ses.beginTransaction();
st1.setAddr("Pulivendula");
ses.save(st1);
tx.commit();
System.out.println("record modified for"+st1.getVer()+"times");
ses.close();

ForComplete Program Please refer Hibernate execution\VersioningAnnotations floder
Algorithms:
Increment Algorithm:
GenericGenerator gives Student.java file with Annotations as Hibernate POJO class. Develop the
client application we can use @GenericGenerator, @Generated value Annotations to configure
algorithm items on singular identity field.
For complete program Please refer Hibernate execution\My Eclipse10.x
projects\TestProject2folder and you can also refer Hibernate
execution\IncrementAlgorithm(Annotations) folder.
Note: For screen shots of executing program in MyEclipse 10.X IDE please refer Snapshots of
Annotations algorithms word document.
Sequence Algorithm:
For Complete program please refer Hibernate execution\SequenceAlgorithm(Annotations)



6

P) Develop a Hibernate application on Inheritance table per class mapping by using
Annotations.
Please refer Hibernate execution\Inheritence mapping(Table per class)(Annotations) folder.
P) Develop a Hibernate application on Inheritance table per sub class mapping by using
Annotations.
Please refer Hibernate execution\Inheritence mapping(Table per sub class)(Annotations) folder.
P) Develop a Hibernate application on Hibernate table per concrete class mapping by using
annotations.
Please refer Hibernate execution\HibernateMappingConcreteClass(Annotations) folder.
P) Develop a Hibernate application on OneToOne foreign key Unidirectional Association
Mapping with Annotations

Please refer Hibernate execution\OneToOne Foreignkey(Annotations) folder
P) Develop a Hibernate application on OneToMany Unidirectional Association Mapping
with Annotations
Please refer Hibernate execution\OneToMany(Unidirectional)(Annotations) folder
P) Develop a Hibernate application on OneToMany Bidirectional Association Mapping
with Annotations
Please refer Hibernate execution\OneToMany(bidirectional)Annotationsfolder
P) Develop a program on OneToOne primary key Bidirectional Association Mapping with
Annotations.

Please refer Hiberate execution\OneToOne PrimaryKey(Annotations) folder
Note: Before executing this program we have to create tables manually otherwise we will get
exception.

ot properly working with Hibernate-3.6.5

Vous aimerez peut-être aussi