Using DAOs without implementing them
Upcoming SlideShare
Loading in...5
×
 

Using DAOs without implementing them

on

  • 5,248 views

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.

Statistics

Views

Total Views
5,248
Views on SlideShare
5,236
Embed Views
12

Actions

Likes
1
Downloads
151
Comments
0

5 Embeds 12

http://www.slideshare.net 4
http://www.jugevents.org 3
http://localhost:8081 2
http://localhost:8084 2
https://localhost:8443 1

Accessibility

Upload Details

Uploaded via as OpenOffice

Usage Rights

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

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Using DAOs without implementing them Using DAOs without implementing them Presentation Transcript

  • Parancoe usare i DAO senza implementarli a cura di Lucio Benfante Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License Roma 2007
  • 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)
  • Convention over Configuration DRY (Don't repeat yourself) Don't re-invent the wheel Guidelines View slide
  • Fun for the developer Main Guideline View slide
    • 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
  • ...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( "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 ?
  • NOIA!
  • 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?
  • NOIA!
  • Hibernate (JPA)
    • Hibernate (JPA)
    • Mapping with
    • annotations!
  • 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() { ... } }
  • @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
  • NOIA!
  • 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!
  • DAO
    • DAO
    • (Data Access Object)
  • The classic DAO
    • Interface
    • Implementation(s)
    • Factory
  • NOIA!
  • Do we need the factory?
    • Interface
    • Implementation(s)
    • Factory
    • Interface
    • Implementation(s)
    • Factory
  • 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; }
  • A minimal DAO Is it possible?
    • Interface
    • Implementation(s)
    • Factory
  • 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”);
  • 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);
  • 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 { ... }
  • 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();
  • 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] )