Hibernate tutorial for beginner

Hibernate

Hibernate Theory

Object < ---- Java DB Conect (JDBC) ------> persistence
we want save object into db without SQL => save object into database directly

ORM

object relateional mapping

  • one class is one table, one object is one row

Save object to db

we want to use

  • s.save(obj)
  • get()

to achieve, we need

  • session
  • session factory (XML or Java config)

Annotation

@Table(name="name") specific table name
@Column(name="name") specific column name
@Transient data not being stored
@Embeddable save the files in the used table rather then create a new talbe

Fetching Data

name = session.get(worker.class, 102)

Mapping Relations Theory

One To Many

Laptop Student relationship

public class Laptop {
    private id
    @ManyToOne
    private Student std 
}

public class Student {
    @OneToMany(mappedBy="") // one student has many laptops
    private List<Laptop> laptops
}

If there is no (mappedBy="") there additional table will be created
There are three tables
Laptop table
Student table
Student_Laptop table

If there is @OneToMany(mappedBy="") and @ManyToOne then ManyToOne table will create addtion files and no additional table is created

Many to Many

It required two mappedby annotations in order to NOT create additional table

@ManyToMany(mappedBy="")
@ManyToMany(mappedBy="")

Fetch EAGER LAZY

By default it is lazy: it is not fetch other talbe to get the value unless is needed

Hibernate caching

Client <- -> server <- -> DB

  1. fetch data from db
  2. data store in the cache (First Level Cache)
    same query same session :: First Level Cahce
    session share data in Second Level Cache

ehcache
hibernate-ehcache

@Cacheable
@Cache(usage=CacheConcurrencyStrategy)

Hiberate Object State

  1. new() => Transient state
  2. save() persist() get() find() => Persistent state
  3. detach() => Detached state
  4. remove() => Removed state

Example

laptop l = new Laptoo()
l.setlid(52)
l.setPrice(800)

session.save(l)
l.setPrice(750)

session.getTransaction().commit()

The price 750 will be persisted in db as session is still in transaction state

Get VS Load

session.get(Laptop.class, 6) => give you the object
session.load(Laptop.class, 6) => give the proxy object (lazy loading)

JPA

Java Persistent API : a common standard
Switch ORM like from Hivernate to IBatis

EntityManagerFactory emf = Persistence.createEntityManagerFactory();
EntityManager em = emf.createEntityManager();
Alilen a = em.find(Alien.class, 4)

When need to persist, remember transaction().begin() and transaction().commit()

EntityManagerFactory emf = Persistence.createEntityManagerFactory();
EntityManager em = emf.createEntityManager();
Alilen a = new Alien()
a.setAid(9)
a.setAName("Software")
em.getTransaction().begin()
em.persist(a)
em.getTransaction().commit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值