Class SessionFactory

java.lang.Object
org.neo4j.ogm.session.SessionFactory

public class SessionFactory extends Object
This is the main initialization point of OGM. Used to create Session instances for interacting with Neo4j. In a typical scenario one instance of SessionFactory is created, shared across whole application.
Author:
Vince Bickers, Luanne Misquitta, Mark Angrish, Frantisek Hartman, Michael J. Simons
  • Constructor Summary

    Constructors
    Constructor
    Description
    SessionFactory(String... packages)
    Constructs a new SessionFactory by initialising the object-graph mapping meta-data from the given list of domain object packages and starts up the Neo4j database in embedded mode.
    SessionFactory(org.neo4j.ogm.config.Configuration configuration, String... packages)
    Constructs a new SessionFactory by initialising the object-graph mapping meta-data from the given list of domain object packages, and also sets the baseConfiguration to be used.
    SessionFactory(org.neo4j.ogm.driver.Driver driver, boolean useStrictQuerying, String... packages)
    Create a session factory with given driver Use this constructor when you need to provide fully customized driver.
    SessionFactory(org.neo4j.ogm.driver.Driver driver, String... packages)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this session factory Also closes any underlying resources, like driver etc.
    void
    deregister(EventListener eventListener)
    Removes the specified listener from this SessionFactory.
    Returns current load strategy
    boolean
     
    Retrieves the meta-data that was built up when this SessionFactory was constructed.
    Opens a new Neo4j mapping Session using the Driver specified in the OGM baseConfiguration The driver should be configured to connect to the database using the appropriate DriverConfig
    void
    register(IdStrategy strategy)
    Register an instance of IdStrategy This instance will be used for generation of ids annotated with @Id @GeneratedValue(strategy=SomeClass.class)
    void
    register(EventListener eventListener)
    Registers the specified listener on all Session events generated from this SessionFactory.
    final void
    runAutoIndexManager(org.neo4j.ogm.config.Configuration configuration)
    Opens a session and runs the auto-index manager with the given configuration and the metadata configured in this factory.
    void
     
    void
    Sets the LoadStrategy Will be used for all queries on subsequently created sessions.
    <T> T
    unwrap(Class<T> clazz)
    Method allows to unwrap either the OGM Driver ("Transport") by passing in one of org.neo4j.ogm.drivers.bolt.driver.BoltDriver org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver org.neo4j.ogm.drivers.http.driver.HttpDriver It also allows access to the underlying Neo4j Java Driver ("Bolt driver") or the embedded database instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SessionFactory

      public SessionFactory(String... packages)
      Constructs a new SessionFactory by initialising the object-graph mapping meta-data from the given list of domain object packages and starts up the Neo4j database in embedded mode. If the embedded driver is not available this method will throw a Exception.

      The package names passed to this constructor should not contain wildcards or trailing full stops, for example, "org.springframework.data.neo4j.example.domain" would be fine. The default behaviour is for sub-packages to be scanned and you can also specify fully-qualified class names if you want to cherry pick particular classes.

      Indexes will also be checked or built if configured.
      Parameters:
      packages - The packages to scan for domain objects
    • SessionFactory

      public SessionFactory(org.neo4j.ogm.config.Configuration configuration, String... packages)
      Constructs a new SessionFactory by initialising the object-graph mapping meta-data from the given list of domain object packages, and also sets the baseConfiguration to be used.

      The package names passed to this constructor should not contain wildcards or trailing full stops, for example, "org.springframework.data.neo4j.example.domain" would be fine. The default behaviour is for sub-packages to be scanned and you can also specify fully-qualified class names if you want to cherry pick particular classes.

      Indexes will also be checked or built unless auto index mode is set to NONE.
      Parameters:
      configuration - The baseConfiguration to use
      packages - The packages to scan for domain objects. They will be merged with the configuration.
    • SessionFactory

      public SessionFactory(org.neo4j.ogm.driver.Driver driver, String... packages)
      See Also:
    • SessionFactory

      public SessionFactory(org.neo4j.ogm.driver.Driver driver, boolean useStrictQuerying, String... packages)
      Create a session factory with given driver Use this constructor when you need to provide fully customized driver. Indexes will not be automatically created.
      Parameters:
      driver - driver to be used with this SessionFactory
      useStrictQuerying - Flag wether to use strict querying or not. Overwrites configuration settings (from the driver).
      packages - The packages to scan for domain objects
  • Method Details

    • runAutoIndexManager

      public final void runAutoIndexManager(org.neo4j.ogm.config.Configuration configuration)
      Opens a session and runs the auto-index manager with the given configuration and the metadata configured in this factory. This method can be run multiple times.
      Parameters:
      configuration - only used to configure aspects of the auto-index manager, not for the session factory at this point.
    • metaData

      public MetaData metaData()
      Retrieves the meta-data that was built up when this SessionFactory was constructed.
      Returns:
      The underlying MetaData
    • openSession

      public Session openSession()
      Opens a new Neo4j mapping Session using the Driver specified in the OGM baseConfiguration The driver should be configured to connect to the database using the appropriate DriverConfig
      Returns:
      A new Session
    • register

      public void register(EventListener eventListener)
      Registers the specified listener on all Session events generated from this SessionFactory. Only Session instances created after this call are affected.
      Parameters:
      eventListener - The event listener to register.
    • deregister

      public void deregister(EventListener eventListener)
      Removes the specified listener from this SessionFactory. Only Session instances created after this call are affected.
      Parameters:
      eventListener - The event listener to deregister.
    • getLoadStrategy

      public LoadStrategy getLoadStrategy()
      Returns current load strategy
      Returns:
      load strategy
    • setLoadStrategy

      public void setLoadStrategy(LoadStrategy loadStrategy)
      Sets the LoadStrategy Will be used for all queries on subsequently created sessions. This also can be set on individual Session instances.
      Parameters:
      loadStrategy - load strategy
    • isUseStrictQuerying

      public boolean isUseStrictQuerying()
      Returns:
      True if this instance uses strict querying or not.
    • setEntityInstantiator

      public void setEntityInstantiator(EntityInstantiator entityInstantiator)
    • close

      public void close()
      Closes this session factory Also closes any underlying resources, like driver etc.
    • register

      public void register(IdStrategy strategy)
      Register an instance of IdStrategy This instance will be used for generation of ids annotated with @Id @GeneratedValue(strategy=SomeClass.class)
      Parameters:
      strategy - IdStrategy to use
    • unwrap

      public <T> T unwrap(Class<T> clazz)
      Method allows to unwrap either the OGM Driver ("Transport") by passing in one of
      • org.neo4j.ogm.drivers.bolt.driver.BoltDriver
      • org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
      • org.neo4j.ogm.drivers.http.driver.HttpDriver
      It also allows access to the underlying Neo4j Java Driver ("Bolt driver") or the embedded database instance.
      Type Parameters:
      T - The type of the object to unwrap
      Parameters:
      clazz - The class of the object to unwrap
      Returns:
      The unwrapped, underlying object
      Throws:
      IllegalArgumentException - when an object of the given class cannot be unwrapped.