db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Db-derby Wiki] Update of "DerbyOnTomcatWithSpringHibernate" by JoonasKoivunen
Date Thu, 16 Apr 2009 14:39:52 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification.

The following page has been changed by JoonasKoivunen:
http://wiki.apache.org/db-derby/DerbyOnTomcatWithSpringHibernate

The comment on the change is:
Encouraged by comments at #derby continued writing up about daos and services.

------------------------------------------------------------------------------
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>${derby.version}</version>
-     <scope>test</scope> <!-- this means derby will only be available for
testing in exproject-core, 
+     <scope>test</scope> <!-- this means derby will only be available for

+                              testing in exproject-core, will not be included 
-                              will not be included to other projects using exproject-core
as dependency -->
+                              to other projects using exproject-core 
+                              as dependency -->
    </dependency>
  </dependencies>
  }}}
@@ -105, +107 @@

  ==== Entities ====
  (TODO: create two simple entities that have some kind of relation to each other)
  
+ In this example we have two simple entities, Parent and Child. You can probably guess their
relationship.
+ {{{
+ #!java
+ package org.example.exproject.domain.entities;
+ 
+ // imports omitted
+ 
+ @Entity(name="parent")
+ public class Parent {
+   @Id
+   private Long id;
+   
+   @OneToMany(cascade=CascadeType.ALL, targetEntity=Child.class)
+   private Set<Child> children;
+ 
+   // getters and setters omitted
+ }
+ }}}
+ 
+ {{{
+ #!java
+ 
+ package org.example.exproject.domain.entities;
+ 
+ // imports omitted
+ 
+ @Entity(name="child")
+ public class Child {
+   @Id
+   private Long id;
+   
+   @ManyToOne(cascade=CascadeType.ALL)
+   private Parent parent;
+ 
+   // getters and setters omitted
+ }
+ 
+ }}}
+ 
  ==== DAO's ====
  (TODO: simple daos, HibernateDaoSupport, link to ibm generic dao article)
+ DAO -layer provides simple CRUD like operations to your database. While we are using Hibernate
in this example it's generally a good idea to build your DAO interfaces so that you could
later add a JDBC backend if you need or want. Though, there might be a point in encapsuling
some logic also to DAO -layer which will open possibilities like implementing the logic in
derby itself.
+ 
+ Spring framework provides abstract HibernateDaoSupport -class which will make your Hibernate
DAO -code consist mostly of one-liners.
+ 
+ Generic DAO implementation will save you a lot of trouble, essentially providing you with
CRUD -operations with the cost of one constructor parameter. IBM has a nice article on [http://www.ibm.com/developerworks/java/library/j-genericdao.html
not repeating code with DAOs], there are lot of more articles around, to name a few:
+  * [http://www.hibernate.org/328.html Generic Data Access Objects] at hibernate.org
+  * [http://blog.hibernate.org/Bloggers/GenericDAOPatternWithJDK50 Generic DAO Pattern With
JDK 5.0] at inrelation.to
+  * [http://code.google.com/p/hibernate-generic-dao/ hibernate-generic-dao] project
  
  ==== Services ====
  (TODO: simple generic crud service, @Transactional)
+ Service layer adds logic that uses your DAO interfaces to provide your business logic and
handles transaction creation and commits. With Spring a @Transactional -annotation and some
configuration (in the next section) Spring will arrange creation and committing for you without
any boilerplate code.
+ 
+ As with generic dao's you migth find the need to implement yourself a Generic Service as
well to provide simplest CRUD functionality by just delegating methods over to your generic
dao. This again, will let your ParentService implementation focus on the real business logic.
+ 
+ @Transactional has some pitfalls as well, some recommended reading:
+  * [http://www.ibm.com/developerworks/java/library/j-ts1.html Understanding Transaction
Pitfalls] at ibm.com
+  * Spring reference documentation on [http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html
transaction management]
  
  ==== Configuring ====
  (TODO: Spring context:component-scan, context:annotation-config, tx:annotation-driven)

Mime
View raw message