10. /20@yegor256 10
@Entity
@Table(name = "point")
public class Point {
private int id;
@Id
@GeneratedValue
public int getId() { return this.id; }
@Column(name = "x")
public int getX() { return this.x; }
public void setX(int x) { this.x = x; }
@Column(name = "y")
public int getY() { return this.y; }
public void setY(int y) { this.y = y; }
}
11. /20@yegor256 11
void static moveTo(int id, int dx, int dy) {
Session session = factory.openSession();
try {
Transaction txn = session.beginTransaction();
Query query = session.createQuery(“SELECT p FROM point WHERE id=:id”);
query.setParameter(“:id”, id);
Point p = query.list().get(0);
p.setX(p.getX() + dx);
p.setY(p.getY() + dy);
session.update(p);
txn.commit();
} catch (HibernateException ex) {
txn.rollback();
} finally {
session.close();
}
}
12. /20@yegor256 12
PostgreSQL
JDBC
UPDATE point
SET x = “100”, y = “120”
WHERE id = 123
p.getX();
p.getY();
statement.executeUpdate();setX()
Query query = session.createQuery(“SELECT p FROM point WHERE id=:id”);
query.setParameter(“:id”, id);
Point p = query.list().get(0);
p.setX(p.getX() + dx);
p.setY(p.getY() + dy);
session.update(p);
update()Point
Session
setY()
16. /20@yegor256 16
PostgreSQL
JDBC
UPDATE point
SET x = “100”, y = “120”
WHERE id = 123
statement.executeUpdate();
Point p = new Point(123, db);
p.moveTo(50, 70);
moveTo()
Point
x.update(“point”)
.set(“x”, this.x)
.set(“y”, this.y)
.where(“id”, this.id)
.execute();
jOOQ
17. /20@yegor256 17
class Point {
private final DB db;
private final int id;
public void moveTo(int dx, int dy) {
this.db.update(“point”)
.set(“x”, ??)
.set(“y”, ??)
.where(“id”, this.id)
.execute();
}
}
21. /20@yegor256 21
Point p = new Point(new Cached(mysql));
p.draw(canvas1);
p.draw(canvas2);
cache
22. /20@yegor256 22
class Point extends ActiveRecord {
protected int x;
protected int y;
void moveTo(int dx, int dy) {
this.x += dx;
this.y += dy;
this.update(); // from parent class
}
}
ActiveRecord
23. /20@yegor256 23
class Point {
void moveUp(int dy) {
// UPDATE point SET y = ?
}
void moveRight(int dx) {
// UPDATE point SET x = ?
}
void moveTo(int dx, int dy) {
// UPDATE point SET x = ?, y = ?
}
}
updates
24. /20@yegor256 24
db = new TransactionAwareDB(db);
db.start();
try {
Point p1 = new Point(1, db);
Point p2 = new Point(2, db);
p1.moveTo(15, 30);
p2.moveTo(7, 13);
db.commit();
} catch (Exception ex) {
db.rollback();
}
transactions