-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JPA & Oracle Sequence not increasing proparly.
PostPosted: Wed Oct 31, 2007 3:22 am 
Newbie

Joined: Tue Oct 30, 2007 5:08 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.5

Mapping documents: Hibernate Annotations 3.3

Name and version of the database you are using: Oracle 10g



Dear All,
I am having a problem using JPA and Oracle Sequence. Actually my application works fine. But when I restart my application server the sequecnce value is increased by either 50,100 or 1000 also. But once my server starts it works fine I mean the values are 50,51,52,53 again if I restart the server then I get 100 or 151 or 1000 as my sequence value. So I am not getting that what goes wrong. The entity and the dao class are as follows.

Entity class
Code:
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity(name="COMPANYDETAILS")
public class VendorDetailsVO {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_ADDVENDORID")
   @SequenceGenerator(name="SEQ_ADDVENDORID",sequenceName="SEQ_ADDVENDORID")
   @Column(name="COMPANYID")
   private long id;
   
   @Column(name="COMPANYTYPEID")
   private long compTypeId;
   
   @Column(name="REGOFFICEADD")
   private String regOfficeAdd;
   
   @Column(name="CITY")
   private String city;
}


the DAO class where i persists the data
Code:
import org.mkcl.tps.common.vo.vendor.VendorDetailsVO;
import org.mkcl.tps.dao.vendor.interfaces.IAddVendorDao;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


public class AddVendorDaoImpl implements IAddVendorDao{
   private EntityManager entityManager;
   
   @Transactional
   public int insertNewVendor(VendorDetailsVO vendor) {
            
      try{
         vendor.setId(0);
         entityManager.persist(vendor);
         entityManager.flush();
      }catch(Exception e){
         System.out.println("Problem in insetion of new Vendor : "+e);
         entityManager.refresh(vendor);
         return 0;
      }finally{
         try{
            entityManager.close();
         }catch(Exception e){
            System.out.println("Problem in clossing the Entity Manager : AddVendor : "+e);
         }
      }
      return 1;
   }

   public EntityManager getEntityManager() {
      return entityManager;
   }

   @PersistenceContext
   public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
   }
}


I am using spring for Dependancy Injection so snippet of applicationContext.xml is as follows.
Code:
bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="ORACLE"/>
            <property name="showSql" value="true"/>
         </bean>
      </property>
   </bean>
   
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"></property>
   </bean>
   
   <tx:annotation-driven transaction-manager="transactionManager"/>


and the persistence.xml is as follows
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
   <persistence-unit name="userPersistenceUnit"/>
</persistence>


Thank you very much in advance.

_________________
With Best Regards,
Ishaan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 4:01 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

In the SequenceGenerator annotation add the following

Code:
allocationSize=1, initialValue=1

You can leave out the initialValue if you like, the allocationSize is the important thing.


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 6:00 am 
Newbie

Joined: Tue Oct 30, 2007 5:08 am
Posts: 10
Dear Andy.

Thank you very much now its working fine. :-)

_________________
With Best Regards,
Ishaan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.