hibernate笔记,结束第九章

conversation implementation with detached object state有时候需要考虑重写equals(), 这时候要考虑implementing equality with a business key

The save() operation also returns the database identifier of the persistent instance.

 

get和load的区别:

If no row with the given identifier value exists in the
database,  get()  returns  null.  The  load()  method  throws  an  ObjectNotFoundException.

The  get() method on the other hand never returns a proxy, it always hits the database.

 

you  don’t  have  to  reattach  (with  update()  or  lock())  a  detached
instance to delete it from the database.

 

item.getId(); // The database identity is "1234"
item.setDescription(...);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item2 = (Item) session.get(Item.class, new Long(1234));
session.update(item); // Throws exception!
tx.commit();
session.close();

 

A persistent instance with the same database identifier is already associated with the Session!

 

merge的时候,如果persistence context有当前的item, update它,如果没有从db里找,要是找到了,update它,要是找不到,create一条记录。

你可能感兴趣的:(Hibernate,职场,笔记,休闲,结束)