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 dataCode:
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 followsCode:
<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.