Successfully reported this slideshow.

ORM is offensive

1

Share

Loading in …3
×
1 of 11
1 of 11

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

ORM is offensive

  1. 1. /11@yegor256 1 ORM - это обидно Егор Бугаенко ORM is an offensive anti-pattern
  2. 2. /11@yegor256 2 @Entity @Table(name = "post") public class Post { private int id; private String title; @Id @GeneratedValue public int getId() { return this.id; } @Column(name = "title") public Title getTitle() { return this.title; } public void setTitle(String txt) { this.title = txt; } }
  3. 3. /11@yegor256 3 MySQL JDBC UPDATE user
 SET name = “Jeff”
 WHERE id = 123 u.getName(); statement.executeUpdate(); setName() this.name = “Jeff”; u.setName(“Jeff”); session.update(u); update() User Session
  4. 4. /11@yegor256 4 не объект
  5. 5. /11@yegor256 5 Session session = factory.openSession(); try { Transaction txn = session.beginTransaction(); User user = query.list().get(0); user.setName(/* the logic to test */); session.update(user); txn.commit(); } catch (HibernateException ex) { txn.rollback(); } finally { session.close(); }
  6. 6. /11@yegor256 6 public class LoggedUser implements User { private final User origin; public LoggedUser(User user) { this.origin = user; } @Override public void rename(String name) { long start = System.currentTimeMillis(); this.origin.rename(name); logger.debug( “user renaming took %d ms”, System.currentTimeMillis() - start ); } }
  7. 7. /11@yegor256 7 List posts = session.createQuery( "FROM Post WHERE author = ‘Jeff’” ).list(); for (Post post : (List<Post>) posts){ System.out.println( "Title: " + post.getTitle() ); }
  8. 8. /11@yegor256 8 SQL-speaking объекты
  9. 9. /11@yegor256 9 MySQL JDBC UPDATE user
 SET name = “Jeff”
 WHERE id = 123 statement.executeUpdate(); rename() x.update(“user”) .set(“name”, “Jeff”) .where(“id”, 123) .execute(); u.rename(“Jeff”); User jOOQ
  10. 10. /11@yegor256 10 public class Post { private int id; public int getId() { return this.id; } public Title getTitle() { return this.x.select(“title”) .from(“post”) .where(“id”, this.id) .fetchOne(); } public void setTitle(String txt) { this.x.update(“user”) .set(“title”, txt) .where(“id”, this.id) .execute(); } }
  11. 11. /11@yegor256 11 www.yegor256.com

×