Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By rcclark

Thread: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

  1. #1
    rcclark is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Question No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    I am new to Spring and am trying to go through 2 books and online help to get it set up to access the database but my page keeps giving me the above error. Can someone please help me identify what I have configured incorrectly?

    The full error is:
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0

    Thank you for your help.

    Ray

    The datasource is defined as this:

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    Index of /schema/context
    http://www.springframework.org/schem...ontext-3.0.xsd
    ">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>
    <context:property-placeholder location="/WEB-INF/jdbc.properties" />
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>
    </beans>

    The applicationContext is this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...ring-beans.xsd
    Index of /schema/context
    http://www.springframework.org/schem...ng-context.xsd
    Index of /schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <mvc:view-controller path="/index.html" />

    <bean name="helloService" class="dvdTracker.service.HelloService" />

    <bean id="dvdDao" class="dvdTracker.repository.DvdDaoImpl">
    <property name="dataSource" ref="dataSource" />
    </bean>
    </beans>

    The DvdDaoImpl class is this:

    package dvdTracker.repository;

    import dvdTracker.domain.Dvd;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.namedparam.NamedPara meterJdbcTemplate;
    import org.springframework.jdbc.core.simple.Parameterized RowMapper;

    public class DvdDaoImpl implements DvdDao {

    private JdbcTemplate jdbcTemplate;
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }
    ...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    rcclark is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Question Re: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    I see, I missed the code tag. Thank you. Here is my post again:

    I am new to Spring and am trying to go through 2 books and online help to get it set up to access the database but my page keeps giving me the above error. Can someone please help me identify what I have configured incorrectly?

    The full error is:
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0

    Thank you for your help.

    Ray

    The datasource is defined as this:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            ">
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <context:property-placeholder location="/WEB-INF/jdbc.properties" />
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    </beans>
    The applicationContext is this:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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/mvc
                               http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:annotation-driven />
        
        <mvc:view-controller path="/index.html" />
    
        <bean name="helloService" class="dvdTracker.service.HelloService" />
    
        <bean id="dvdDao" class="dvdTracker.repository.DvdDaoImpl">
            <property name="dataSource" ref="dataSource" />
        </bean>
    </beans>
    The DvdDaoImpl class is this:

    Java Code:
    package dvdTracker.repository;
    
    import dvdTracker.domain.Dvd;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import javax.sql.DataSource;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    
    public class DvdDaoImpl implements DvdDao {
    
        private JdbcTemplate jdbcTemplate;
        private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
        
        public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
            this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        }
        
        @Override
        public List<Dvd> getDvdList() {
            String sql = "select idDVD, title, dvdLength from dvd";
            List<Dvd> dvds = this.jdbcTemplate.query(sql, new TransactionMapper());
            return dvds;
        }
    ...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    What's the full stack trace?
    That error is usually the cause of another one (initialising another object), so there should be a reasonably big stack trace which would tell you what it was trying to create.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    rcclark is offline Member
    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Default Re: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    Thank you. I was copying an example from a book and mistakenly left in the JPA filter in the web.xml file. Here was the code that I needed to remove:

    Java Code:
        <filter>
            <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
            <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
        </filter>
    DarrylBurke likes this.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: No unique bean of type [javax.persistence.EntityManagerFactory] is defined

    Thanks for posting your solution.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 11-09-2011, 01:47 PM
  2. Error: The Type Property Already Defined
    By cest.lavie16 in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 01:32 AM
  3. Replies: 1
    Last Post: 03-23-2011, 07:55 AM
  4. Replies: 7
    Last Post: 08-22-2010, 11:51 AM
  5. Replies: 4
    Last Post: 03-24-2010, 12:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •