Site Sponsors:
Hibernate: Flush -v- Commit 
Once again, the question has been asked - Must we only use transactions with Hibernate?

The answer is no!

Indeed, assuming we have run something like the following:

drop table "NAGY".ACCOUNTS;
create table "NAGY".ACCOUNTS
(
ACCOUNT_ID BIGINT not null primary key,
LEGALNAME VARCHAR(255)
);


After we reverse engineer, then we can "go .flush():"

        Date date = new Date();
long ZID = date.getTime();
SessionFactory fact = NewHibernateUtil.getSessionFactory();
Session session = fact.openSession();

// Create
Accounts ref = new Accounts();
ref.setAccountId(ZID);
ref.setLegalname("NAGY");
session.save(ref);
session.flush();
session.clear();

// Read Created
ref = (Accounts) session.load(Accounts.class, ZID);
System.out.println("Got: " + ref.getLegalname());

// Update
ref.setLegalname("Professor Nagy");
session.update(ref);
session.flush();

// Read Update
ref = (Accounts) session.load(Accounts.class, ZID);
System.out.println("Got: " + ref.getLegalname());

// Delete
ref = (Accounts) session.load(Accounts.class, ZID);
session.delete(ref);
session.flush();

session.close();


... or we can "go .commit()":

        // Create
Accounts ref = new Accounts();
ref.setAccountId(ZID);
ref.setLegalname("NAGY");
Transaction trans = session.beginTransaction();
session.save(ref);
trans.commit();
session.clear();


... We can even do a bit of both!

        Date date = new Date();
long ZID = date.getTime();
SessionFactory fact = NewHibernateUtil.getSessionFactory();
Session session = fact.openSession();

// Create - a 'la Transaction
Accounts ref = new Accounts();
ref.setAccountId(ZID);
ref.setLegalname("NAGY");
Transaction trans = session.beginTransaction();
session.save(ref);
trans.commit();
session.clear();

// Read Created
ref = (Accounts) session.load(Accounts.class, ZID);
System.out.println("Got: " + ref.getLegalname());

// Update
ref.setLegalname("Professor Nagy");
session.update(ref);
session.flush();

// Read Update
ref = (Accounts) session.load(Accounts.class, ZID);
System.out.println("Got: " + ref.getLegalname());

// Delete - a la Transaction
trans = session.beginTransaction();
ref = (Accounts) session.load(Accounts.class, ZID);
session.delete(ref);
trans.commit();

session.close();

Indeed, the standard says lots of things things like "The entity X will be entered into the database at or before transaction commit or as a result of the flush operation."

--Enjoy,

-Rn


[ view entry ] ( 2172 views )   |  permalink  |  related link

<<First <Back | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | Next> Last>>