ORM is an Offensive Anti-Pattern

/20@yegor256 1
ORM is an Offensive

Anti-Pattern
Yegor Bugayenko
/20@yegor256 2
what’s wrong

with data?
/20@yegor256 3
ANSI C
typedef struct {
int x;
int y;
} Point;
void moveTo(Point p, int dx, int dy) {
p.x += dx;
if (p.x > 640) { p.x = 640; }
p.y += dy;
if (p.y > 480) { p.y = 480; }
}
void draw(Point p, Canvas c) {
c.put(p.x, p.y, “black”);
}
/20@yegor256 4
typedef struct {
int x;
int y;
int color; // here!
int scale; // here!
} Point;
void moveTo(Point p, int dx, int dy) {
p.x += dx;
if (p.x > 640) { p.x = 640; }
p.y += dy;
if (p.y > 480) { p.y = 480; }
}
void draw(Point p, Canvas c) {
c.put(p.x, p.y, “black”);
}
/20@yegor256 5
maintainability
command & control trust & delegatevs
/20@yegor256 6
class Point {
private int x;
private int y;
void moveTo(int dx, int dy) {
this.x += dx;
if (this.x > 640) { this.x = 640; }
this.y += dy;
if (this.y > 480) { this.y = 480; }
}
void draw(Canvas c) {
c.put(this.x, this.y, “black”);
}
}
/20@yegor256 7
encapsulation
/20@yegor256 8
Java
class Point {
private int x;
private int y;
public int getX() { return this.x; }
public int getX() { return this.y; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
class PointUtils {
static void moveTo(Point p, int dx, int dy) {
p.setX(p.getX() + dx);
if (p.getX() > 640) { p.setX(640); }
p.setY(p.getY() + dy);
if (p.getY() > 480) { p.setY(480); }
}
static void draw(Point p, Canvas c) {
c.put(p.getX(), p.getY(), “black”);
}
}
/20@yegor256 9
ORM/JPA/Hibernate
/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; }
}
/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();
}
}
/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()
/20@yegor256 13
JDBC
Point
Session
client
/20@yegor256 14
what is the
alternative?
/20@yegor256 15
JDBC
Point
adapter
client
/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
/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();
}
}
/20@yegor256 18
no mapping!
/20@yegor256 19
jOOQ
jcabi-jdbc
JDBC JDBI
DbUtils
Yank
/20@yegor256 20
Volume 2
Section 6.5
/20@yegor256 21
Point p = new Point(new Cached(mysql));
p.draw(canvas1);
p.draw(canvas2);
cache
/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
/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
/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
1 of 24

Recommended

662305 10 by
662305 10662305 10
662305 10Nitigan Nakjuatong
305 views6 slides
Basic program in java by
Basic program in java Basic program in java
Basic program in java Sudeep Singh
390 views14 slides
Oops in c++ by
Oops in c++Oops in c++
Oops in c++DravidSh
49 views5 slides
Cpp by
Cpp Cpp
Cpp Ankit Dubey
44 views6 slides
Ques 8 by
Ques 8Ques 8
Ques 8RushilPatel44
18 views3 slides
Computer graphics programs in c++ by
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++Ankit Kumar
803 views10 slides

More Related Content

What's hot

Es84 by
Es84Es84
Es84Imee Kassandra Cacho II
38 views4 slides
Artificial intelligence by
Artificial intelligenceArtificial intelligence
Artificial intelligenceAditya Sharma
294 views9 slides
Pratik Bakane C++ by
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++pratikbakane
5.7K views42 slides
analog clock C# by
analog clock C#analog clock C#
analog clock C#omeed
768 views8 slides
Stl algorithm-Basic types by
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic typesmohamed sikander
387 views16 slides

What's hot(19)

Pratik Bakane C++ by pratikbakane
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane5.7K views
analog clock C# by omeed
analog clock C#analog clock C#
analog clock C#
omeed768 views
Datastructures asignment by sreekanth3dce
Datastructures asignmentDatastructures asignment
Datastructures asignment
sreekanth3dce899 views
Understanding storage class using nm by mohamed sikander
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
mohamed sikander375 views
Code for program to draw a circle using mid point circle algorithm in c by Ossa2015
Code for program to draw a circle using mid point circle algorithm in cCode for program to draw a circle using mid point circle algorithm in c
Code for program to draw a circle using mid point circle algorithm in c
Ossa2015663 views
Programa expresiones regulares by Anel Sosa
Programa expresiones regularesPrograma expresiones regulares
Programa expresiones regulares
Anel Sosa91 views
DAA Lab File C Programs by Kandarp Tiwari
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari25.4K views
C++ Programming - 3rd Study by Chris Ohk
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
Chris Ohk1.7K views
Processing資料(5) 正弦波と極座標 by reona396
Processing資料(5) 正弦波と極座標Processing資料(5) 正弦波と極座標
Processing資料(5) 正弦波と極座標
reona3962.6K views
C++ Programming - 4th Study by Chris Ohk
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk1.6K views
Dvst by hahaa225
DvstDvst
Dvst
hahaa225374 views

Viewers also liked

ORM is a perfect anti-pattern by
ORM is a perfect anti-patternORM is a perfect anti-pattern
ORM is a perfect anti-patternYegor Bugayenko
2.3K views10 slides
Object Oriented Lies by
Object Oriented LiesObject Oriented Lies
Object Oriented LiesYegor Bugayenko
2.4K views13 slides
ORM is offensive by
ORM is offensiveORM is offensive
ORM is offensiveYegor Bugayenko
2.4K views11 slides
How Anemic Objects Kill OOP by
How Anemic Objects Kill OOPHow Anemic Objects Kill OOP
How Anemic Objects Kill OOPYegor Bugayenko
2K views16 slides
Built-in Fake Objects by
Built-in Fake ObjectsBuilt-in Fake Objects
Built-in Fake ObjectsYegor Bugayenko
1.1K views30 slides
TDC2016SP - JooQ: SQL orientado a objetos. by
TDC2016SP - JooQ: SQL orientado a objetos.TDC2016SP - JooQ: SQL orientado a objetos.
TDC2016SP - JooQ: SQL orientado a objetos.tdc-globalcode
686 views31 slides

Viewers also liked(17)

ORM is a perfect anti-pattern by Yegor Bugayenko
ORM is a perfect anti-patternORM is a perfect anti-pattern
ORM is a perfect anti-pattern
Yegor Bugayenko2.3K views
TDC2016SP - JooQ: SQL orientado a objetos. by tdc-globalcode
TDC2016SP - JooQ: SQL orientado a objetos.TDC2016SP - JooQ: SQL orientado a objetos.
TDC2016SP - JooQ: SQL orientado a objetos.
tdc-globalcode686 views
openEHR: NHS Code4Health RippleOSI and EtherCis by Ian McNicoll
openEHR: NHS Code4Health RippleOSI and EtherCisopenEHR: NHS Code4Health RippleOSI and EtherCis
openEHR: NHS Code4Health RippleOSI and EtherCis
Ian McNicoll781 views
jOOQ at Topconf 2013 by Lukas Eder
jOOQ at Topconf 2013jOOQ at Topconf 2013
jOOQ at Topconf 2013
Lukas Eder1.5K views
An Introduction to jOOQ by Steve Pember
An Introduction to jOOQAn Introduction to jOOQ
An Introduction to jOOQ
Steve Pember2.2K views
An introduction to Reactive applications, Reactive Streams, and options for t... by Steve Pember
An introduction to Reactive applications, Reactive Streams, and options for t...An introduction to Reactive applications, Reactive Streams, and options for t...
An introduction to Reactive applications, Reactive Streams, and options for t...
Steve Pember596 views
jooqってなんて読むの? から始めるO/RマッパーとSpringBootの世界 by Y Watanabe
jooqってなんて読むの? から始めるO/RマッパーとSpringBootの世界jooqってなんて読むの? から始めるO/RマッパーとSpringBootの世界
jooqってなんて読むの? から始めるO/RマッパーとSpringBootの世界
Y Watanabe6.8K views
Reactive Streams and the Wide World of Groovy by Steve Pember
Reactive Streams and the Wide World of GroovyReactive Streams and the Wide World of Groovy
Reactive Streams and the Wide World of Groovy
Steve Pember904 views
openEHR Technical Workshop Intro MIE 2016 by Ian McNicoll
openEHR Technical Workshop Intro MIE 2016openEHR Technical Workshop Intro MIE 2016
openEHR Technical Workshop Intro MIE 2016
Ian McNicoll534 views
Семинар: Закон 54-ФЗ об онлайн-кассах. Как подготовиться by MoySklad
Семинар: Закон 54-ФЗ об онлайн-кассах. Как подготовитьсяСеминар: Закон 54-ФЗ об онлайн-кассах. Как подготовиться
Семинар: Закон 54-ФЗ об онлайн-кассах. Как подготовиться
MoySklad159.7K views
Railway Oriented Programming by Scott Wlaschin
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
Scott Wlaschin639.6K views
MVC + ORM (with project implementation) by Prateek Chauhan
MVC + ORM (with project implementation)MVC + ORM (with project implementation)
MVC + ORM (with project implementation)
Prateek Chauhan2.8K views

Similar to ORM is an Offensive Anti-Pattern

Runge kutta C programme by
Runge kutta C programmeRunge kutta C programme
Runge kutta C programmeShah Keval
996 views2 slides
Cquestions by
Cquestions Cquestions
Cquestions mohamed sikander
446 views52 slides
C questions by
C questionsC questions
C questionsmohamed sikander
328 views30 slides
week-18x by
week-18xweek-18x
week-18xKITE www.kitecolleges.com
489 views9 slides
week-17x by
week-17xweek-17x
week-17xKITE www.kitecolleges.com
201 views9 slides
VTU Data Structures Lab Manual by
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab ManualNithin Kumar,VVCE, Mysuru
3.7K views32 slides

Similar to ORM is an Offensive Anti-Pattern(20)

Runge kutta C programme by Shah Keval
Runge kutta C programmeRunge kutta C programme
Runge kutta C programme
Shah Keval996 views
Create a java project that - Draw a circle with three random init.pdf by arihantmobileselepun
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
Best Bugs from Games: Fellow Programmers' Mistakes by Andrey Karpov
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
Andrey Karpov530 views
Cg my own programs by Amit Kapoor
Cg my own programsCg my own programs
Cg my own programs
Amit Kapoor3.9K views
Assignment on Numerical Method C Code by Syed Ahmed Zaki
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
Syed Ahmed Zaki7.6K views
Below is my code.How can i fix thisimport java.awt.;pu.pdf by aminaENT
Below is my code.How can i fix thisimport java.awt.;pu.pdfBelow is my code.How can i fix thisimport java.awt.;pu.pdf
Below is my code.How can i fix thisimport java.awt.;pu.pdf
aminaENT3 views
[3] 프로세싱과 아두이노 by Chiwon Song
[3] 프로세싱과 아두이노[3] 프로세싱과 아두이노
[3] 프로세싱과 아두이노
Chiwon Song525 views
Creating an Uber Clone - Part XVIII.pdf by ShaiAlmog1
Creating an Uber Clone - Part XVIII.pdfCreating an Uber Clone - Part XVIII.pdf
Creating an Uber Clone - Part XVIII.pdf
ShaiAlmog1268 views
Sparse Matrix and Polynomial by Aroosa Rajput
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
Aroosa Rajput4.8K views
Bindings: the zen of montage by Kris Kowal
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
Kris Kowal808 views

More from Yegor Bugayenko

Can Distributed Teams Deliver Quality? by
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?Yegor Bugayenko
269 views23 slides
Are You Sure You Are Not a Micromanager? by
Are You Sure You Are Not a Micromanager?Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?Yegor Bugayenko
245 views16 slides
On Requirements Management (Demotivate Them Right) by
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)Yegor Bugayenko
220 views16 slides
My Experience of 1000 Interviews by
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 InterviewsYegor Bugayenko
219 views20 slides
Are you sure you are not a micromanager? by
Are you sure you are not a micromanager?Are you sure you are not a micromanager?
Are you sure you are not a micromanager?Yegor Bugayenko
251 views15 slides
Quality Assurance vs. Testing by
Quality Assurance vs. TestingQuality Assurance vs. Testing
Quality Assurance vs. TestingYegor Bugayenko
660 views25 slides

More from Yegor Bugayenko(20)

Can Distributed Teams Deliver Quality? by Yegor Bugayenko
Can Distributed Teams Deliver Quality?Can Distributed Teams Deliver Quality?
Can Distributed Teams Deliver Quality?
Yegor Bugayenko269 views
Are You Sure You Are Not a Micromanager? by Yegor Bugayenko
Are You Sure You Are Not a Micromanager?Are You Sure You Are Not a Micromanager?
Are You Sure You Are Not a Micromanager?
Yegor Bugayenko245 views
On Requirements Management (Demotivate Them Right) by Yegor Bugayenko
On Requirements Management (Demotivate Them Right)On Requirements Management (Demotivate Them Right)
On Requirements Management (Demotivate Them Right)
Yegor Bugayenko220 views
My Experience of 1000 Interviews by Yegor Bugayenko
My Experience of 1000 InterviewsMy Experience of 1000 Interviews
My Experience of 1000 Interviews
Yegor Bugayenko219 views
Are you sure you are not a micromanager? by Yegor Bugayenko
Are you sure you are not a micromanager?Are you sure you are not a micromanager?
Are you sure you are not a micromanager?
Yegor Bugayenko251 views
Zold: a cryptocurrency without Blockchain by Yegor Bugayenko
Zold: a cryptocurrency without BlockchainZold: a cryptocurrency without Blockchain
Zold: a cryptocurrency without Blockchain
Yegor Bugayenko282 views
How to Cut Corners and Stay Cool by Yegor Bugayenko
How to Cut Corners and Stay CoolHow to Cut Corners and Stay Cool
How to Cut Corners and Stay Cool
Yegor Bugayenko318 views

Recently uploaded

nintendo_64.pptx by
nintendo_64.pptxnintendo_64.pptx
nintendo_64.pptxpaiga02016
6 views7 slides
Navigating container technology for enhanced security by Niklas Saari by
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas SaariMetosin Oy
14 views34 slides
What is API by
What is APIWhat is API
What is APIartembondar5
12 views15 slides
FOSSLight Community Day 2023-11-30 by
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30Shane Coughlan
6 views18 slides
360 graden fabriek by
360 graden fabriek360 graden fabriek
360 graden fabriekinfo33492
162 views25 slides
Benefits in Software Development by
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software DevelopmentJohn Valentino
5 views15 slides

Recently uploaded(20)

Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy14 views
FOSSLight Community Day 2023-11-30 by Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan6 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492162 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j17 views
predicting-m3-devopsconMunich-2023.pptx by Tier1 app
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptx
Tier1 app8 views
ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
Introduction to Git Source Control by John Valentino
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source Control
John Valentino7 views
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation by HCLSoftware
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook AutomationDRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
HCLSoftware6 views
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok16 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254558 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm15 views
predicting-m3-devopsconMunich-2023-v2.pptx by Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app11 views

ORM is an Offensive Anti-Pattern

  • 1. /20@yegor256 1 ORM is an Offensive
 Anti-Pattern Yegor Bugayenko
  • 3. /20@yegor256 3 ANSI C typedef struct { int x; int y; } Point; void moveTo(Point p, int dx, int dy) { p.x += dx; if (p.x > 640) { p.x = 640; } p.y += dy; if (p.y > 480) { p.y = 480; } } void draw(Point p, Canvas c) { c.put(p.x, p.y, “black”); }
  • 4. /20@yegor256 4 typedef struct { int x; int y; int color; // here! int scale; // here! } Point; void moveTo(Point p, int dx, int dy) { p.x += dx; if (p.x > 640) { p.x = 640; } p.y += dy; if (p.y > 480) { p.y = 480; } } void draw(Point p, Canvas c) { c.put(p.x, p.y, “black”); }
  • 5. /20@yegor256 5 maintainability command & control trust & delegatevs
  • 6. /20@yegor256 6 class Point { private int x; private int y; void moveTo(int dx, int dy) { this.x += dx; if (this.x > 640) { this.x = 640; } this.y += dy; if (this.y > 480) { this.y = 480; } } void draw(Canvas c) { c.put(this.x, this.y, “black”); } }
  • 8. /20@yegor256 8 Java class Point { private int x; private int y; public int getX() { return this.x; } public int getX() { return this.y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } } class PointUtils { static void moveTo(Point p, int dx, int dy) { p.setX(p.getX() + dx); if (p.getX() > 640) { p.setX(640); } p.setY(p.getY() + dy); if (p.getY() > 480) { p.setY(480); } } static void draw(Point p, Canvas c) { c.put(p.getX(), p.getY(), “black”); } }
  • 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()
  • 14. /20@yegor256 14 what is the alternative?
  • 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