Upload
jaffarshaik
View
236
Download
3
Embed Size (px)
DESCRIPTION
Hibernate Study Material
Citation preview
Hibernate 3.0
��������� ���� ����� ��� �� ���������������� �
������ �������������� � � ��
���������������
� ������� ���� ��
�������
� �� ������������������
���������������������������� �������
�����������������
!�����������������
���������������������
�������� ���������
� "����!����#���� ������
$�%� % ���$�%� %����������%� %����
What is Persistence ?
�� ������� �&���' ����������� � ������������ �������������������"� !������������
� �� '����� �� ��� ���� ���� ������� �����
()������*��� �
���+��'���������� !��� ����� ������������������' ������������)
,)�-�
.����� �������� '�� ���/��������� �����������)
0)�$�&��% ���������������������������
$$��������������+�����������������������������������"�)
Mapping
����������������������������������� �&��+�������������� �� �� �'����� '����������)
import java.io.Serializable;import java.util.Set;public class Employee implements Serializable {
private String name;….private Set addresses;public Integer getAge() {
return age;}public void setAge(Integer age) {….public Set getAddresses() {
return addresses;}public void setAddresses(Set addresses) {
this.addresses = addresses;}
package com.test;
public class Address implements Serializable{
private Integer id;….private Employee empObj;public Integer getId() {
return id;}…..public void setId(Integer id) {
this.id = id;}public void setEmpObj(Employee empObj) {
this.empObj = empObj;}
}
Common Mapping Techniques
������������
���������"������������ ��' ���������������� ���������������������
���������� �&������ ���������������)
���������
������������������������ �������!������� �&����� ������
�������� �����������������������"������������������������ !� '���� �&�����'����
������������������������������"��������� �&������� �&����� ������������)
!���� ��"������
.� ���������������!������ �&������������' ������������������������
��� ��������� ������)��'������������� ������������ �&�� �������!���������������
����������������� �����������������������)��
Common Mapping Techniques
#�������������������
!�������������������������
����%������������������������
�������� ��������������������������
������ ������
$��1 � �1 $�
$��1 � �1 ����
�����1 � �1 ����
Chapter 2- Introduction to Hibernate
#���������$
������������&���%����������!���������� �� �����$2#���������� ��)
.��������
() �������� ���/��3 �� ������ �����)
,) ��������/���������������' !��
0) ������������������������ ���� ���/�������
4) ���� ���� �� ���� ������������������ ����� ��)�5��)
Chapter 2- Introduction to Hibernate
#���������%�����������
Application Classes
Hibernate
JDBC JNDI JTA
Database Server
Chapter 2- Introduction to Hibernate
#���������� �&������� �
Application Classes
Hibernate
JDBC JNDI JTA
Database Server
Configuration
Chapter 2- Introduction to Hibernate
#���������� �&������� �
���� �'������ �������+�� ���� �������! �"��� �� ������������������ ����� ������
��������%������������)
���'����� �� ���������������� ���� �� �� �� �'������ � '������� ������
������)������'������������)� ���� ����������)�'�)/��'��)
Chapter 2- Introduction to Hibernate
67/������ �89():9��� ����89��'%;97<
6=>$��?�5�������%� �'������ ���@A ��
B%22������2�������� �'������ ��>�>�0):225CB
B����D22������)� ��' �)��2������%� �'������ �%0):)���B<
6������%� �'������ �<
6���� �%'��� �<
6=%% >�������� ����� ����������%%<
6� �������8B� ����� �)���E�����B<� �)�����)&���)>��62� ���<
6� �������8B� ����� �)��B<&���D�����D22� ���� ��2������62� ���<
6� �������8B� ����� �)�����B< �62� ���<
6� �������8B� ����� �)����! �B<�����62� ���<
6=%% F>A��� ����� ��� ��G�����������%��H�%%<
6� �������8B� ����� �)� �E��*B<(62� ���<
6=%% �3 ��������%%<
6� �������8B������B< �)������)������)��3 >�����62� ���<
62������%� �'������ �<
Chapter 2- Introduction to Hibernate
6=%% 5�����������9� ��� ���������� ��� ��/�����������%%<
6� �������8B����E���� �E� ��/�E�����B<����62� ���<
6=%% >����������� ��%����������%%<
6� �������8B����)� ���E�����B< �)������)����)C ����� ���62� ���<
6=%% 5�� �����/������3 �� ���� �� %%<
6� �������8B�� !E���B<��62� ���<
6=%% > ������%��������������������� ���������%%<
6� �������8B���,���)��� B<���62� ���<
6��������� ��8B� �2���25��� �)���)/��B2<
6��������� ��8B� �2���2.����)���)/��B2<
62���� �%'��� �<
Chapter 2- Introduction to Hibernate
C !�� ��������������*�������������
���� �I��� ����� �8�!�� �'������ �GH)� �'���GH)��������� �I��� �GH
$
���� �I��� ����� ��8�!�� �'������ �GH
)� �'���GJ2� �2���)�'�)/��KH)��������� �I��� �GHL
Chapter 2- Introduction to Hibernate
���� ����'���
() ������'���������������������� ���� ��' ���������#@>������������ ���� ��)
/D����� ����������� ������3��
,)����'���������������������� ����'��������� ��� �� �'����������
/D�� �'������ �������
0)��������"����'������������ !������������� ��� ������ ������ ����������������������������
/D������� ����'��������������������
4) ���'������������ !�/���� �� '�������+� � !'�����������'����� ������
/L�@������� �� ���@���� ���������'�M��� )
Chapter 2- Introduction to Hibernate
������� �����'��
() ���������'�������������������������� ���,) ����!�����������/������� ������������ �)0) C��� ��������������� ��� ����������)4) C ��������'���� �������������� ���������� ���� ���������������)N) �������� ����� '���������� �� ����������� �� ����� ��� '�� ���� �&��������������� '�! ")
O) ������������� ���� ������������� ����������������� � ���� �������� �)
Chapter 2- Introduction to Hibernate
������� �I��� ����'��
() ���� ����� �������' �������� �I��� �,) ��������� �������������� ����������������� �������)0) ���������� ��' ����!� ����������� �)4) �'�� ����������� ��������������������������������������� � �+������ ����������' ������������)
N) ������� �I��� ��������������3 �������������� �������������������������������������������)�������� �� ��������������������������������� ������� '�! "��������������������'��������� '�! "�1 �����'������������ ����� ���������������'�������������� ��%������������������)
Chapter 2- Introduction to Hibernate
���� �'������ �����'��
���� �'������ �� �&����������� �� �'����������)��
������������ ��������� �'������ ����������� �����'������ ���� �� '���������� �����������������%����'���� ������������������������ �I��� �)
����������� �����'��
����������� �����'��������� ��� ����.��������� ����F>A���������� �� �F�.�����"���������� �������!����''���"����� '�/���� ����� ����������� ������)
Chapter 2- Introduction to Hibernate
3����������������'���
���3������'������ !��� ��� ��' ������������������������������� �� ��� !�����������/����)
3����!���������3 � ��������������3 �������� '�� ���������.�3������������������� �������������������������������� '���������������������������'�������� �/����������)
������������'����������������������� !��� ��� ���������/���� �&��% ���������������)
.�3�������������������!������������+�������� ������������� ��������������)
Chapter 2- Introduction to Hibernate
����
.������������ �&����������&��������� ������������ ��������������������������������� ��������������������� '�������������������' ������&�"������)�G��*)�������*���H
����������� �����%�'�������� ������)��� ����@���� ����2� �� ���@���� ���'���)
Chapter 3- Creating Persistent Classes
���������������F����������)
�������!���� �����������������������'������������������ ���������' �� ���������������� �������������'����)�����������!������� ����'����)
.�����������������!��������������� ����������������� �����������2������� ��)
���������������� �� ����� �������)�������������������������' ��&������ ����������������'�������������������� ����� �������� �� �� ������������� ������������������������������� ����� '�� �� ���� �.������������ ����� ����������>.������������ �����������'������ ������ ���'�����'����������������� �������������������������� ���������� ��
Chapter 3- Creating Persistent Classes
����������A�����F���������
�������������A "P��������� ��������� ������L��������������������� �����L������'� ���� ��L22M������������������� ���' ������ ��'����QQQR
Chapter 3- Creating Persistent Classes6=>$��?�5�������%���������@A ��
B%22������2���������������>�>�0):225CB
B����D22������)� ��' �)��2������%�������%0):)���B<
6������%�������<
6���������8B� �)���)A "B�����8JA$$��B<
6������8B��B����8J���K ������%����8J:K<
6���� ������8J��� B2<
62��<
6� �������8J����B�2<
6� �������8B���� B�2<
6� �������8J����K � �%����8J��K2<
6� �������8J����K ���8J����K � ����8J���� ���K2<
6� �������8J� ������K2<
6� �������8J� ��K<
6� ��������8J� ��K ���%���8JC@�5#��G(,�,HK2<
62� ���<�������62�����<62������%�������<
Chapter 3- Creating Persistent Classes
() ������%�����1 ��'�� !������������� ������������� �&��)
��������'���������� '� ��A "������������������������ ����������� '������' ����!� �&�����������������' ���� �&���������' ���� ��)� ������� ��������������� '���������'������� ��������� �������� ����'���������������%�������������� �������!������������� ������������� �&��)���'�����'���������� '���������'���������� ����������� �����+��������� ������ ����������'����������������������%������������)
6���� <�����
���������������� ��������!� !�� �������' ���&���� �&������������������������������������'����������� �����!�������������������� ��� ��������������'�������������� �&��)
Chapter 3- Creating Persistent Classes() A����%������� �
�������1 ����' ���������� �������������������������������>������������������� �����/���������G�'�� ���������������������� �������������� ����+����'������� ����������� ������������ �������� ��)
��������1 �'����������������������� �������� ������!�������G��,���������%��������������� ����������� ������������ �������� ��H
������1 �'�������������������� �����G��,��� ����� ������.������� ����������� ������������ �������� ��H
Chapter 3- Creating Persistent Classes() A����%������� �
��� �1�������������>��' ������������������ ������������)� ��������� ����������������� ����� ����������� �����������������)
����'�������������������%�����%"��������������� ���������/�%����)��? ������ ������ �� !�����������)
���������������%�����%"���/�%��������
�������� �������%�����%"�������G(::H
I ����� �����
6���� ������8J��� K<
6���� ���8J����K<��� 62����<
6���� ���8J� ����K<�/�62����<
6���� ���8J��/�� K<N::62����<
62���� <
Chapter 3- Creating Persistent Classes
() A����%������� �
�����
�������� ���������������������� ���� �)G��,�� ���� �������H
@���)�/
������������������������ ��������������' �� !���������� ��������������������������������� '���������FS������������������������ ��������)
@���)�����
������� ������"�@���)�/�������������������������(O���������� ��)
C�����
���"����������������� ���� ���������� ������������)
Chapter 3- Creating Persistent Classes� ������������� ��������������������������������������� �
�������������������@���P
�������������'��������� �I��� ����� �I��� �L
�������P
���P
22������������ �I��� �' ��������)�'�)/��
���� �I��� �8��!�� �'������ �GH)� �'���GH)��������� �I��� �GHL
R�������G�� !��� /H�P
22���"����� ��� �����/���� ������������������!��� !�
�����))������GB����������� �I��� ����� ��'����)B�T�/HL
�� !��!�5/���� �����������*5 G/HL
R
R
Q))
Chapter 3- Creating Persistent Classes� ������������� ��������������������������������������� �
��������������'��������� ������� �8�!����� ���GHL
������������������ ���������� �GH��� !��������5/���� �
P
���� ���8G���� �H���� �)��GHL
�'G�88����HP
�8���� �I��� �) ������ �GHL
���� �)��G�HL
R
�����L
R
��������������� ����� ����� �GH�� !� ������5/���� �
P
���� ���8G���� �H���� �)��GHL
���� �)��G����HL
�'G�=8����H
P
�)�� �GHL
R
R
R
Chapter 3- Creating Persistent Classes
�����������������P
2UU
U�V���� ���
U2
��������������� �������G�����WX����H�P
22��$>$�.�� %��������� ������
���� ������ � 8�������@���)�������� �GHL
A "�� "8�!�A "GHL
� ")������GQ)
Q
���� �)���G� "HL
���� �)'����GHL
���� �)�� �GHL
R
Chapter 3- Creating Persistent Classes
����������������!����A�����>���
�����))�P
������A� ������L
Q
R
�������
6� ���Q
6� ��������8J����K ���%���8J�� �K2<
Chapter 3- Creating Persistent Classes
���������������*��� �����
���� ���������� ����������&���� �&����������'�������������������������������!�������� ���������������)��.����������������������"����������� '���������*��� �����'������������� �&����������)
������� ���P
����������������L
Q
R
�������
6� �������8J�����K ���8J�����*���K<
6� ��������8J����K ���%���8J�� �K2<
62� ���<
Chapter 3- Creating Persistent Classes
@���
� ����� �8G� ���H���� �)� ��G� ���)������(H
��� �8� �)�������GH)���S���GHL
Chapter 3- Creating Persistent Classes
��������!����>��2�������.�������
������.�� ���P
))
������>�����������L
R
�������
6� ���Q
6� ��������8J����K ���%���8J>��K2<
Q
@���
.�� �����8G.�� ���H���� �)� ��G.�� ���)������(HL
�)���������GHL
Chapter 3- Creating Persistent Classes
����������#��%$���������
6���������8J�KQ)��������8J'���K<
Q)
62�����<
@��������� �)���G�H�1 ��� !�� ��� �������������������"� !����� �&���� ����+����������)
Chapter 3- Creating Persistent Classes
���������������������S�� ����2���������
������ �� ������������������������������' ���������'�������! ����������������' ��������������������� �� ������������������)������������������������� ��������!������ �&����������������������������� ��� �+�������������'��' ���������)
������S� ���
P
��� ��L
���������L
��� ��� �L
Q
R
Chapter 3- Creating Persistent Classes
�������
Q
6��� �����8J��� �K � ����8J��� �+ ���8Y����K
������%����8J���'���K2<
))
���
����������� ��� P
������ � ���������� %�������������"����� � ������� �/�HL
@���
))
S� ��� � �8�!�S� ���GHL
� �)��C��GJ��KHL���� �)���G� �HL���� �)'����GHL
��� ��G� �)��S�� �GHHL
Chapter 3- Creating Persistent Classes
��������
��� ��&���)���)UL
������� ����P
Q
�����������������L
R
�������
6������������8J��������K � ����8J�����K ������%����8J���'���K2<
���
���������� ���G�))���������������QHL
Chapter 3- Creating Persistent Classes
�����������������!����F����������)
����'���'������#����������������6������%�����������"��8B� �)���B<
6���������8B�>B�����8B�>B����������� %����8B��B<
6������8B��B�� ����8B�>B�������%����8B:B<
6���� ������8B��� B2<
62��<
6��������� � ����8B��E���B����8B�����B2<
6� �������8B����B�� ����8B��� 5B2<
)))
6������������8B������5���� ��>B����������� %����8B������5���� ��>B<
6� �������8B�!'����B����8B�����B2<
62��������<
6������������8B������� ����>B����������� %����8B������� ����>B<
6� �������8B� ����B����8B�����B2<
62��������<
62�����<
62������%�������<
Chapter 3- Creating Persistent Classes
�����������������!����F����������)
���'���������� ����������������������6��������� <�����)��
����������������������������������>� �&������+�������� ���������������>�!���������� ��������������' ������>�������������� ������������� �����)
���������������������������������������5���� ��> �&���������������� ���������>�������������>� �&��)��.����� ���������������������' �����������5���� ��>)��
InternationalCDindianullRock4555
SpecialEditionCDnullblastRock344
CdnullnullRock4343
Cd_typecountrynewfeaturesTitleid
Chapter 3- Creating Persistent Classes
@���
�>���8Q
������5���� ��> ���8Q
Q
���� �)���G��HL
���� �)���G���HL
���� �)���G���HL
���� �)'����GHL
���� �)�� �GHL
Chapter 3- Creating Persistent Classes
����'���'(�������������������������6=>$��?�5�������%���������@A ��
B%22������2���������������>�>�0):225CBB����D22������)� ��' �)��2������%�������%0):)���B<
6������%�����������"��8B� �)���B<
6���������8B�>B�����8B�>B����������� %����8B��B<6������8B��B�� ����8B�>B�������%����8B:B<6���� ������8B��� B2<
62��<6� �������8B����B�� ����8B��� 5B2<)))6& ���%������������8B������5���� ��>B�����8B���B<6"��� ����8B��B2<6� �������8B�!'����B����8B�����B2<62��������<6& ���%������������8B������� ����>B�����8B���B<6"��� ����8B��B2<6� �������8B� ����B����8B�����B2<62��������<62�����<
62������%�������<
Chapter 3- Creating Persistent Classes
���������� ��������������������
C��� ��������������!������������ ��������)
C��� �����6�����))<������' ���������2����)
Chapter 3- Creating Persistent Classes
) ������*����� �����+ ������
6�����))
6� �������8J'������K ' ����8J� ����G'�������+ Y��������HK2<
62�����<
Chapter 3- Working with Collections
) ������*����� ������ ��
����' #������,�( ������� ' ������,�
(���' #���(��,�( ����(�� ' ���(��,�-����' %����-���,�
���������� &������������� �� ����������������������.
.�� ����� ��
5/D import java.io.Serializable;import java.util.Set;public class Employee {
private String name;….private List addresses;public Integer getAge() {
return age;}public void setAge(Integer age) {….public List getAddresses() {
return addresses;}public void setAddresses(List addresses) {
this.addresses = addresses;}
Chapter 3- Working with Collections
����/�0�������
�&������ ������ ����������� ���������/,�-���,������,� ��� ��,�� ��������1����� ������������������� �� �����������/�� ���� �� &���������������������� ������ �.
0/2�3����/�� ����45� ����6 ����45����6 ������45������678
�������� &����,�� �9���� ���� �� ���������� ����������������������/�� �����1���������������.
3����/'����'� '������ ����45� ����6 �����45�����678
�&�� �������� �������� �� ���������/�& ��� ���� ������ �,�� �����������&���������������3� �� ����'����/8
3� �� ����'����/������45�����68
3���'�� ����������45����6 ����45����6 � ����45� ����678
37� �� ����'����/8
���"�%� ���������� �����'��������������������� '����� �� �������/)
Chapter 3- Working with Collections5�����5�����
.������������ '���� ����� ������������������� ��������/������ ���� ����� �)��
�'�� ��� ����� �� ����� ��������������� ��������6����<
6������ ����8J� ����K ���8J���K2<
���� ��������������������������������������)
�'�� ��� ����� ��� ������� �&������ ��������6����%� %����<������� �����'���� �� �&���)
6����%� %������ ����8J� ����K �����8J�����K ��%& ��8J���Z'����Z��� K2<
? ������������������ �� ����������&����� ����� ����������
6� �� ���%����������8J�����K<
6� �������8JK2<
62� �� ���%����<
Chapter 3- Working with Collections
A�%����� ����.�� ����� ��
�*����������*��� �
���������������� '��' �����!���� ������������ ����� '����������� ����� �)���'����� ����� �����������)
6���������8JM ��K � /�8JM ��K)
6�����*�8J��+<62��<
62�����<
���������������������������������������������������)��
Chapter 3- Working with Collections
�������������2�� ������
���������� �� ���
P
��������� ��L����� ���������L
����������� ����L
R
�������
6���������8J���� �� ���K ����8J���� �� ���K<
6������8J��K<
6���� ������8J�����K2<
62��<
6�������8J� ����K<
6"��� ����8J���E��+2<
6���/�� ����8J� ���E���K ���8J�����K2<
6������ ����8J� ���E����K ���8J�����K2<
62���<
Chapter 3- Working with Collections�3
������������� �� ���GQ
���������� �����G
��������� ���E��� �/���� ���E���� �/�HL
������������������������ �� ��� �&��������� ��������� '���� �&���������� !�����3 � ���� ����������������������"��' �������� �� �������)��������� ���������������������"����������������� ���!�������"�2������ '����� ������������ ����� ���������)
Chapter 3- Working with Collections@���
���� �� �����8�!����� �� ���GHL
��)��C��GJ�����KHL
��������8�!��������GHL
�)���GJ� � K�K���KHL
�)���GJ��'K�K���KHL
�)��� ����G�HL
Q))
�����,8��,)��� ����GHL
��� ��G�,)��GJ� � KHHL
Chapter 3- Working with Collections
�����������$�&�������D6����%� %����<�����
Chapter 4- Using Persistent Objects
(�1������� ��������� ���������� �
() ���� �)���G� "H
- �������������� ����������
������� �&���� ��G����� ������������*��� ��� �"� � � �H
������� �&���� ��G����� ������������*��� ��H
�������� ���� ��G����� ������������*��� ��H
A "�� "8GA "H���� �)� ��G@�)������:H
$����� �)��G��)������:HL�%% !����� ���� !����/���� ���'�� ��� ��� /���)
+�������������
>��������/���� �� '����'����������������3 ����������� /����)���������/�����'�������� ����������!������ ����GH���� ������������� !���������������'���GH���������GH���� ��)
Chapter 4- Using Persistent Objects
��������� ������
���� �)���G� "H
��&���������������
���� �)'��G� "HL
:���������������
���$@����G� "H�1 �������� ���������!�������� ����� �&����� ���������������' ����'�������� �������)
@����G� "H�1 ��������������!������� �&������������� ������������������������������ ������3 ���������� ���"����������������������������)
@����G� "�:H�%
I����GH�1 ���'�������� ������������!������������������������ ���� �&�����'�� ���������������������)
Chapter 4- Using Persistent Objects
+�������������
������� ����'���G��������H
������� ����'���G���������$�&�� �������������H
������� ����'���G����������$�&��WX�����������WX�����H
����� "�8���� �)'���GJ' � A "KHL
���� �)'���GJ' � @��!��������JT������TKKHL
���� �)'���GJ' � @��!��������87K�������������)�����H
���� �)'���GJ' � @��!��������87�$����G�����HK��!
$�&��WXP�����������R��!���WXP������)��#�CM�������)��#�CMR
Chapter 4- Using Persistent Objects
+�������-���������������
������� ��������G��������H
������� ��������G���������$�&�� �������������H
������� ��������G����������$�&��WX�����������WX�����H
���� ��8���� �)����GJ' � @���� �������KHL
����G��)���C/�GHH
P
@����8G@�H��)�/�GHL
Q))
R
Chapter 4- Using Persistent Objects
+���������� ������ ��.
A "�� "8���� �)� ��GA ")������:H
� ����� ������ �8���� �)'���G� ")��.��� �GH�K!� ����)���� ��� ��"�Y�[+ KHL
� ����� ������ �8���� �)'���G� ")��.��� �GH�K���� ����)���� ��� !������)����
��"�Y�[+ KH
��!+�������� ����������� ���� ������� '�������� �&��)
Chapter 4- Using Persistent Objects
(������;�������
� �'��!������������ �&��� ������ '� �&������������������� ������ ��� �����
�������������������� �� ���� '���������)
��������8���� �)'���GJ���� � ")������*G� ")���� �H�' ��� "HL
' G��� �8:L�6����)��*GHL�TTH
P
$�&���WX� !8����)��G�HL
���������8G�����H !W:XL
Q)
R
��������� ����������������� �&�����
��������8���� �)'���GJ���� � "��� ")������*G� ")���� �H�' ��� "HL
))
A "�� "8GA "H !W:XL
Chapter 4- Using Persistent Objects
;�����������"�����;������
@��������3��� �&��
.�� !��� �� �� �� ����������
3������8���� �)���3��GJ' � A "�� "!��� ")��� <(::KH
�'G���)�����#����GHH
P
Q))
R
Chapter 4- Using Persistent Objects
"�����;������
3<���������45&���%--68
3=>��% %>�&� ��? ��� �@@8
37<����8
�������������������������������&���
;�����<����4����� �.���"����;����A5&���%--6BC
;����.�����/�������ADEBC
;����.���+����������AEBC
-����� ��4<����.����AB
..
Chapter 4- Using Persistent Objects
"�����;�������*����"���������������
3<���������45&���%--68
3=>��% %>�&� ��? ��� �*������ �.����� 8��21�����@@8
37<����8
�������������������������������&���
;�����<����4����� �.���"����;����A5&���%--6BC
;����.�����/�������ADEBC
;����.���+����������AEBC
;����.������A51����6,FFB
-����� ��4<����.����AB
..
Chapter 4- Using Persistent Objects
���������;����
�����������������4����� �.��������������A? �.�����B
��������.���A0/������ �.�<A5����6,6����6BC
��������.���� �/�������AEBC
-����� �4��������.����ABC