Seam + Hibernate + MVC
melampo Nov 16, 2006 6:54 AMHi.
First of all, sorry for my english :D
I am newby using seam and I am trying to develope a web application with a MVC layer model (I have no choose, I didn't design the application).
I am some confused about what annotations a need for each layer and I can't find any example that implements this model. Anyone can help me?:
Entity:
@Entity @Name("libro") @Scope(CONVERSATION) @Table(name = "libros") public class Libro implements Serializable { private static final long serialVersionUID = -1252025299985845136L; private String isbn; private String titulo; private String autor; @Id @Column(name = "isbn") public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } @NotNull @Column(name = "titulo") public String getTitulo() { return titulo; } public void setTitulo(String titulo) { this.titulo = titulo; } @NotNull @Column(name = "autor") public String getAutor() { return autor; } public void setAutor(String autor) { this.autor = autor; } }
Service Interface:
@Local public interface LibroManager { ... public void add(); ... }
Service Implementation:
@Stateful @Name("libroManager") @Scope(CONVERSATION) public class LibroManagerAction implements LibroManager, Serializable { private static final long serialVersionUID = 4607492839166551543L; @In(required = false) @Out(required = false) private Libro libro; private LibroDAOhbn libroDAO = new LibroDAOhbn(); ... public void add() { libroDAO.Create(libro); } ... }
Dao Interface:
@Local public interface LibroDAO { public void Create(Libro libro); }
Dao Implementation:
@Stateless @Name("libroDAO") public class LibroDAOhbn implements LibroDAO { @PersistenceContext private EntityManager em; public void Create(Libro libro) { System.out.println("Registrando " + libro.getTitulo()); try { em.persist(libro); } catch (Exception e) { System.out.println(e.getMessage()); } } }
Ok, when the Create(libro) method is called, I only get an exception: NULL. I think that this is caused because I lost the EntityManager or Entity context, but I don't know how to mend it.
I try to inject the libroDAOhbn in the service class with:
@In (create=true) private LibroDAOhbn libroDAO = new LibroDAOhbn();
but I get an error everytime I call any service's method. I think that it must to have an easy solution, but I can't find it :). Some suggestions?
If I don't use a DAO layer and I do all the implementation in the service/controller , I have no problems. This runs perfectly:
@Stateful @Name("libroManager") @Scope(CONVERSATION) public class LibroManagerAction implements LibroManager, Serializable { private static final long serialVersionUID = 4607492839166551543L; @In(required = false) @Out(required = false) private Libro libro; private LibroDAOhbn libroDAO = new LibroDAOhbn(); ... public void add() { try { em.persist(libro); FacesMessages.instance().add( "Insertado correctamente: " + libro.getTitulo()); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } .... }
but I need the DAO's :(
Thank's for all.
PS: Sorry for my englis again.