That is very nice.

This isn't meant to rain on Twig's parade, but if you're going the JDO route 
you can also use generics to simplify things via a generic dao;

abstract class AbstractJdoDao<E> extends JdoDaoSupport {
    protected final transient Logger log = LoggerFactory.getLogger(getClass());

    protected final Class<E> entityClass;

    AbstractJdoDao(final Class<E> _entityClass, final PersistenceManagerFactory 
pmf) {
        this.entityClass = _entityClass;

        setPersistenceManagerFactory(pmf);
    }

    public E findById(final String id) {
        final E result = 
getPersistenceManager().getObjectById(this.entityClass, id);


        return (getPersistenceManager().detachCopy(result));
    }

Then my FacilityDao starts with

@Repository
public class FacilityDao extends AbstractJdoDao<Facility> {
    /**
     * @param pmf
     */
    @Autowired
    public FacilityDao(final PersistenceManagerFactory pmf) {
        super(Facility.class, pmf);
    }

Notice how it passes the class it's working with, which is stored in the 
abstract dao as entityClass.

Still not as easy as Twig though!


John Patterson wrote:
> Just to show how easy this sort of query and mapping is in Twig.   
> Notice the lack of Keys - the Book id is optional.
> 
> public class Book {
>      private long id;
>      private String title;
> 
>      @Entity(Child)
>      private List<Chapter> chapters = new ArrayList<Chapter>();
> }
> 
> The @Entity(Child) annotation applies to all the elements of the  
> collection
> 
> public class Chapter {
> 
>      @Key
>      private String title;
>      private int numPages;
> 
>      @Entity(Parent)
>      private Book book;
> }
> 
> The Chapter title has been configured to be the key name so it is  
> automatically encoded in the key.
> 
> Getting the book is as easy as:
> 
> session.find(Chapter.class, "War and Peace").getBook()
> 
> Notice that because Twig was written from scratch using generics the  
> result of find() is known to be a Chapter so we can then just call  
> getBook() on the result.
> 
> http://code.google.com/p/twig-persist/
> 
> On 3 Nov 2009, at 00:03, Solvek wrote:
> 
>> Now. How to retrieve a book and its chapters having only Chapter's
>> title (imagine all titles in whole systems are unique)
>>
>> On Sep 15, 1:07 am, Max Ross <[email protected]> wrote:
>>> Hello hello and welcome to the very first installment of JDO/JPA  
>>> Snippets
>>> That Work!
>>>
>>> Creating A Bidrectional Owned One-To-Many
>>>
>>> Suppose you're building a book catalog application and you want to  
>>> model
>>> books and chapters.  Books contain chapters.  A chapter cannot  
>>> exist without
>>> a book, so if you delete a book you want its chapters automatically  
>>> deleted
>>> along with it.  You also want to each chapter to have a reference  
>>> to the
>>> book that owns it.  Sounds like a bidrectional, owned, one-to-many
>>> relationship is just the thing.  First we'll set up our model  
>>> objects and
>>> then we'll add some code to create a Book with 2 Chapters.
>>>
>>> JPA:
>>> @Entity
>>> public class Book {
>>>     @Id
>>>     @GeneratedValue(strategy=GenerationType.IDENTITY)
>>>     private Key id;
>>>
>>>     private String title;
>>>
>>>     @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
>>>     private List<Chapter> chapters = new ArrayList<Chapter>();
>>>
>>>     // getters and setters
>>>
>>> }
>>>
>>> @Entity
>>> public class Chapter {
>>>     @Id
>>>     @GeneratedValue(strategy=GenerationType.IDENTITY)
>>>     private Key id;
>>>
>>>     private String title;
>>>     private int numPages;
>>>
>>>     @ManyToOne(fetch = FetchType.LAZY)
>>>     private Book book;
>>>
>>>     // getters and setters
>>>
>>> }
>>>
>>> Now let's create a book with two chapters (we'll assume someone  
>>> else is
>>> creating and closing an EntityManager named 'em' for us):
>>>
>>> Book b = new Book();
>>> b.setTitle("JPA 4eva");
>>> Chapter c1 = new Chapter();
>>> c1.setTitle("Intro");
>>> c1.setNumPages(10);
>>> b.getChapters().add(c1);
>>> Chapter c2 = new Chapter();
>>> c2.setTitle("Configuration");
>>> c2.setNumPages(9);
>>> b.getChapters().add(c2);
>>>
>>> em.getTransaction().begin();
>>> try {
>>>     em.persist(b);
>>>     em.getTransaction().commit();} finally {
>>>
>>>     if (em.getTransaction().isActive()) {
>>>         em.getTransaction().rollback();
>>>     }
>>>
>>> }
>>>
>>> JDO:
>>>
>>> @PersistenceCapable(identityType = IdentityType.APPLICATION,  
>>> detachable =
>>> "true")
>>> public class Book {
>>>
>>>     @PrimaryKey
>>>     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>>>     private Key id;
>>>
>>>     private String title;
>>>
>>>     @Persistent(mappedBy = "book")
>>>     @Element(dependent = "true")
>>>     private List<Chapter> chapters = new ArrayList<Chapter>();
>>>
>>>     // getters and setters
>>>
>>> }
>>>
>>> @PersistenceCapable(identityType = IdentityType.APPLICATION,  
>>> detachable =
>>> "true")
>>> public class Chapter {
>>>     @PrimaryKey
>>>     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>>>     private Key id;
>>>
>>>     private String title;
>>>     private int numPages;
>>>
>>>     @Persistent
>>>     private Book book;
>>>
>>>     // getters and setters
>>>
>>> }
>>>
>>> Now let's create a book with two chapters (we'll assume someone  
>>> else is
>>> creating and closing a PersistenceManager named 'pm' for us):
>>>
>>> Book b = new Book();
>>> b.setTitle("JDO 4eva");
>>> Chapter c1 = new Chapter();
>>> c1.setTitle("Intro");
>>> c1.setNumPages(10);
>>> b.getChapters().add(c1);
>>> Chapter c2 = new Chapter();
>>> c2.setTitle("Configuration");
>>> c2.setNumPages(9);
>>> b.getChapters().add(c2);
>>>
>>> pm.currentTransaction().begin();
>>> try {
>>>     pm.makePersistent(b);
>>>     pm.currentTransaction().commit();} finally {
>>>
>>>     if (pm.currentTransaction().isActive()) {
>>>         pm.currentTransaction().rollback();
>>>     }
>>>
>>> }
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to