GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Hibernate-Oracle Sequence... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate-Oracle Sequence..." Watch "Hibernate-Oracle Sequence..." New topic
Author

Hibernate-Oracle Sequence...

Konda Golamaru
Ranch Hand

Joined: Dec 12, 2005
Posts: 36
Hi,

I have created a oracle sequence(hibernate_seq) with start value 100.
And I have the below mapping in my .hbm.xml file to get the next sequence
value.

When I test using JUnit, I get the applicationId=1 where I would expect
applicationId to be 100 which is the current sequence value.

When I execute teh Junit test script 3 times, instead of creating 3 records,
the same record is overridden and the applicationId has the value 1.

Please let me know if I am doing some thing wrong?

<id name="applicationId" column="APPLICATION_ID" type="long" unsaved-
value="0">
<generator class="sequence">
<param name="sequence">hibernate_seq</param>
</generator>
</id>
Konda Golamaru
Ranch Hand

Joined: Dec 12, 2005
Posts: 36
I have the below JUnit test method.
When I execute the command: ant test -Dtestcase=ApplicationDAO, How come only one record being created in the database table though
I execute the command more than one time. Each time the applicationId is set to : 1.

Does not it create a record for each execution? Please help.

public void testSaveApplication() throws Exception{
//Configuration configuration = new Configuration(); // configuring hibernate
SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory") ;

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

application=new Application();
application.setApplicationNumber("123");
application.setApplicationTitle("test_title");
application.setApplicationTypeId("8");
application.setMarketedName("marketed name");
application.setRenewalCycle("renewal cycle");
application.setComments("comments");
application.setInitialSubmissionId("12345678");
application.setAssociateApplicationId("81");
application.setLockedBy("Locked By");
application.setCreatedBy("spring");
application.setCreatedOn("31-JULY-2008");
application.setLastModifiedBy("spring");
application.setLastModifiedOn("31-JULY-2008");
application.setProcedureId("1");
application.setAtcCode("811");
application.setFilingAgencyId("4");
application.setParentNationalApplicationId("5");
application.setInitialApplicationSubTypeId("6");
application.setLockedBySessionId("Lock by session id");
application.setAppMedicinalProducts("6 mg");
application.setAssociateApplicationId("9");
application.setPlannedSubmissionsCount("5");
application.setActualSubmissionsCount("6");


dao.saveApplication(application);


assertNotNull("primary key assigned",application.getApplicationId());
log.info(application);
assertNotNull(application.getApplicationNumber());

log.debug("end of testSaveApplication");

Application app=dao.getApplication(application.getApplicationId());
log.debug(" app : "+app);

tx.commit();
session.flush();
session.close();
}
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
Would you know if you have the hbm ddl property set to create-drop?


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Konda Golamaru
Ranch Hand

Joined: Dec 12, 2005
Posts: 36
Hi Paul,

Thanks for your reply.

Please find the below session factory mapping info for the property mapping.
I have the ddl property set to create. Please let me know if you see any issues/errors with the mapping.


<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
Konda Golamaru
Ranch Hand

Joined: Dec 12, 2005
Posts: 36
Hi Paul,

Finally It works as I expected. I have changed the hbm ddl property value from create to update and it works.

<prop key="hibernate.hbm2ddl.auto">update</prop>


Now, it picks the next sequence value and also It creates the new record for each JUnit execution.

Your reply helped me to dig further on the issue and finally here we are.

Thanks again.
Eduardo F. Sandino
Greenhorn

Joined: Dec 10, 2009
Posts: 7
Hi Konda, I have a problem with the Sequences in Oracle, I am not using triggers + sequences in Oracle DB, only sequences, and i have defined my entities using hbm.xml files.

The problem I have is that the sequence doesn't start using the value defined in DB, and when the Application is running It creates records with Id=50, the next time I start the Oracle Server It start records are created using ID=100 and increments.. I really don't figure out, What is the problem ? Do you have any Idea? I have left part of my code here...

The Id's finally are generated and save in db, but I want to have a normal flow... 1,2,3,4,5, N and actually I get.. 50,51,52, restart oracle and Application and then.. 50,51,52,53... other restart more and I get .. 100,101,102,103...

Any Idea ?

regards

Eduardo





And the generated Java Entity is:




Konda Golamaru wrote:Hi Paul,

Finally It works as I expected. I have changed the hbm ddl property value from create to update and it works.

<prop key="hibernate.hbm2ddl.auto">update</prop>


Now, it picks the next sequence value and also It creates the new record for each JUnit execution.

Your reply helped me to dig further on the issue and finally here we are.

Thanks again.
 
GeeCON Prague 2014
 
subject: Hibernate-Oracle Sequence...