-->
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.  [ 4 posts ] 
Author Message
 Post subject: problem with primary key values in hibernate
PostPosted: Thu Feb 07, 2008 8:56 am 
Newbie

Joined: Thu Feb 07, 2008 5:40 am
Posts: 2
Location: pune
Hello Friends I am new to hibernate I have taken this example from hibernate quickely, I am using oracle 10 g. I have a problem when I run the EventLoader It inserts the values into database but the values inserted into to the database are unpredictab.

Explanation of problem is as follows:
there are two tables
1. Events
2. Locations

Event contains an uid field which is pk.
and Location contains an iid field which is pk

when I insert the values using the hibernate it inserts values into database as
for first insert row uid=1 and iid=2
for second insert row uid=3, and iid=4 and so on

but when I used the mysql this is working fine
what is problem with oracle

the following is the source code.

Location Class
import java.io.Serializable;
public class Location implements Serializable
{
private Long id;
private String name;
private String address;

public void setId(Long id){
this.id = id;
}

public void setName(String name){
this.name = name;
}

public void setAddress(String address){
this.address = address;
}

public Long getId(){
return this.id;
}

public String getName(){
return this.name;
}

public String getAddress(){
return this.address;
}
}

event Class

package com.manning.hq.ch01;

import java.io.Serializable;
import java.util.Date;

public class Event implements Serializable
{
private Long id;
private int duration;
private String name;
private Date startDate;
private Location location;

public Long getId(){
return this.id;
}

public void setId(Long id){
this.id = id;
}

public int getDuration(){
return this.duration;
}

public void setDuration(int duration){
this.duration = duration;
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public Date getStartDate(){
return this.startDate;
}

public void setStartDate(Date startDate){
this.startDate = startDate;
}

public Location getLocation(){
return this.location;
}

public void setLocation(Location location){
this.location = location;
}
}


EventLoader Class

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import com.manning.hq.ch01.Location;

public class EventLoader
{
public static void main(String[] args)
{
Location location = new Location();

location.setName("Nitin N Kalaskar");
location.setAddress("At post Nagapur dist beed");

Event event = new Event();

event.setName("Annual Meeting");
event.setDuration(60);
event.setStartDate(createDate(2008,2,7));
event.setLocation(location);

Session session = null;
Transaction tx = null;
SessionFactory sessionFactory = null;
try
{
Configuration configuration = new Configuration();
configuration.configure();

sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(location);
session.save(event);
// Use cascading to save the location


session.flush();
tx.commit();
System.out.println("Event and location saved!");
}
catch (HibernateException e)
{
try
{
if(tx != null) {
tx.rollback();
}
}
catch (HibernateException ignore)
{
//ignore
}
throw e;
}
finally
{
if(session == null)
{
try
{
session.close();
}
catch (HibernateException ignore)
{
//ignore
}
}
if(sessionFactory != null){
try
{
sessionFactory.close();
}
catch (HibernateException ignore)
{
//ignore
}
}
}
}
private static Date createDate(int year, int month, int day){
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTime();
}
}[/quote]

_________________
Thanks and regards
Nitin Kalaskar
E-Mail: nitinkalaskar2007@hotmail.com
Software Developer
Perennialsys pune(India).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 1:10 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Since the values you refer to are primary keys, the way in which they are generated depends on what generator mechanism you specified in the "id" section on your mapping file.
In your case, the mechanism would seem to be an Oracle sequence shared by both entities.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: Thanks for reply
PostPosted: Fri Feb 08, 2008 2:06 am 
Newbie

Joined: Thu Feb 07, 2008 5:40 am
Posts: 2
Location: pune
gonzao_diaz wrote:
Since the values you refer to are primary keys, the way in which they are generated depends on what generator mechanism you specified in the "id" section on your mapping file.
In your case, the mechanism would seem to be an Oracle sequence shared by both entities.


Hello gonzao_diaz
I got the problem, thanks for your reply.

I have specified generated as native, and when I run the ant task with schema export it creates a sequence, now I have a question in my mind how to create more than one sequence useing schema-export task, please help me.

Thanks and regards
Nitin Kalaskar

_________________
Thanks and regards
Nitin Kalaskar
E-Mail: nitinkalaskar2007@hotmail.com
Software Developer
Perennialsys pune(India).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 08, 2008 11:38 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
With "native", hibernate chooses what generation method to apply, depending on the capabilities of the underlying database. (in your case, a database sequence).
If you want it specifically to be a sequence, try "sequence", specifying a different sequence name inside each node

http://www.hibernate.org/296.html

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.