01: /**
02: * EasyBeans
03: * Copyright (C) 2007 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * --------------------------------------------------------------------------
22: * $Id: StatelessBean.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.examples.ear;
25:
26: import java.util.List;
27:
28: import javax.ejb.Stateless;
29: import javax.persistence.EntityManager;
30: import javax.persistence.PersistenceContext;
31: import javax.persistence.Query;
32:
33: /**
34: * Defines a stateless bean with a local interface. This beans provides methods
35: * for accessing the two entities, Book and Author.<br>
36: * By default, an interface is considered as a local interface if there is no
37: * @Remote annotation.
38: * @author Florent Benoit
39: */
40: @Stateless
41: public class StatelessBean implements StatelessLocal {
42:
43: /**
44: * Entity manager utilise par ce bean.
45: */
46: @PersistenceContext
47: private EntityManager entityManager = null;
48:
49: /**
50: * Create authors and their books.
51: */
52: public void init() {
53: // Books of balzac
54: Author balzac = new Author("Honore de Balzac");
55: Book pereGloriot = new Book("Le Pere Goriot", balzac);
56: balzac.getBooks().add(pereGloriot);
57: Book lesChouans = new Book("Les Chouans", balzac);
58: balzac.getBooks().add(lesChouans);
59:
60: // Store (only author as cascade attribute is set to all)
61: entityManager.persist(balzac);
62:
63: // Add Hugo books
64: Author hugo = new Author("Victor Hugo");
65: hugo.addBook("Les Miserables");
66: hugo.addBook("Notre-Dame de Paris");
67:
68: // Store
69: entityManager.persist(hugo);
70: }
71:
72: /**
73: * @return list of authors.
74: */
75: @SuppressWarnings("unchecked")
76: public List<Author> listOfAuthors() {
77: Query auteurs = entityManager.createNamedQuery("allAuthors");
78: return auteurs.getResultList();
79: }
80:
81: /**
82: * @return ist of books.
83: */
84: @SuppressWarnings("unchecked")
85: public List<Book> listOfBooks() {
86: return entityManager.createNamedQuery("allBooks")
87: .getResultList();
88: }
89:
90: }
|