-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite-id with sequence
PostPosted: Tue Mar 09, 2004 11:45 am 
Newbie

Joined: Thu Feb 12, 2004 10:10 am
Posts: 6
Hi all,
I'm using hibernate 2.1.1 and Oracle 9.2.
I created a mapping for a table with with 3 key-property: 2 strings and an Integer, as a sequence.
I read this topic http://forum.hibernate.org/viewtopic.php?t=927648 and i follow your tips, but it still doesn't work.

Here is my mapping:

<class name="Visit" table="visit">
<composite-id name="id" class="CompositeIDVisit" unsaved-value="any">
<key-property name="trialTipNum" column="tip_num" type="string"/>
<key-property name="centreCode" column="codice_centro" type="string" />
<key-property name="visitId" column="id_visita" type="java.lang.Integer"/>
</composite-id>


*********************************
I created the bean Visit:

public class Visit
{
private CompositeIDVisit id;
private String trialTipNum;
private String centreCode;
private Integer visitId;

public Visit(CompositeIDVisit compositeId)
{
this.setId(compositeId);
}

public CompositeIDVisit getId()
{
return id;
}

public void setId(CompositeIDVisit id)
{
this.id = id;
this.setCentreCode(id.getCentreCode());
this.setTrialTipNum(id.getTrialTipNum());
this.setVisitId(id.getVisitId());
}

/*
...
setter and getter for the propertis triaTipNum., centreCode, visitId...

overriding of equals and hashCode methods...
...
*/
}

*********************************
I've created the composite id class:

import java.io.*;
public class CompositeIDVisit implements Serializable
{
private String centreCode;
private String trialTipNum;
private Integer visitId;

public String getTrialTipNum()
{
return trialTipNum;
}

public String getCentreCode()
{
return centreCode;
}

public void setVisitId(Integer visitId)
{
this.visitId = visitId;
}

public void setTrialTipNum(String trialTipNum)
{
this.trialTipNum = trialTipNum;
}

public void setCentreCode(String centreCode)
{
this.centreCode = centreCode;
}

public Integer getVisitId()
{
return visitId;
}

public boolean equals(Object obj)
{
CompositeIDVisit v = (CompositeIDVisit) obj;
if(this.centreCode.equals(v.getCentreCode()) &&
this.trialTipNum.equals(v.getTrialTipNum()) &&
this.visitId.equals(v.getVisitId()))
return true;
return false;
}

/**
* Returns a hash code value for the object.
*
* @return a hash code value for this object.
* @todo Implement this java.lang.Object method
*/
public int hashCode()
{
String hc = this.centreCode + this.trialTipNum + this.visitId;
return hc.hashCode();
}

public CompositeIDVisit()
{
}

public CompositeIDVisit(String centreCode, String trialTipNum, Integer visitId)
{
this.centreCode = centreCode;
this.trialTipNum = trialTipNum;
this.visitId = visitId;
}

}

****************************
And the generator from sequence class

import net.sf.hibernate.*;
import net.sf.hibernate.engine.*;
import net.sf.hibernate.id.SequenceGenerator;
import net.sf.hibernate.type.IntegerType;
import java.util.Properties;
import it.etnoteam.novartis.trialdb.utils.*;

import org.apache.log4j.*;

public class CompositeIDVisitGenerator
{
private static Logger logger = Logger.getLogger(it.etnoteam.novartis.trialdb.hibernate.id.CompositeIDVisitGenerator.class);

private static SequenceGenerator SEQUENCE_GENERATOR =
new SequenceGenerator();

static
{

try
{
SEQUENCE_GENERATOR.configure(
new IntegerType(),
new Properties(),
((SessionImplementor) HibernateUtils.getSession()).getFactory().getDialect());
}
catch (HibernateException he)
{
logger.error("Exception configuring sequence generator. Exception: " + he);
}
}

public static final CompositeIDVisit generate(String centreCode,
String trialTipNum,
Session session)
throws HibernateException
{

CompositeIDVisit id = new CompositeIDVisit();
id.setCentreCode(centreCode);
id.setTrialTipNum(trialTipNum);
try
{
id.setVisitId((Integer)SEQUENCE_GENERATOR.generate((SessionImplementor) session, id));
}
catch (Exception ex)
{
logger.error("Exception: ", ex);
}
return id;
}

}


*********************************************

When i try to create a new Visit object I get an oracle exception: "sequence does not exist". But the view exists!


Here's the code i use to create a new Visit object:

private void saveVisit(Centre centre)
{
Session hb_session = HibernateUtils.newSession();
try
{
CompositeIDVisit compId = CompositeIDVisitGenerator.generate(centre.getCentreCode(), centre.getTrialTipNum(), hb_session);

Visit visit = new Visit(compId);
hb_session.save(visit);
hb_session.flush();
}
catch (Exception ex)
{
logger.error("Cannot save visit. Exception: " + ex);
}
}

I can't really understand where is the problem. Why it cannot find the sequence?

Thanks in advance,

Luca


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 8:01 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Note: in the future please put your code in "[code]" blocks. It makes reading much easier.

Anyway, hibernate looks for a default sequence named "hibernate_sequence" if you don't specify a sequence name. (I've never tried it, but you can specify a name by passing the SequenceGenerator.SEQUENCE property to SequenceGenerator.configure())

I see from your code that you haven't specified a sequence name, so do you have a sequence named "hibernate_sequence"? If not, specify the name and you should be set.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:05 am 
Newbie

Joined: Thu Feb 12, 2004 10:10 am
Posts: 6
First of all, i'm really sorry for my previous mail without "[code]" blocks! I'm really a newbie of hibernate and its forum...!

Then... i can't find where the SEQUENCE attribute of SequenceGenerator should be specified. Maybe in the property file you pass to SequenceGenerator.configure(...)? But what should i put in this file (if this is the right solution) to configure the sequence generator?

Further, i can't set SEQUENCE name in the code cause it's final, and i cannot set it in my class.

Thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 12:33 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Do you have the API javadoc?

SEQUENCE is a static final String attribute of SequenceGenerator. Us it as a property key in the Properties you send to SequenceGenerator.configure(). You could load the property up from a file (via Properties.load()) but I'd just set it programmatically.

i.e.:
Code:
Properties props = new Properties();
props.setProperty(SequenceGenerator.SEQUENCE, "the_sequence_name");
SEQUENCE_GENERATOR.configure(
    new LongType(),
    props,
    ((SessionFactoryImplementor)<!-- INSERT METHOD TO GET YOUR SESSION FACTORY HERE -->).getDialect()
);


You could also just create a sequence named "hibernate_sequence" since that's what Hibernate looks for by default.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 12:35 pm 
Newbie

Joined: Thu Feb 12, 2004 10:10 am
Posts: 6
Thanks again


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