-
1. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 7, 2009 6:38 AM (in response to itays100)JpaIdentityStore uses the SMPC (EntityManager) so you need to define it in components.xml. If you want to use Hibernate session in your classes, just write a factory method that gets it from EntityManager.getDelegate().
HTH
-
2. Re: Seam Security with hibernate session won't work.
itays100 Nov 7, 2009 7:08 PM (in response to itays100)Nikolay,
Yeh well so you agree with me that
Seam Security with hibernate session won't work.I guess I need to clarify myself:
I need to know how to configure the component.xml in order to support both seam security and hibernate session.The functionalities i want to achieve are 2:
1. inject @In private EntityManager entityManager;
to my component that handle user accounts.2. inject @In private Session myDatabase; to other classes
Basically what i need is two component names right ?
I think there exist a way to define delegate inside the components.xml so that when i inject the myDatabase component i should get hibernate session.But i realy interest to here from someone thinking what is a good design for this.
What should I change in the current xml above ?
Thanks.
-
3. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 9, 2009 2:37 AM (in response to itays100)
Itay Sahar wrote on Nov 07, 2009 19:08:
Yeh well so you agree with me that
Seam Security with hibernate session won't work.I am not going to argue semantics, but it is called JpaIdentityStore, you can't really expect it to work without JPA (EntityManager)...
I guess I need to clarify myself:
I need to know how to configure the component.xml in order to support both seam security and hibernate session.
The functionalities i want to achieve are 2:
1. inject @In private EntityManager entityManager;
to my component that handle user accounts.
2. inject @In private Session myDatabase; to other classes
Basically what i need is two component names right ?
I think there exist a way to define delegate inside the components.xml so that when i inject the myDatabase component i should get hibernate session.Technically, having both SMPC and SMHS (Hibernate session) should work, as long as you only use the EntityManager in the JpaIdentityStore.
But still probably not a good idea. So, as I said, define a SMPC, use a factory method to get the Session:<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/myapp/EntityManagerFactory" /> <factory name="mySession" scope="STATELESS" auto-create="true" value="#{entityManager.delegate}" />
Then you can use in your DAO, etc. classes.
@In Session mySession;
HTH
-
4. Re: Seam Security with hibernate session won't work.
itays100 Nov 10, 2009 12:06 AM (in response to itays100)Things turn complicated. Please see current configuration:
By the way EntityManager is not required ejb container right?<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence- unit-jndi-name="java:comp/env/jdbc/TestDB" /> <factory name="bookingDatabase" scope="STATELESS" auto-create="true" value="#{entityManager.delegate}" /> <security:jpa-identity-store entity-manager="#{entityManager}" user-class="com.example.User" role-class="com.example.Role"/> <security:jpa-token-store token-class="com.example.security.AutoLoginToken"/> <security:remember-me mode="autoLogin"/> <transaction:hibernate-transaction session="#{bookingDatabase}"/>
Tomcat run but throw exception when connecting to home page:
SEVERE: swallowing exception java.lang.IllegalStateException: Could not start transaction at org.jboss.seam.jsf.SeamPhaseListener.begin(SeamPhaseListener.java:598) at org.jboss.seam.jsf.SeamPhaseListener.begin(SeamPhaseListener.java:583) at org.jboss.seam.jsf.SeamPhaseListener.handleTransactionsBeforePhase(SeamPhaseListener.java:327) at org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:144) at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:118) at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:214) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83) at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178) at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290) at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390) at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517) at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:595) Caused by: java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241) at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78) at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144) at org.jboss.seam.Component.callComponentMethod(Component.java:2249) at org.jboss.seam.Component.unwrap(Component.java:2275) at org.jboss.seam.Component.getInstance(Component.java:2041) at org.jboss.seam.Component.getInstance(Component.java:1983) at org.jboss.seam.Component.getInstance(Component.java:1977) at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55) at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50) at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:147) at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:51) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72) at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44) at org.jboss.el.parser.AstValue.getValue(AstValue.java:63) at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:112) at org.jboss.seam.Component.getInstanceFromFactory(Component.java:2068) at org.jboss.seam.Component.getInstance(Component.java:2011) at org.jboss.seam.Component.getInstance(Component.java:1983) at org.jboss.seam.Component.getInstance(Component.java:1977) at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55) at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50) at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:147) at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:51) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72) at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44) at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:112) at org.jboss.seam.transaction.HibernateTransaction.initSession(HibernateTransaction.java:69) at org.jboss.seam.transaction.HibernateTransaction.begin(HibernateTransaction.java:80) at org.jboss.seam.jsf.SeamPhaseListener.begin(SeamPhaseListener.java:593)
-
5. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 10, 2009 2:23 AM (in response to itays100)
Itay Sahar wrote on Nov 10, 2009 00:06:
Things turn complicated. Please see current configuration:It seems that java:comp/env/jdbc/TestDB is pointing to your data source, not at the entity manager factory. Since you are using Tomcat (the example I posted was for JBoss), you need something like:
<transaction:entity-transaction entity-manager="#{entityManager}"/> <persistence:entity-manager-factory name="myDatabase"/> <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{myDatabase}"/>
Do look at the JPA example in the Seam distribution for more details.
By the way EntityManager is not required ejb container right?No.
-
6. Re: Seam Security with hibernate session won't work.
itays100 Nov 10, 2009 11:46 PM (in response to itays100)I will try to give more details:
This is META-INF/context.xml:
<Context path="/myApp" docBase="myApp" debug="5" reloadable="true" crossContext="true"> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="1234" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql:///my_db"/> </Context>
This is hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="connection.datasource">java:comp/env/jdbc/TestDB</property> <property name="hbm2ddl.auto">create-drop</property> <!-- property name="hbm2ddl.auto">create</property--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="transaction.flush_before_completion">true</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
components.xml: (partial)
<persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{exampleEntityManagerFactory}"/> <persistence:entity-manager-factory name="exampleEntityManagerFactory" persistence-unit-name="myApp"/> <transaction:entity-transaction entity-manager="#{entityManager}"/>
Tomcat 6 log:
SEVERE: Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener org.jboss.seam.InstantiationException: Could not instantiate Seam component: exampleEntityManagerFactory at org.jboss.seam.Component.newInstance(Component.java:2144)
-
7. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 11, 2009 2:48 AM (in response to itays100)
Itay Sahar wrote on Nov 10, 2009 23:46:
I will try to give more details:
This is hibernate.cfg.xml:Since this is JPA, you need a persistence.xml in WEB-INF/classes/META-INF. Like I said, build the JPA example from the distribution for Tomcat, and look at the configuration for a working example.
-
8. Re: Seam Security with hibernate session won't work.
itays100 Nov 12, 2009 10:31 AM (in response to itays100)
you need a persistence.xml in WEB-INF/classes/META-INFIn the JPA example persistence.xml is in /META-INF and not WEB-INF/classes
For now i came with this configuraion:
The context.xml and persistence.xml are both in /META-INF.This is the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <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="bookingDatabase" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/> <!-- RESOURCE_LOCAL <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> --> <!-- alternative <property name="jboss.entity.manager.factory.jndi.name" value="java:/jpaBookingEntityManagerFactory"/> --> </properties> </persistence-unit> </persistence>
I tried also with no-jta-data-source.
And this is from WEB/components.xml:<persistence:entity-manager-factory name="bookingDatabase"/> <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{bookingDatabase}"/> <transaction:entity-transaction entity-manager="#{entityManager}"/>
And I'm not sure what about the WEB-INF/classes hibernate.cfg.xml. It include the mapping of the entities should this file be replcae by persistence.xml ? if yes where can i define the mapping for exp:
<session-factory> <mapping class="com.....User"/>
Tomcat 6 log:
12/11/2009 11:00:47 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener org.jboss.seam.InstantiationException: Could not instantiate Seam component: bookingDatabase at org.jboss.seam.Component.newInstance(Component.java:2144) at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304) at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278) at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:113) at org.jboss.seam.init.Initialization.init(Initialization.java:740) at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627) at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:578) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Caused by: java.lang.RuntimeException: exception invoking: startup at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:154) at org.jboss.seam.Component.callComponentMethod(Component.java:2257) at org.jboss.seam.Component.callCreateMethod(Component.java:2172) at org.jboss.seam.Component.newInstance(Component.java:2132) ... 29 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144) ... 32 more Caused by: java.lang.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:119) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) at org.jboss.seam.persistence.EntityManagerFactory.createEntityManagerFactory(EntityManagerFactory.java:81) at org.jboss.seam.persistence.EntityManagerFactory.startup(EntityManagerFactory.java:50) ... 38 more 12/11/2009 11:00:49 org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() 12/11/2009 11:00:49 org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized()
Thanks!
-
9. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 12, 2009 10:40 AM (in response to itays100)
Itay Sahar wrote on Nov 12, 2009 10:31:you need a persistence.xml in WEB-INF/classes/META-INF
In the JPA example persistence.xml is in /META-INF and not WEB-INF/classes
For now i came with this configuraion:
The context.xml and persistence.xml are both in /META-INF.Did you actuall build the example? Build it and check(explode) the WAR it produces.
And I'm not sure what about the WEB-INF/classes hibernate.cfg.xml. It include the mapping of the entities should this file be replcae by persistence.xml ? if yes where can i define the mapping for exp:If you are using annotations, you don't need it. And yes, you can list entities in persistence.xml (you shouldn't need to, though).
Caused by: java.lang.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:119)
You are missing a some jar file Hibernate needs. Build the example, deploy it and modify as needed to get started. That would make things a lot easier.
-
10. Re: Seam Security with hibernate session won't work.
itays100 Nov 12, 2009 10:54 AM (in response to itays100)I found seam examples difficult to build. I succeeded building the hibernate example.
As to the missing jar, what does it mean in persistence.xml:<org.hibernate.ejb.HibernatePersistence.
I don't want to use jboss embedded or ejb. Should I replace it to somthing else ?
Do you have the booking example on jpa that i could use that will work on tomcat. -
11. Re: Seam Security with hibernate session won't work.
itays100 Nov 12, 2009 7:28 PM (in response to itays100)Ok. so i built the jpa example war and delpoy on tomcat 6. the application is running!
But what is the best and fast way to create eclipse project from this archive.
I would like to run ant from eclipse on 1 build file and not 3.Thanks.
-
12. Re: Seam Security with hibernate session won't work.
kapitanpetko Nov 13, 2009 2:45 AM (in response to itays100)
Itay Sahar wrote on Nov 12, 2009 19:28:
Ok. so i built the jpa example war and delpoy on tomcat 6. the application is running!
But what is the best and fast way to create eclipse project from this archive.Unfortunately, there is no easy/fast way to do this. Your best bet is to 'Java Project from Existing Ant Buildfile' in Eclipse.