Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.0, jboss 4.3 and JPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.0, jboss 4.3 and JPA

    Hi,

    I'm a newbie to spring. I need to configure my database using jndi. It is a simple web application that will be deployed in JBOSS 4.3

    I get the following exception,
    Code:
    ERROR [DispatcherServlet] Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManagementServiceImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
            at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
            ... 105 more
    Here is my entity, a simple pojo
    Code:
    package com.pat.form;
    
    import java.util.ArrayList;
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    
    
    @Entity
    @Table(name = "USER")
    public class User {
    	private String userName;
    	private String password;
    	private String firstName;
    	private String lastName;
    	private String email;
    	private String phoneNumber;
    	
    	
    	@Column(name="USERNAME")
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	@Column(name="PASSWORD")
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	@Column(name="FIRSTNAME")
    	public String getFirstName() {
    		return firstName;
    	}
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	@Column(name="LASTNAME")
    	public String getLastName() {
    		return lastName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	@Column(name="EMAIL")
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	@Column(name="PHONE_NUMBER")
    	public String getPhoneNumber() {
    		return phoneNumber;
    	}
    	public void setPhoneNumber(String phoneNumber) {
    		this.phoneNumber = phoneNumber;
    
    
    	public void setUserID(String s) {
    		userID = s;
    	}
    
    	@Id
    	@Column(name="USERID")
    	public String getUserID() {
    		return userID;
    	}
    
    }
    Here is my DAO/Service
    Code:
    package com.pat.service;
    
    import com.pat.User;
    
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Repository
    @Service
    public class UserManagementServiceImpl implements IUserManagementService{
    	
    	private  EntityManager entityManager;
    	
    	   @PersistenceContext 
    	    public void setEntityManager(EntityManager entityManager) {  
    
    	        this.entityManager = entityManager;  
    
    	    } 
    
    	  public boolean isUserAlreadyExists(User user){
    		     //some logic
    		  return false;
    		  }
    	
    	public List<User> findAllUsers() {
    		return entityManager.createNativeQuery("select o from User o",
    				User.class).getResultList();
    	}
    	
    	@Transactional
    	//@PreAuthorize("hasRole('RIGHT_CREATE')")
    	public void create(User user) {
    		this.entityManager.persist(user);
                  }
    }
    Here is my applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:sec="http://www.springframework.org/schema/security"
    	xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    		http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:property-placeholder location="classpath*:WEB-INF/classes/*.properties"/>
       
       <context:annotation-config/>
       
        <context:component-scan base-package="com.pat">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
        
        <bean id="usrMgmtSrvc" class="com.pat.service.UserManagementServiceImpl">
    <property name="entityManagerFactory" value="javax.persistence.EntityManagerFactory"></property>  
    </bean>
       	<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
             <property name="showSql" value="true"></property>
             <property name="database" value="SYBASE"></property>         
        </bean>
        
    	<jee:jndi-lookup id="jndiEntityManagerFactory" 
    	                 jndi-name="persistence/MyPU" 
    	                 proxy-interface="javax.persistence.EntityManagerFactory" 
    	                 lookup-on-startup="false"/>
    	
    	 <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" lazy-init="true"> 
    		<property name="transactionManagerName" value="java:/TransactionManager" />
         </bean>
         
         <tx:annotation-driven transaction-manager="jtaTransactionManager" /> 
    </beans>
    Here is my persistence.xml
    Code:
    <?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" 
                 version="2.0" 
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    
       <persistence-unit name="MyPU" transaction-type="JTA">
            <jta-data-source>java:/jdbc/SybaseDS</jta-data-source>
            <class>com.pat.form.User</class>
            <properties>
            <!-- <property name="jboss.entity.manager.jndi.name" value="java:/jdbc/SybaseDS"/> -->
              <property name="jboss.entity.manager.factory.jndi.name" value="persistence/MyPU"/>
              <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect"/>
              <property name="hibernate.connection.autocommit" value="false"/>
              <property name="hibernate.show.sql" value="true"/>
            </properties>
        </persistence-unit>    
    </persistence>
    Please help me resolve this issue.
Working...
X