Package creation in Oracle is a TWO step Process.
1.Declaration of Package
2.Definition of Package
Declaration of Package
CREATE OR REPLACE PACKAGE TEST IS
PROCEDURE JUNITPROC(C OUT SYS_REFCURSOR);
END TEST;
Definition of Package
CREATE OR REPLACE PACKAGE BODY TEST IS
PROCEDURE JUNITPROC(C OUT SYS_REFCURSOR) IS
BEGIN
OPEN C FOR
SELECT 78985450.1245487986418648 DECIMAL_VAL FROM DUAL;
END JUNITPROC;
END TEST;
There are two types of objects in Hibernate
1. Value Object
2. Entities
Value Objects are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone. Entity Objects are those who can stand alone like College and Student.
So in case of value objects preferred way is to Embed them into an entity object. In the example below Address class does not derive the meaning of its own. So Address is Value Object and is Embeddable. Whereas UserDetails(Entities) could stand on its own and it could have Embedded Objects
Address.java
import javax.persistence.Embeddable;
@Embeddable
public class Address {
private String Street;
private String Location;
private String City;
public String getStreet() {
return Street;
}
public void setStreet(String street) {
Street = street;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
}
UserDetails.java
public class UserDetails
{
@Embedded
private Address address;
.
.
.
}
The above code explains how the Embedded and Embeddable annotations can be used in the code.
CreateUser.java
Address objAddress = new Address();
objUserDetail.setAddress(objAddress);
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
By using @GeneratedValue annotation we can create primary key in table.The annotations takes 4 parameter as strategy attribute.
@GeneratedValue(strategy=GenerationType.AUTO)
Auto tells the Hibernate to do the primary key addition from hibernate side
@GeneratedValue(strategy=GenerationType.IDENTITY)
IDENTITY tells the Hibernate to do the primary key addition from DB side when set to auto increment.
@GeneratedValue(strategy=GenerationType.SEQUENCE)
SEQUENCE tells the Hibernate to do the primary key addition as defined by sequence in DB.
@GeneratedValue(strategy=GenerationType.TABLE)
TABLE tells the Hibernate to create a seperate table in DB and maintain the last inserted values.
The table contains only one row with last inserted value.
In Some cases the hibernate.cfg.xml might become unrecognized.In such case the code should be changed to force the config to be picked from the file location.
public static SessionFactory createSessionFactory()
{
Configuration configuration = new Configuration().configure();
configuration.configure("hibernate.cfg.xml");
configuration.addAnnotatedClass(com.mugil.tutor.UserDetails.class);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
Hibernate uses SessionFactory pattern internally as below
1.Create Object for SessionFactory
2.Open Session to begin Transaction
3.Begin Transaction using beginTransaction() Method
4.Save the Object by Passing Object of the bean
5.Complete the Transaction using commit
Annotations
@Entity – Means entity as a whole>table would be created by the Name of the Entity
@Id – Tells the Primary Key
Having a Different table name from Class Name Annotations
@Entity(name="User_Details")
public class Users
{
.
.
}
Table with User_Details would be created instead of Users
Having a Different Column name from Object Name Annotations
@Entity(name="User_Details")
public class Users
{
@Id
@Column(name="USER_ID")
private String UserId;
.
.
}
Columns with User_Id would be created instead of UserId
Appending String to Getters
public void setName(String name)
{
Name = name + " Append Test ";
}
Appending String to Getters
@Entity
@Table (name="User_Details")
public class Users
{
}
The Entity Name Still remains the same but the table Name is different.
@Basic Annotation – Tells Hibernate to persist which it does by default
public class Users
{
@Basic
private String UserName;
.
.
}
@Basic has 2 Parameters – Fetch, optional. The only time you use @Basic is while applying the above options.
@Transient Annotation – Tells Hibernate to not store data in database
public class Users
{
@Transient
private String UserName;
.
.
}
@Temporal Annotation – Tells Hibernate to specify Date or Time
public class Users
{
@Temporal (TemporalType.Date)
private String joinedDate;
.
.
}
Without @Temporal the joinedDate is store along with TimeStamp in DB. Now using TemporalType(which is ENUM) you can select the type of data which can be stored in Database.
@Lob – Tells Hibernate to specify Date or Time
public class Users
{
@Lob
private String Address;
.
.
}
Tells the database field should be created as CLOB instead of VARCHAR(255).
When the control reaches
Line 6 The Size and Modcount of the arrNames would be 4
Line 7 The Size and Modcount would be 5 and size would be 3
Line 7 The Size and Modcount would be 6 and size would be 4