Announcement Announcement Module
Collapse
No announcement yet.
Spring + JPA . Data not getting persisted in database. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + JPA . Data not getting persisted in database.

    Hello all
    I have seen many threds on this forum for the same topic and I tried to follow almost all of them thugh it did not work fo rme. Can anyone please help me to understand why my data is not getting persisted into database ?
    Below are my configurations.

    application-context.xml

    Code:
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
            <property name="driverClassName" value="${database.driverClassName}"/>
            <property name="url" value="${database.url}"/>
            <property name="username" value="${database.username}"/>
            <property name="password" value="${database.password}"/>
            <property name="initialSize" value="5"/>
            <property name="testWhileIdle" value="true"/>
            <property name="timeBetweenEvictionRunsMillis" value="50000"/>
            <property name="removeAbandoned" value="true"/>
            <property name="removeAbandonedTimeout" value="1200"/>
            <property name="validationQuery" value="select 1 from dual"/>
            <property name="minIdle" value="1"/>
        </bean>
       
         <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
      
      <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
      
    	  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            <property name="jpaDialect" ref="jpaDialect"/>
            <property name="persistenceUnitName" value="coimPU"/>
        </bean>
        
    	 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    	 
    	 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="dataSource" ref="dataSource"/>
        </bean>
            <tx:annotation-driven />
           
             <context:annotation-config/>
    	 
    	 
    	  <bean id="mybean" class="com.barcap.dms.connect.hibernet.entity.DocSourceMaster" />
    My entitybean is

    Code:
    
    
    	
    	
    
    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.persistence.*;
    
    
    
        @Repository
        @Entity
        @Table(schema="COIM_OWNER", name="COB_DOCUMENT_SOURCE_MASTER")
    	public class DocSourceMaster {
        	
        	
    	    private transient EntityManager entityManager;
        	
        	
        	
    		public EntityManager getEntityManager() {
    			return entityManager;
    		}
            
    		@PersistenceContext
    		public void setEntityManager(EntityManager entityManager) {
    			this.entityManager = entityManager;
    		}
    
    
    
    
    		@Id
    	    @GeneratedValue(strategy = GenerationType.AUTO)
    	    @Column(name = "id")
    	    private Long documentId;
    		
    		
    		
    		
    		
    		@Column(name="SOURCE")
    	    private String Source;
    		
    		
    		@Column(name="DOC_REPO")
    	    private String repositoryType;
    
    	   
    		
    	    
    
    		public Long getDocumentId() {
    			return documentId;
    		}
    
    		public void setDocumentId(Long documentId) {
    			this.documentId = documentId;
    		}
    
    		public String getSource() {
    			return Source;
    		}
    
    		public void setSource(String source) {
    			Source = source;
    		}
    		public String getRepositoryType() {
    			return repositoryType;
    		}
    
    		public void setRepositoryType(String repositoryType) {
    			this.repositoryType = repositoryType;
    		}
    		
    		@Transactional(rollbackFor=Exception.class)
    	    public void persist() {
    	        if (this.entityManager == null)
    	        	this.entityManager = entityManager();
    	        DocSourceMaster ob = new DocSourceMaster();
    	        ob.setRepositoryType("Internal");
    	        ob.setSource("ASA");
    	        this.entityManager.persist(ob);
    	        
    	      
    	    }
    
    		
    	   
    		
    
    		
    		
    
         }
    And here is my main method.

    Code:
    public static void main(String args[]) {
    		
    		springUtil = SpringInitiator.getInstance().getSpringUtil();
    		
    		
    		// Sample code for DB access
    		DocSourceMaster dsm = (DocSourceMaster)springUtil.getBeanByName("samir");
    		dsm.setSource("mybean");
    		dsm.setRepositoryType("internal");
    		dsm.persist();
    		
    	 
    
    	}
    Thanks in advance
    Samir
Working...
X