Parancoe usare i DAO senza implementarli a cura di Lucio Benfante Creative Commons Attribution-NonCommercial-ShareAlike 2....
What is Parancoe? <ul><li>A framework (YAFF?)  </li></ul><ul><ul><ul><ul><ul><li>Yet Another  Fucking  Framework </li></ul...
Convention over Configuration DRY (Don't repeat yourself) Don't re-invent the wheel Guidelines
Fun for the developer Main Guideline
<ul><li>mvn archetype:create  </li></ul><ul><li>-DarchetypeGroupId=org.parancoe  </li></ul><ul><li>-DarchetypeArtifactId=p...
...easier with a  good  IDE
The new application
What I'm able to use? (JUNIOR Programmer)
What I like to use? (SENIOR Developer)
What for the persistent layer? Class.forName( &quot;com.mioDbms.mioDriver&quot; ); Connection con=null; try  { con = Drive...
NOIA!
Hibernate <class name=&quot;events.Person&quot; table=&quot;PERSON&quot;> <id name=&quot;id&quot; column=&quot;PERSON_ID&q...
NOIA!
Hibernate (JPA) <ul><li>Hibernate (JPA) </li></ul><ul><li>Mapping with </li></ul><ul><li>annotations! </li></ul>
A “simple” persistent class @Entity() public class Person { private Long id;  private String firstName;  private String la...
@Entity() public class Person { private Long id;   private String firstName;  private String lastName; private Integer ver...
NOIA!
The Parancoe Way @Entity() public class Person extends  EntityBase  { private String firstName;  private String lastName; ...
DAO <ul><li>DAO </li></ul><ul><li>(Data Access Object) </li></ul>
The classic DAO <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul>
NOIA!
Do we need the factory? <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul><ul><li>...
Common implementation public List<Person>  findByLastName (String lastName) { Session session = sessionFactory.openSession...
A minimal DAO Is it possible? <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul>
Parancoe DAO @Dao(entity=Person.class) public  interface  PersonDao extends GenericDao<Person, Long>  { List<Person> findB...
Some finder examples <ul><li>List<Person>  findBy FirstName And LastName And BirthDate(   String firstName, String lastNam...
Complex queries @Dao(entity=Person.class) public interface PersonDao extends GenericDao<Person, Long> { ... List<Person> s...
Methods of the base DAO PK create(T newInstance); void createOrUpdate(T o); T read(PK id); void update(T transientObject);...
Informazioni sul JUG Padova <ul><li>Sito Web </li></ul><ul><ul><li>www.jugpadova.it </li></ul></ul><ul><li>Mailing List  <...
Upcoming SlideShare
Loading in...5
×

Using DAOs without implementing them

3,730

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 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,730
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
154
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Using DAOs without implementing them

    1. 1. Parancoe usare i DAO senza implementarli a cura di Lucio Benfante Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Roma 2007
    2. 2. What is Parancoe? <ul><li>A framework (YAFF?) </li></ul><ul><ul><ul><ul><ul><li>Yet Another Fucking Framework </li></ul></ul></ul></ul></ul><ul><li>A framework aggregation </li></ul><ul><li>A tool for developers </li></ul><ul><li>Hibernate/JPA + Spring 2 + Spring MVC </li></ul><ul><li>Maven 2 </li></ul><ul><li>+ ... </li></ul><ul><li>(write what you are using) </li></ul>
    3. 3. Convention over Configuration DRY (Don't repeat yourself) Don't re-invent the wheel Guidelines
    4. 4. Fun for the developer Main Guideline
    5. 5. <ul><li>mvn archetype:create </li></ul><ul><li>-DarchetypeGroupId=org.parancoe </li></ul><ul><li>-DarchetypeArtifactId=parancoe-webarchetype </li></ul><ul><li>-DarchetypeVersion=0.3.2 </li></ul><ul><li>-DgroupId= it.jugpadova </li></ul><ul><li>-DartifactId= demoapp </li></ul><ul><li>-DpackageName= it.jugpadova.demoapp </li></ul><ul><li>...at work in seconds! </li></ul>Starting a new application
    6. 6. ...easier with a good IDE
    7. 7. The new application
    8. 8. What I'm able to use? (JUNIOR Programmer)
    9. 9. What I like to use? (SENIOR Developer)
    10. 10. What for the persistent layer? Class.forName( &quot;com.mioDbms.mioDriver&quot; ); Connection con=null; try { con = DriverManager.getConnection ( “ jdbc:...”, “user”, “pass”); ResultSet rs; PreparedStatement ps = conn.prepareStatement( &quot; SELECT * FROM Person WHERE name=? ”); ps. setString (1, &quot;Lucio Benfante&quot;); 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. 11. NOIA!
    12. 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> <ul><li>Hibernate? </li></ul><ul><li>Mapping in XML? </li></ul>
    13. 13. NOIA!
    14. 14. Hibernate (JPA) <ul><li>Hibernate (JPA) </li></ul><ul><li>Mapping with </li></ul><ul><li>annotations! </li></ul>
    15. 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. 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? <ul><li>Declaration of persistent classes </li></ul><hibernate-configuration> <session-factory> <mapping class=&quot;it.jugpadova.demoapp.po.Person&quot;/> </session-factory> </hibernate-configuration> <ul><li>Required fields </li></ul>
    17. 17. NOIA!
    18. 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. 19. DAO <ul><li>DAO </li></ul><ul><li>(Data Access Object) </li></ul>
    20. 20. The classic DAO <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul>
    21. 21. NOIA!
    22. 22. Do we need the factory? <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul><ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul>
    23. 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. 24. A minimal DAO Is it possible? <ul><li>Interface </li></ul><ul><li>Implementation(s) </li></ul><ul><li>Factory </li></ul>
    25. 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. 26. Some finder examples <ul><li>List<Person> findBy FirstName And LastName And BirthDate( String firstName, String lastName, Date birthDate); </li></ul><ul><li>List<Person> findBy BirthDate OrderBy LastName And FirstName( Date birthDate); </li></ul><ul><li>List<Person> findBy OrderBy LastName And FirstName(); </li></ul><ul><li>Person findByFirstNameAndLastName( String firstName, String lastName); </li></ul><ul><li>List<Person> findByLastName( @Compare(CompareType.ILIKE) String lastName); </li></ul><ul><li>List<Person> findByLastName(String lastName, @FirstResult int offset, @MaxResults int limit); </li></ul>
    27. 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. 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. 29. Informazioni sul JUG Padova <ul><li>Sito Web </li></ul><ul><ul><li>www.jugpadova.it </li></ul></ul><ul><li>Mailing List </li></ul><ul><ul><li>!Attenzione nuova mailing list su googlegroups </li></ul></ul><ul><ul><li>http://groups.google.com/group/jugpadova </li></ul></ul><ul><li>Sito Parancoe </li></ul><ul><ul><li>www.parancoe.org </li></ul></ul><ul><li>Contattami </li></ul><ul><ul><li>Lucio Benfante ( [email_address] ) </li></ul></ul><ul><ul><li>ML Parancoe ( [email_address] ) </li></ul></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×