Code:
@RunWith(JMock.class)
public class XTest{
private Mockery mm;
private static Session ss;
private X xx;
private final String string = getRandomString();
private static final String JDBC_URL = "jdbc:hsqldb:mem:SCHEME";
@Before
public void setUp(){
mm = new JUnit4Mockery();
ss = mm.mock(Session.class);
xx = new X();
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection(JDBC_URL,"sa","");
Statement st = conn.createStatement();
String expression = "CREATE SCHEMA SCHEME AUTHORIZATION DBA";
st.executeUpdate(expression);
/* Am lil doubtful abt this */
expression = "CREATE SCHEMA dual_SCHEME AUTHORIZATION DBA";
st.executeUpdate(expression);
st.close();
conn.close();
Configuration cfg = new Configuration().configure("abc.xml");
HibernateUtil.setSessionFactory(cfg.buildSessionFactory()) ;
}
/* This test seems to be not working!! */
public void testSaveandGetX(){
X x = getRandomX();
x.setStr1(string);
save(x);
HibernateUtil.begintransaction();
/* This returns a null object!! */
X ret = xx.getXwithStr1(string);
Assert.assertEquals(string,ret.getStr1);
HibernateUtil.commitTransaction();
}
public void save(X x){
HibernateUtil.beginTransaction();
session.save(x);
HibernateUtil.commiTransaction(); /* Throws an exception! Not able to commit*/
}
public void getXwithStr1(String s){
/* This call also fails!*/
X x = (X)HibernateUtil.getSession().createCriteria(Newspaper.class).add(Expression.eq("Str1",s)).uniqueResult();
return x;
}
}
/* X.hbm.xml */
/*
Add all the hibernate includes!
*/
<class name="X" table="SCHEME.X">
<id name="id" column="X_ID" type="long">
<generator class = "sequence">
<param name="sequence">SCHEME.X_SEQ</param>
</generator>
</id>
<property name="Str1" column="Str1" not-null="true" />
<set name ="sections" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="X_ID" not-null="true"/>
<one-to-many class="XSection" />
</set>
/* XSection.hbm.xml */
/*
Add all the hibernate includes
*/
<class name="XSection" table="SCHEME.XSection">
<id name="id" column="XSection_ID" type="long">
<generator class="sequence">
<param name="sequence">SCHEME.XSection_SEQ</param>
</generator>
</id>
<many-to-one name="par" column="X_ID" class="X" not-null="true" />
<property name="mark" column="MARK" type="long" not-null="true" />
</class>
For some reason, I am not able to commit the Transaction!. I am not able to retrieve the data. I feel there is something wrong in the creation of my schema in HSQL.
I would really appreciate if someone could help me out with this! Pl.
Thanks
Vivek