Hibernate Tutorial

来源:互联网 发布:国内erp软件排名 编辑:程序博客网 时间:2024/06/05 14:52

What is Hibernate?                        

an ORM(object relation management) tool

Used in the data layer of applications

Implements JPA

 

What it resolves?

before it, the jdbc connection be used based on query tech which be quite complicate; after it, mapping member variables to columns and relatetionships; handling data datatypes.

managing changes to object state.

 

How to setup,

get the hibernate jar and related dependent file in the its lib folder.

How to save object without hibernate

JDBC Database configuration

The Model object

Service method to create the model object

database design

DAO method to save the object using SQL queries

How to save object in hibernate ways

JDBC Database configuration- hibernate configuration

The model object - Annotations

Service method to create the model object -use the Hibernate API per se

database design - Not needed

DAO method to save the object using SQL queries -Not needed

 

Let's kick off with hibernate.cfg.xml

set Hibernate.cfg.xml based on the hibernate provided example files.

connection drivers:connection.url:connection username:conection.passowrd

SQL dialect:               to be found in the hibernate jar class files.

cache.provider_class: 

Show_sql: printout to stdout

hdm2ddl.auto: value could be create or update

Then create a new DTO/Model object:

userId and userNmae in the class file should be defined as model in out case;

adding new entry to mapping of entity class in the hibernate.cfg.cml;

anotations and also XMl file could both indicate the DTO entity class to be handled by HIbernate

Anotations types: The anotations could be placed on top of  both the field value and the method(method return value)

 @Entity(this class needs to be persisted, on the top of class); @entity(name="user_details") which make the table name and class name different

 @Id(primary key for selected column)

 @Column(name="new column alias name")

 @Table(name="USER_DETAILS") different with @Entity

 @Basic() by default which decides the value type converting

 @Transient which asks hibernamte ignore this class fieldvalue

 @Temporal(TemporalType.Date) only keep the date value(no hour and mins) whihc is different from the data entry by default

 @Lob which indicate the length of the column is not a default 255 bytes value.

 @GeneratedValue and GeneratedValue(strategy=Type.value[by default is AUTO, the others are table, inentity and sequence]) which will autoly generate keys for us

  @Embeddable for inter model class/table which should not be created independently by Hibernate.

  @AttributeOverrides{

  @AttributeOverride(name="street",column=@Column(name="HOME_STREET_NAME"))

}   which is used for multi embeded model class/table

  @EmbededId for the column from embeded class happens to be the primary key.

  @ElementCollection which is used to be create (another table) or subtable to save the object.

Using the Hibernate API

1) create a session factory(SessionFactory sessionFactory =  new Configuration().configure().buildSessionFactory())

2) create a session from the session factory(Session session=sessionFactory.openSession())

3) use the session to save the object(session.beginTransaction(); session.save(DTO object);session.gettransaction().commit();session.close())

Retrieving Objects using session.get

user=null; user = (UserDetails)session.get(userdetail.class, 1(this is the primary Id value))

Primary Keys

natural keys and seroget keys(could be auto generated by Hibernate with anotation of @GeneratedValue );

 

Value Types and Embedding Objects, what if a modle class has the value of object or list, set and map....

Value objects is aimed to provide values for other model table/class with the help of @Embeddable anotations which should be placed on both the parent table/Class model and the embeded value objects as well

private Address homeAddress;

Private Address officeAddress;

Save Collections:

Hibernate support a set of collections interface, for instance, java.util.set.

private Set<Address> ListOfAddress=newHashSet();

@ElementCollection should be used here.

-------------------------------------------------------------------------------------Ended at Hibernate Tutorial 10---------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

原创粉丝点击