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