Your SlideShare is downloading. ×
0
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Using DAOs without implementing them
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Using DAOs without implementing them

3,694

Published on

With Parancoe (www.parancoe.org) You can define and use your DAOs without implementing them. This speeds up the development of your application and the satisfaction of the developers. This …

With Parancoe (www.parancoe.org) You can define and use your DAOs without implementing them. This speeds up the development of your application and the satisfaction of the developers. This presentation was held at the JavaDay in Torino, the October 20, 2007.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,694
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
154
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Transcript

    • 1. Parancoe usare i DAO senza implementarli a cura di Lucio Benfante Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Roma 2007
    • 2. What is Parancoe?
      • A framework (YAFF?)
              • Yet Another Fucking Framework
      • A framework aggregation
      • A tool for developers
      • Hibernate/JPA + Spring 2 + Spring MVC
      • Maven 2
      • + ...
      • (write what you are using)
    • 3. Convention over Configuration DRY (Don't repeat yourself) Don't re-invent the wheel Guidelines
    • 4. Fun for the developer Main Guideline
    • 5.
      • mvn archetype:create
      • -DarchetypeGroupId=org.parancoe
      • -DarchetypeArtifactId=parancoe-webarchetype
      • -DarchetypeVersion=0.3.2
      • -DgroupId= it.jugpadova
      • -DartifactId= demoapp
      • -DpackageName= it.jugpadova.demoapp
      • ...at work in seconds!
      Starting a new application
    • 6. ...easier with a good IDE
    • 7. The new application
    • 8. What I'm able to use? (JUNIOR Programmer)
    • 9. What I like to use? (SENIOR Developer)
    • 10. What for the persistent layer? Class.forName( "com.mioDbms.mioDriver" ); Connection con=null; try { con = DriverManager.getConnection ( “ jdbc:...”, “user”, “pass”); ResultSet rs; PreparedStatement ps = conn.prepareStatement( " SELECT * FROM Person WHERE name=? ”); ps. setString (1, "Lucio Benfante"); rs = ps.executeQuery(); while ( rs.next() ) { rs.getString...ecc..ecc.... } rs.close(); ps.close(); stmt.close(); } catch (Exception e){ ... } finally { try { con. close (); } catch( Exception ignoreMe ) {} } JDBC ?
    • 11. NOIA!
    • 12. Hibernate <class name=&quot;events.Person&quot; table=&quot;PERSON&quot;> <id name=&quot;id&quot; column=&quot;PERSON_ID&quot;> <generator class=&quot;native&quot;/> </id> <property name=&quot;age&quot;/> <property name=&quot;firstname&quot;/> <property name=&quot;lastname&quot;/> <set name=&quot;events&quot; table=&quot;PERSON_EVENT&quot;> <key column=&quot;PERSON_ID&quot;/> <many-to-many column=&quot;EVENT_ID&quot; class=&quot;events.Event&quot;/> </set> </class>
      • Hibernate?
      • Mapping in XML?
    • 13. NOIA!
    • 14. Hibernate (JPA)
      • Hibernate (JPA)
      • Mapping with
      • annotations!
    • 15. A “simple” persistent class @Entity() public class Person { private Long id; private String firstName; private String lastName; private Integer version; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { ... } public void setId(Long id) { ... } public String getFirstName() { ... } public void setFirstName(String firstName) { ... } public String getLastName() { ... } public void setLastName(String lastName) { ... } @version public Integer getVersion() { ... } public void setVersion() { ... } }
    • 16. @Entity() public class Person { private Long id; private String firstName; private String lastName; private Integer version; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { ... } public void setId(Long id) { ... } public String getFirstName() { ... } public void setFirstName(String firstName) { ... } public String getLastName() { ... } public void setLastName(String lastName) { ... } @version public Integer getVersion() { ... } public void setVersion() { ... } } What I don't like?
      • Declaration of persistent classes
      <hibernate-configuration> <session-factory> <mapping class=&quot;it.jugpadova.demoapp.po.Person&quot;/> </session-factory> </hibernate-configuration>
      • Required fields
    • 17. NOIA!
    • 18. The Parancoe Way @Entity() public class Person extends EntityBase { private String firstName; private String lastName; public String getFirstName() { ... } public void setFirstName(String firstName) { ... } public String getLastName() { ... } public void setLastName(String lastName) { ... } } ...auto-discovered!
    • 19. DAO
      • DAO
      • (Data Access Object)
    • 20. The classic DAO
      • Interface
      • Implementation(s)
      • Factory
    • 21. NOIA!
    • 22. Do we need the factory?
      • Interface
      • Implementation(s)
      • Factory
      • Interface
      • Implementation(s)
      • Factory
    • 23. Common implementation public List<Person> findByLastName (String lastName) { Session session = sessionFactory.openSession(); Transaction tx = null; List<Person> result = null; try { tx = session.beginTransaction(); Query q = session.createQuery( &quot;from Person p where p.lastName = ?”+ “ order by p.lastName” ); q.setString(0, lastName); result = q.list(); tx.commit(); } catch (HibernateException he) { if (tx!=null) tx.rollback(); throw he; } finally { try {session.close();} catch(Exception ignore) {} } return result; }
    • 24. A minimal DAO Is it possible?
      • Interface
      • Implementation(s)
      • Factory
    • 25. Parancoe DAO @Dao(entity=Person.class) public interface PersonDao extends GenericDao<Person, Long> { List<Person> findByLastName(String lastName); } Auto-discovered Automatically available in the Spring context people = dao().getPersonDao().findByLastName(“Benfante”);
    • 26. Some finder examples
      • List<Person> findBy FirstName And LastName And BirthDate( String firstName, String lastName, Date birthDate);
      • List<Person> findBy BirthDate OrderBy LastName And FirstName( Date birthDate);
      • List<Person> findBy OrderBy LastName And FirstName();
      • Person findByFirstNameAndLastName( String firstName, String lastName);
      • List<Person> findByLastName( @Compare(CompareType.ILIKE) String lastName);
      • List<Person> findByLastName(String lastName, @FirstResult int offset, @MaxResults int limit);
    • 27. Complex queries @Dao(entity=Person.class) public interface PersonDao extends GenericDao<Person, Long> { ... List<Person> searchByPartialUncasedLastName( String partialLastName); ... } @Entity() @NamedQueries({ @NamedQuery( name=&quot;Person.searchByPartialUncasedLastName&quot; , query=&quot;from Person p”+ ” where lower(p.lastName) like lower(?)”+ ” order by p.lastName&quot;) }) public class Person extends EntityBase { ... }
    • 28. Methods of the base DAO PK create(T newInstance); void createOrUpdate(T o); T read(PK id); void update(T transientObject); void delete(T persistentObject); List<T> findAll(); List<T> searchByCriteria(Criterion... criterion); List<T> searchByCriteria(DetachedCriteria criteria); List<T> searchByCriteria( DetachedCriteria criteria, int firstResult, int maxResults); int deleteAll(); long count();
    • 29. Informazioni sul JUG Padova
      • Sito Web
        • www.jugpadova.it
      • Mailing List
        • !Attenzione nuova mailing list su googlegroups
        • http://groups.google.com/group/jugpadova
      • Sito Parancoe
        • www.parancoe.org
      • Contattami
        • Lucio Benfante ( [email_address] )
        • ML Parancoe ( [email_address] )

    ×