Using DAOs without implementing them

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Groups

    Using DAOs without implementing them - Presentation 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
      • 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
    5. ...easier with a good IDE
    6. The new application
    7. What I'm able to use? (JUNIOR Programmer)
    8. What I like to use? (SENIOR Developer)
    9. 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 ?
    10. NOIA!
    11. 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?
    12. NOIA!
    13. Hibernate (JPA)
      • Hibernate (JPA)
      • Mapping with
      • annotations!
    14. 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() { ... } }
    15. @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
    16. NOIA!
    17. 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!
    18. DAO
      • DAO
      • (Data Access Object)
    19. The classic DAO
      • Interface
      • Implementation(s)
      • Factory
    20. NOIA!
    21. Do we need the factory?
      • Interface
      • Implementation(s)
      • Factory
      • Interface
      • Implementation(s)
      • Factory
    22. 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; }
    23. A minimal DAO Is it possible?
      • Interface
      • Implementation(s)
      • Factory
    24. 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”);
    25. 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);
    26. 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 { ... }
    27. 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();
    28. 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] )

    + benfantebenfante, 3 years ago

    custom

    2896 views, 0 favs, 3 embeds more stats

    With Parancoe (www.parancoe.org) You can define and more

    More info about this document

    CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

    Go to text version

    • Total Views 2896
      • 2889 on SlideShare
      • 7 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 113
    Most viewed embeds
    • 3 views on http://www.jugevents.org
    • 2 views on http://localhost:8081
    • 2 views on http://localhost:8084

    more

    All embeds
    • 3 views on http://www.jugevents.org
    • 2 views on http://localhost:8081
    • 2 views on http://localhost:8084

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events