I have a CompositeUserType which needs to access 2 separate tables, on the NullSafeGet this is working fine using the passed SessionImplementor like so
Code:
@Override
public Set nullSafeGet(ResultSet rs, String[] names,
SessionImplementor session, Object owner) throws HibernateException,
SQLException {
String rSubLayers = (String)Hibernate.STRING.nullSafeGet(rs, names[0]);
if (rSubLayers.length()>0){
HashSet<Layer> returnedLayers = new HashSet<Layer>();
for (String lyrID : rSubLayers.split(",")){//Split the layer list
if (lyrID!=null && !lyrID.contains("null")){
Long lID = Long.decode(lyrID);
//Concurrently get further child layers
Layer lyr = (Layer) session.getFactory().getCurrentSession().get(Layer.class, lID);
returnedLayers.add(lyr);
}
}
return returnedLayers;
}
return null;
}
But the same method doesn't work with NullSafeSet (the parts that don't work are marked with /*Method 1*/ and /*Method 2*/) like so...
Code:
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
SessionImplementor session) throws HibernateException, SQLException {
StringBuilder subLayers = new StringBuilder("");
if (value == null) {
st.setString(index, "");
}else if (!(value instanceof Set)){
throw new IllegalArgumentException( "Received object in nullSafeSet was " + (value==null?"null":value.getClass()) + " in " + returnedClass() );
} else {
//Build the comma separated lists for database
for (Layer lyr : (Set<Layer>)value){//if this is not a set of layers, the exception would have been thrown already
//Insert layer name into comma separated string (and layer into layer table)
subLayers.append(lyr.getLayerID()+",");
[b]
/*Method 1*/
session.getFactory().getCurrentSession().load(lyr, lyr.getLayerID());
/*Method 2*/
session.getFactory().getCurrentSession().save(lyr);
[/b]
}
//Remove additional comma
subLayers = subLayers.deleteCharAt(subLayers.length()-1);
}
st.setString(index, subLayers.toString());
}
There are two methods I've tried, obviously not together which both give the same error
Code:
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
at edina.clive.domain.hibernate.SubLayerUserType.nullSafeSet(SubLayerUserType.java:131)
at org.hibernate.type.CompositeCustomType.nullSafeSet(CompositeCustomType.java:219)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2002)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2248)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at edina.clive.controller.test.WmsControllerTest.buildCapabilitiesTestData(WmsControllerTest.java:237)
at edina.clive.controller.test.WmsControllerTest.testHandleGetCapabilities(WmsControllerTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
and...
Code:
Hibernate: insert into layers (layerName, layerTitle, layerSRS, layerscalemin, layerscalemax, boundingBoxGeometry, subLayers, layerID) values (?, ?, ?, ?, ?, ?, ?, ?)
2009-04-03 09:35:31,540 5575 ERROR [main] org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
at edina.clive.domain.hibernate.SubLayerUserType.nullSafeSet(SubLayerUserType.java:131)
at org.hibernate.type.CompositeCustomType.nullSafeSet(CompositeCustomType.java:219)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2002)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2248)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at edina.clive.controller.test.WmsControllerTest.buildCapabilitiesTestData(WmsControllerTest.java:237)
at edina.clive.controller.test.WmsControllerTest.testHandleGetCapabilities(WmsControllerTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Can anyone shed light on why these don't work and perhaps how to fix it?