@RelatedTo(`type` = "PARTICIPATES_IN")
val meetings: java.util.Set[Meeting] = new java.util.HashSet[Meeting]()
def participateIn(meeting: Meeting) = {
meeting.creator = this // since bidirectional relation
meetings.add(meeting) //#needs_transaction => throw an NotInTransactionException !!
}
@Transactional
def save(meeting: Meeting): Meeting = {
val creator = userRepository.getUserBy_email("te...@test.com") // user retrieved (attached)
creator.participateIn(meeting) //attach a detached meeting entity
meeting.creator = creator
meetingRepository.save(meeting)
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<tx:annotation-driven mode="aspectj" />
<neo4j:repositories base-package="repositories"/>
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="controllers, applicationservices, models,repositories"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<bean id="neo4jTransactionManager" class="org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean">
<constructor-arg ref="graphDatabaseService"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager"/>
<neo4j:repositories base-package="repositories"/>
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="controllers, applicationservices, models,repositories"/>
</beans>
public TransactionState getTransactionState()
{
Transaction tx;
try
{
tx = getTransaction();
}
catch ( SystemException e )
{
throw new RuntimeException( e );
}
return tx != null ? ((TransactionImpl)tx).getState() : TransactionState.NO_STATE;
}
package applicationservices
import org.springframework.beans.factory.annotation.Autowired
import repositories.MeetingRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import scalaz._, Scalaz._
import utils.ScalaCloseableIterable
@Service
class MeetingServices {
@Autowired
var meetingRepository: MeetingRepository = _
@Transactional(readOnly = true)
def findAll() = {
new ScalaCloseableIterable(meetingRepository.findAll()).toList
}
@Transactional def save(meeting: Meeting): Meeting = {
val creator = userRepository.getUserBy_email("te...@test.com") // user retrieved (attached)
creator.participateIn(meeting) //attach a detached meeting entity
meeting.creator = creator
meetingRepository.save(meeting)
}
}
--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
def testMethodSavingAMeetingWithItsCreator = {
val meetingServices = new ClassPathXmlApplicationContext("application-context-test.xml")..getBean("meetingServices", classOf[MeetingServices])
val user = userRepository.getUserBy_email("te...@test.com") // user well retrieved
val possibleMeeting = Meeting.create("title", "description", "activityType", (new DateTime(2013, 2, 13, 21, 30), new DateTime(2013, 2, 13, 22, 30)), "place", 2)
(meetingServices.save(possibleMeeting.toOption.get, user)) match { // call MeetingServices's save method
case Success(s) =>
case Failure(e) => println(e.list(0).cause.get); println(e.list(0).msg) // enter in this failure case, since a NotInTransaction occured in the save method
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<neo4j:config graphDatabaseService="graphDatabaseService"/>
<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>
<tx:annotation-driven mode="aspectj" />
<neo4j:repositories base-package="repositories"/>
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="applicationservices, repositories"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase">
<constructor-arg value="target/neo4jgraph" />
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService" />
<bean id="neo4jTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="org.neo4j.kernel.impl.transaction.SpringTransactionManager">
<constructor-arg ref="graphDatabaseService" />
</bean>
</property>
<property name="userTransaction">
<bean class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
<constructor-arg ref="graphDatabaseService" />
</bean>
</property>
</bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="neo4jTransactionManager" />
<neo4j:repositories base-package="repositories"/>
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="services, repositories"/>
</beans>
@Transactional
def save(meeting: Meeting) {
val creator = userRepository.getUserByEmail("te...@test.com")
meetingRepository.save(meeting)
creator.participateIn(meeting)