0% found this document useful (0 votes)
13 views3 pages

Hibernate Annotations

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Hibernate Annotations

Uploaded by

vishalwdv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1-Oct

2 ways to provide metadata


A)XML Approach [Hibernate version 3]
-POJO/Entity class
-Client code
-hibernate.cfg.xml [db configurations]
-pojo.hbm.xml [class-table mapping inf]

B)Annotation Approach [Hibernate version4 onwards]


-POJO/Entity class with annotations[replace hbm file]
-Client code
-hibernate.cfg.xml [db configurations]
__________________________________________________________

****Annotations
-starts with @symbol

-used to associate metadata about some programming element like


class,method,variable
eg:-@Override

-used to hold metadata along with the java code & so must be used for fixed stuff

-Annotations are inside the java code [not external like xml] & so if changed java
code needs to rebuild

**use
DB COnfigurations will keep on changing -[hibernate.cfg.xml]
Table structure remain same-[use annotation]

__________________________________________________________
Annotations
@Entity--->marks a class as a Entity/POJO class
@Table-->specific table name
@Id--->specific PK column
@Column--->specify non PK columns

___________________________________________________
steps for Hibernate-Annotation
1)open eclipse
2)create java project & add hibernate+db jar files

3)create pojo class[com.hibernate.model] with annotations


@Entity,@Table,@Id,@Column

4)mapping in hibernate.cfg.xml last line


<mapping class="pojo class name" />

5)create client class[com.hibernate.client]


__________________________________________________________

Lab1)Hibernate with Annotations


create Customer class-->
custId,custName,contactNo,birthDate
perform following operations --->save/get/update/delete
create a package com.hibernate.util --->HibernateUtil---> static method
public static SessionFactory obtainSessionFactory(){
....
}
In client
SessionFactory factory=HibernateUtil.obtainSessionFactory();

__________________________________________________________

****Component Mapping
How to map has a kind of Class relationship[Containment]

@Entity @Table
class Employee{
@Id,@Column
empid

@Column
empname,empsal,jdate;

@Embedded
Address ob;
}
@Embeddable
class Address{
@Column
street,city,country,pincode;
}
//Here do not create a seperate table for class Address

-Here we use 2 annotations


@Embedded specifies not to create a new table for that property

@Embeddable applied to some class which wud be embedded in other class

_________________________________________________________________________
Lab2)
Profile [Main POJO]
--------
int profileId
Date creationDate
PersonalDetails perInfo
EducationalDetails eduInfo

PersonalDetails
----------------
String firstName,lastName,email,contactNo,gender
Date birthDate
Address resAddr

Address
-------
String street,city,country,zip

EducationalDetails
------------------
String qualification,result
int yearPassing
__________________________________________________

create a different clients for above assignment with following facilities


Add a new Profile
Search a Profile
Update a Profile
Delete a Profile

__________________________________________________

You might also like