[JBoss Seam] - Seam + Hibernate + MVC
by MelampO
Hi.
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.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986482#3986482
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986482
19Â years, 7Â months
[JBoss/Spring Integration] - Re: Jndi lookup on spring factory return 'null'
by blubberFoo
Hello,
I have the same problem. Have you found a solution yet?
I have JBoss 4.0.4GA and a .ear archive with a bla-server.spring archive inside. The spring deployer does its job well and the ApplicationContext is created and registered to local JNDI:
| 11:49:27,755 INFO [ApplicationContextLoaderImpl] Bean factory [bla-server] binded to local JNDI.
|
The JNDIView lists the bound object in the gloabal namespace:
| +- bla-server (class: org.jboss.spring.factory.NamedXmlApplicationContext)
|
With the following code it is not possible to access the bound object when the snippet is run from outside the container.
| Hashtable<String,String> environment = new Hashtable<String,String>();
| environment.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
| environment.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
| environment.put("java.naming.provider.url","jnp://localhost:1099");
|
| try {
| initialContext = new InitialContext(environment);
| }
| catch (NamingException exception) {
| fail("Could not create InitialContext.Exception:"+exception);
| return;
| }
| try {
| this.ac = (ApplicationContext)initialContext.lookup("bla-server");
| }
| catch (NamingException exception) {
| fail("Could not obtain 'bla-server' ApplicationContext:"+exception);
| return;
| }
|
I want to access the ApplicationContext from the outside to test my beans and the associated ejb3 persistence which are inside the container. Access from inside the container is no problem.
Is there a way to register the ApplicationContext in a way it is visible from the outside? On a more general note I would be interested in how people do test there beans when they rely on the container.
Regards,
Ricky
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986481#3986481
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986481
19Â years, 7Â months
[Persistence, JBoss/CMP, Hibernate, Database] - @GeneratedValue in mappedSuperclass problem
by hoeft
Hi!
Many of my entities have an id of type long. So I put the code (getters, setters ...) into an superclass. After I did it, the id wasn't initialized by hibernate any more, if I persisted an entity. Thats very strange.
Here is the code:
| public class GenerateValueTestcase extends TestCase {
|
| @MappedSuperclass
| public static abstract class EntityWithIntId {
|
| private long id = -1;
|
| @Id
| @GeneratedValue(strategy = GenerationType.AUTO)
| public long getId() {
| return id;
| }
|
| public void setId(long id) {
| this.id = id;
| }
| }
|
| @Entity
| public static class TestEntity extends EntityWithIntId implements Serializable {
|
| private static final long serialVersionUID = 1L;
|
| }
|
| public void testGeneratedIds() {
|
| TestStarterRemote bean = UsefullFunctions.getTestStarterRemote();
|
| Set<Long> ids = new HashSet<Long>();
|
| final int NO_ENTITIES = 10;
|
| for (int i = 0; i < NO_ENTITIES; i++) {
| EntityWithIntId entity = new TestEntity();
| entity = bean.merge(entity);
| ids.add(entity.getId());
| }
|
| assertEquals("Each entity must have an unique id", NO_ENTITIES, ids
| .size());
|
| }
|
| }
|
The bean.merge method:
| public <T> T merge(T o) {
| return entityManager.merge(o);
| }
|
Any ideas or workarounds? Copying the code into each class is not the best idea I think....
Thanks for your help
Meinert
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986474#3986474
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986474
19Â years, 7Â months
[EJB 3.0] - @GeneratedValue in mappedSuperclass Problem
by hoeft
Hi!
Many of my entities have an id of type long. So I put the code (getters, setters ...) into an superclass. After I did it, the id wasn't initialized by hibernate any more, if I persisted an entity. Thats very strange.
Here is the code:
| public class GenerateValueTestcase extends TestCase {
|
| @MappedSuperclass
| public static abstract class EntityWithIntId {
|
| private long id = -1;
|
| @Id
| @GeneratedValue(strategy = GenerationType.AUTO)
| public long getId() {
| return id;
| }
|
| public void setId(long id) {
| this.id = id;
| }
| }
|
| @Entity
| public static class TestEntity extends EntityWithIntId implements Serializable {
|
| private static final long serialVersionUID = 1L;
|
| }
|
| public void testGeneratedIds() {
|
| TestStarterRemote bean = UsefullFunctions.getTestStarterRemote();
|
| Set<Long> ids = new HashSet<Long>();
|
| final int NO_ENTITIES = 10;
|
| for (int i = 0; i < NO_ENTITIES; i++) {
| EntityWithIntId entity = new TestEntity();
| entity = bean.merge(entity);
| ids.add(entity.getId());
| }
|
| assertEquals("Each entity must have an unique id", NO_ENTITIES, ids
| .size());
|
| }
|
| }
|
The bean.merge method:
| public <T> T merge(T o) {
| return entityManager.merge(o);
| }
|
Any ideas or workarounds? Copying the code into each class is not the best idea I think....
Thanks for your help
Meinert
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986471#3986471
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986471
19Â years, 7Â months