8 Replies Latest reply on Nov 11, 2008 9:20 PM by norad2

    Entity beans in seperate jar

    norad2

      I have a Seam application and a backend application that both use the same tables in the database. I would like to be able to use the same entity bean classes for both applications, so that if I make any changes to the entities it applies to both projects.


      Right now I basically have three projects


      ProjectName-Backend
      ProjectName-Common
      ProjectName-Seam


      The entity beans are in the Common project and I would like to be able to use them in a Seam project. What do I need to configure to be able to do this?


      Thanks,
      Dan

        • 1. Re: Entity beans in seperate jar
          gjeudy

          what build tool do you use ?


          do you want to package the common jar inside both backend and seam projects and deploy the projects independently each having its own persistence unit at runtime?


          Or alternatively do you want to deploy the entities independently as a persistence unit that could be shared between 2 projects at runtime?

          • 2. Re: Entity beans in seperate jar
            norad2

            I use Maven.


            The common jar should be inside the backend and seam project, which are deployed independently each with its own persistence unit.

            • 3. Re: Entity beans in seperate jar
              gjeudy

              ok then there is nothing Seam specific to the solution.


              Your problem can be solved by maven, if you are using maven2 then you should use the release plugin to manage your release cycle and version dependencies.


              so when you release common 1.0 you can refer to it in the dependencies section of your other projects.


              release a new common version will require you to update the version in the dependencies section of your other projects though. That is perfectly OK since you don't want the other projects to get the new version of common unless you explicitely upgrade and modify code/test accurately that it works with your backend and seam projects.


              You need to package your seam and backend project as EAR to be able to deploy persistence units at least that is my understanding. I'm not aware of all the options, I know that in jboss you can deploy the ejb-jar standalone with your persistence unit in it.


              • 4. Re: Entity beans in seperate jar
                norad2

                So the issue that I ran into is that it doesn't seem to know how to identify the entities.


                Everything packages and compiles just fine. When I deploy to JBoss there are no errors. When I try to actually use the application I get the following errors.


                javax.el.ELException: javax.ejb.EJBTransactionRolledbackException: Unknown entity: com.company.project.common.model.Users
                
                
                .
                



                This is a Seam project that I had already built and had working. Previously the Entities were inside of the Seam projects jar but I wanted to seperate them from the Seam project so that other projects can use them.

                • 5. Re: Entity beans in seperate jar
                  norad2

                  Ok so I figured this out.


                  The persistence.xml in the Seam jar had to be modified slightly. I guess when the entities are in the Seam jar they do not need to be listed in the persistence.xml explicitly.


                  FROM:


                  <?xml version="1.0" encoding="UTF-8"?>
                  <!-- Persistence deployment descriptor for prod profile -->
                  <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                               version="1.0">
                               
                       <persistence-unit name="XXXXX">
                            <provider>org.hibernate.ejb.HibernatePersistence</provider>
                            <jta-data-source>java:/XXXXX-Datasource</jta-data-source>
                            <properties>
                                 <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
                                 <property name="hibernate.jdbc.batch_size" value="20"/>
                                 <property name="hibernate.show_sql" value="true"/> 
                                 <property name="hibernate.format_sql" value="true"/>
                                 <property name="jboss.entity.manager.factory.jndi.name" value="java:/XXXXX-EntityManagerFactory"/>
                                 <property name="hibernate.default_schema" value="XXXXX"/>
                            </properties>
                       </persistence-unit>
                  </persistence>
                  
                  



                  TO:


                  <?xml version="1.0" encoding="UTF-8"?>
                  <!-- Persistence deployment descriptor for prod profile -->
                  <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                               version="1.0">
                               
                       <persistence-unit name="XXXXX">
                            <provider>org.hibernate.ejb.HibernatePersistence</provider>
                            <jta-data-source>java:/XXXXX-Datasource</jta-data-source>
                            <class>com.company.project.common.model.Entity1</class>
                            <class>com.company.project.common.model.Entity2</class>
                            <class>com.company.project.common.model.Entity3</class>
                            <class>com.company.project.common.model.Entity4</class>
                            <class>com.company.project.common.model.Entity5</class>
                            <properties>
                                 <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
                                 <property name="hibernate.jdbc.batch_size" value="20"/>
                                 <property name="hibernate.show_sql" value="true"/> 
                                 <property name="hibernate.format_sql" value="true"/>
                                 <property name="jboss.entity.manager.factory.jndi.name" value="java:/XXXXX-EntityManagerFactory"/>
                                 <property name="hibernate.default_schema" value="XXXXX"/>
                            </properties>
                       </persistence-unit>
                  </persistence>
                  
                  

                  • 6. Re: Entity beans in seperate jar
                    gjeudy

                    Yes you can do that or use the jar-file element to reference the common entities jar which is a better approach IMHO.

                    • 7. Re: Entity beans in seperate jar
                      ceylanh
                      Hello Dan,

                      I am new to seam and used to use maven for the past struts + EJB + Hibernate projects.

                      I would like to switch to seam for a new project and need a working maven structure.

                      Would you please post your maven poms?

                      Thanks,
                      Hasan Ceylan
                      • 8. Re: Entity beans in seperate jar
                        norad2