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---------------------------------------------------------------------------------------------------------------------------
- Hibernate Tutorial
- Hibernate Tutorial
- Struts Hibernate Integration Tutorial
- Hibernate tutorial with Eclipse
- hibernate tutorial 1
- Hibernate @ManyToOne Unidirectional Tutorial
- Hibernate EhCache configuration tutorial
- Struts 2 + Spring + Hibernate integration Tutorial
- Spring, Hibernate, Maven, and Struts2 integration tutorial
- tutorial
- Tutorial
- Tutorial
- Hibernate Tutorial 11 - Configuring Collections and Adding Keys --Continued
- Hibernate Core Reference Manual学习笔记——Chapter 1. Tutorial
- Spring 4 and Hibernate 4 integration tutorial part 1 xml configuration
- C++ Tutorial
- codesmith tutorial
- J2EE Tutorial
- iOS多线程的初步研究(六)-- NSOperation
- 到底linux下的管道是什么?
- Android DOM 解析
- VISUAL C++ 数据库开发-ADO
- 为什么苹果以前封闭就失败了,现在的苹果还是封闭的,它却成功了?
- Hibernate Tutorial
- 【初学】Objective-C学习笔记-Objectiv-C
- Android能够获取到唯一的设备ID吗?
- Twitter创始人Jack Dorsey的每日必做和不做清单
- CURL 远程网络服务器日志传输失败的错误代码列表
- 【GUI编程之准备--2】wxWidgets编写HelloWorld
- 内存对齐总结
- UITableViewController
- 方兴未艾的计算广告学——微软亚洲研究院高级研究员 刘铁岩