0

I've checked a lot of previous questions in StackOverflow but neither of them solved my problem.

I have a Spring + JSF + JPA (EclipseLink) application that is deployed on Glassfish and now I want my application server to manage connection with database (PostgreSQL). In an admin panel, I have set JDBC Connection Pool as well as JDBC Resources and I am able to ping my database from within Glassfish admin panel. However, I don't know how to configure my app properly to work with this new configuration. How should my applicationContext look like?

(Edited after gerrytan's answer and after I read Exception while creating EntityManagerFactory - Lookup failed for 'persistence/myPU' in SerialContext ):

My persistence.xml looks like this:

<persistence-unit name="persistenceUnit">
    <jta-data-source>jdbc/prawalewa</jta-data-source>
    <class>com.blogspot.prawalewa.model.AdditionalInfoEntity</class>
    <class>com.blogspot.prawalewa.model.ArticleEntity</class>
    <class>com.blogspot.prawalewa.model.ArticleFrameEntity</class>
    <class>com.blogspot.prawalewa.model.ChangeEntity</class>
    <class>com.blogspot.prawalewa.model.CommentEntity</class>
    <class>com.blogspot.prawalewa.model.ContentEntity</class>
    <class>com.blogspot.prawalewa.model.HeaderEntity</class>
    <class>com.blogspot.prawalewa.model.ImageEntity</class>
    <class>com.blogspot.prawalewa.model.MessagerEntity</class>
    <class>com.blogspot.prawalewa.model.PointsBucketEntity</class>
    <class>com.blogspot.prawalewa.model.PointsTypedEntity</class>
    <class>com.blogspot.prawalewa.model.PointsTypedPointsBucketEntity</class>
    <class>com.blogspot.prawalewa.model.RankEntity</class>
    <class>com.blogspot.prawalewa.model.RankTypeEntity</class>
    <class>com.blogspot.prawalewa.model.TagEntity</class>
    <class>com.blogspot.prawalewa.model.UserEntity</class>
    <class>com.blogspot.prawalewa.model.UserCookieEntity</class>
    <class>com.blogspot.prawalewa.model.VoteEntity</class>


</persistence-unit>

And here is a skeleton of applicationContext (that's the biggest problem - not really sure how to change it):

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

<context:component-scan base-package="com.blogspot.prawalewa" />
<jee:jndi-lookup id="prawalewa" jndi-name="jdbc/prawalewa"/>
</beans>

And my web.xml:

 <persistence-unit-ref>
   <persistence-unit-ref-name>persistence/persistenceUnit</persistence-unit-ref-name>
    <persistence-unit-name>persistenceUnit</persistence-unit-name>
</persistence-unit-ref>

And my way of exemplar Dao implementation looks like:

public class UserDaoImpl implements UserDao {

protected EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}
@PersistenceContext(unitName = "persistenceUnit")
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}


@Override
public void addUser(UserEntity userEntity) {
    getEntityManager().persist(userEntity);

}

@Override
public void removeUser(int userId) {
    Query query = getEntityManager().createQuery(
            "DELETE FROM UserEntity u WHERE u.id = :userid");
    query.setParameter("userid", userId);
}
}

And that was all. Now tried to change ApplicationContext in several ways but without success. Is there anyone who could tell me how to change it properly or at least where to begin with it? Or maybe I need to get rid of this and configure application now in a different way? Thank you in advance for your help. Much appreciated.

EDIT: After reading Exception while creating EntityManagerFactory - Lookup failed for 'persistence/myPU' in SerialContext I edited it a little just like you can see above, but still I got two exceptions now while deploying:

java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.blogspot.prawalewa.user.bo.RegistrationBo com.blogspot.prawalewa.user.bean.RegistrationBean.registrationBo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationBoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.blogspot.prawalewa.user.dao.UserDao com.blogspot.prawalewa.user.bo.impl.RegistrationBoImpl.registrationDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistenceUnit' is defined

Where and what should I put it in applicationContext.xml?

1 Answer 1

1

Since you wanted app server to manage database connection (and entity managers I presume) creating BasicDataSource and LocalContainerEntityManagerFactoryBean is wrong / unsuitable.

I would recommend using JNDI lookup. You can obtain EntityManagerFactory from JNDI like this

<jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/>

Read more about this on Spring Manual: 15.5.1 Three options for JPA setup in a Spring environment

As a personal perference I tend to go with low feature container like tomcat when using Spring -- hence less duplicates and fine tuning. I'd rather go full Java-EE w/o Spring for full feature container like Glassfish.

2
  • Thanks for answer gerrytan. So you're saying that my applicationContext.xml should only look like this now: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=..."> <context:component-scan base-package="com.blogspot.prawalewa" /> <jee:jndi-lookup id="prawalewa" jndi-name="jdbc/prawalewa"/> </beans> ? What about TransactionManager, EntityManagerFactory and so on? Is it out of the box? Nov 15, 2013 at 12:20
  • Actually, I edited my post above as now I have a problem with obtaining sessionFactory (despite reading Spring manual about it...), if you could have a look, I would be more than gratful :) Nov 15, 2013 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.