Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: JPA not storing entity in database

  1. #1
    Join Date
    Dec 2010
    Posts
    175

    Question JPA not storing entity in database

    Hi,

    I've defined persist method in GenericDAO as follows:
    Code:
    	@Transactional(readOnly = false)
    	public void persist(T entity) {
    		entityManager.persist(entity);
    	}
    However, when I try to persist the entity a sequence gets generated as shown below but entity is not persisted to the database?

    Any idea, why this is happening???
    Code:
    DEBUG org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
    DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp:
    DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
    DEBUG org.hibernate.SQL - select PRODUCT_ID_SEQ.nextval from dual
    DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Long[1004]]
    DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
    DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
    DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 50200, using strategy: org.hibernate.id.SequenceHiLoGenerator
    DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager

  2. #2
    Join Date
    Dec 2010
    Posts
    175

    Default

    Here is how transactionManager and entityManager are defined:

    Code:
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    		
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    		</property>
    		<property name="persistenceUnitName" value="myPersistenUnit"/>
    	</bean>

  3. #3
    Join Date
    Dec 2010
    Posts
    175

    Default

    Okay. I did added some more code to test if entity is persisted or not? as follows:

    Code:
    	@Transactional
    	public void persist(T entity) {
    		if(!entityManager.contains(entity))
    			System.out.println("Entity is not persisted yet, will persist now!");
    		entityManager.persist(entity);
    		if(entityManager.contains(entity))
    			System.out.println("persisted entity is - " + entity);
    		else
    			System.out.println("Entitiy is still not persisted after call to persist()");
    	}
    Here is the output that I get:
    Code:
    Entity is not persisted yet, will persist now!
    Hibernate: select PRODUCT_ID_SEQ.nextval from dual
    Entitiy is still not persisted after call to persist()
    This means, it is not doing anything when persist() is called on entityManager and that's why there are no INSERT statements present in the log file?

  4. #4
    Join Date
    Dec 2010
    Posts
    175

    Default

    did anyone encountered such issue????

  5. #5
    Join Date
    Dec 2010
    Posts
    175

    Default

    When I added entityManager.flush() to the persist method it throws following exception
    Code:
    	@Transactional(propagation = Propagation.REQUIRED)
    	public void persist(T entity) {
    		entityManager.persist(entity);
    		entityManager.flush();
    	}
    
    Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
    	at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)

  6. #6
    Join Date
    Dec 2010
    Posts
    175

    Default

    Thank you for looking into it. Issue is resolved finally.

  7. #7
    Join Date
    May 2011
    Posts
    1

    Default

    Hi,

    I want to ask you about this issue.

    How did you solve it ?

    Can you explain that ?

    Tks

  8. #8

    Default

    Quote Originally Posted by tiger.spring View Post
    Thank you for looking into it. Issue is resolved finally.
    hi...can you let me know how you got it resolved...I am also getting an issue related to persisting record

    Thanks
    Saikiran

  9. #9
    Join Date
    Dec 2010
    Posts
    175

    Default

    Apologies, for delayed response. I had to meet some deadline.

    Anyway, as you can see from the error (posted above in my earlier post) that in my case the there were no transactions in progress. Which means the transaction manager was not being applied/initialized and hence nothing was happenning.

    If you can post your configurations, may be I will be able to provide some help. Please post your web.xml and any relevant spring configuration.

  10. #10
    Join Date
    Feb 2011
    Posts
    4

    Default

    hi,

    i have the same problem

    my configurations are
    Code:
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaDialect" ref="jpaDialect" />
    	</bean>
    
    	<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://172.16.3.25:3378/ISSYSDB" />
    		<property name="username" value="ist2admin" />
    		<property name="password" value="Admin%kbc55B1" />
    		<property name="maxIdle" value="0" />
    		<property name="minIdle" value="0" />
    		<property name="testOnBorrow" value="true" />
    		<property name="testOnReturn" value="true" />
    		<property name="validationQuery" value="select now()" />
    	</bean>
    
    		<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="persistenceUnitName" value="application" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="false" />
    				<property name="database" value="MYSQL" />
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
    			</bean>
    		</property>
    		<property name="loadTimeWeaver">
    			<bean
    				class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    		</property>
    
    	</bean>
    thnx in advance,

    shady,

Posting Permissions

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