NEO4J and Springdata Integration

Source: Internet
Author: User
Tags neo4j

1. The jar package to be imported by MAVEN project

<!--neo4j--
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.3.2</version>
</dependency>

2. jar package corresponding to spring

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>

3, load the neo4j drive configuration

@Configuration @enableneo4jrepositories ("Com.neo4j.repository") @EnableTransactionManagement Public classNeo4japplicationextendsneo4jconfiguration { Public Static Final intNeo4j_port = 7474; @Bean Publicsessionfactory getsessionfactory () {return NewSessionfactory ("Com.neo4j.domain"); }//Configure Transaction @Bean @Qualifier ("Neo4jtransactionmanager")     PublicNeo4jtransactionmanager Neo4jtransactionmanager ()throwsException {return NewNeo4jtransactionmanager (GetSession ()); }}

4, the connection method uses Httpdriver

Add configuration file Ogm.properties

Driver=org.neo4j.ogm.drivers.http.driver.httpdriver
Uri=http://neo4j:[email protected]:7474

5. Domain Entity Configuration

//node annotations (you can add label labels)@NodeEntity Public classthing{//ID of the node in neo4j       PrivateLong ID;  PublicLong getId () {returnID; } @Override Public Booleanequals (Object o) {if( This==o)return true; if(O = =NULL|| id = =NULL|| GetClass ()! =O.getclass ())return false; Entity entity=(Entity) o; if(!id.equals (entity.id))return false; return true; } @Override Public inthashcode () {return(id = =NULL) ? -1: Id.hashcode (); }        //Properties    PrivateString userId; PrivateString name; PrivateString desc; //Configuring Transformations@DateString ("Yy-mm-dd")    PrivateDate DateTime; //Set Relationship@Relationship (type = "Have_prop", direction=relationship.outgoing) List<Property> Properties =NewArraylist<property>(); @Relationship (Type= "Have_service", direction=relationship.outgoing) Set<Service> Services =NewHashset<service>(); @Relationship (Type= "Have_prop") Set<ThingPropRelation> proprelations =NewHashset<thingproprelation>(); @Relationship (Type= "Have_service") Set<ThingServiceRelation> servicerelations =NewHashset<thingservicerelation>(); @Relationship (Type= "Have_service") Set<ThingServiceRelation> servicerelations =NewHashset<thingservicerelation>();}

Set the relationship of a node

//set up a relationship entity@RelationshipEntity (type= "Have_prop") Public classThingproprelationextendsEntity {//Start Node@StartNode Thing Thing; //End Node@EndNode property;  Publicthingproprelation () {} PublicThing getthing () {returnthing; }     Public voidsetthing (Thing Thing) { This. Thing =thing; }     PublicProperty GetProperty () {returnProperty ; }     Public voidSetProperty (property) { This. property =Property ; }}

6. Set Repository

// Interface Inheritance Graphrepository // provides the basic save Modify Delete query function // Special queries can be implemented by @query annotations  Public Interface extends Graphrepository<thing> {    Thing findbyname (String name);        @Query ("MATCH (t:thing {name:{0}})-[r:have_prop]-> (p) RETURN P")    iterable<Property> getthingpropertybythingname (String thingname);}

7. Application

@Autowired Private

Call

Thing Thing = Thingrepository.findbyname ("Thing");

NEO4J and Springdata Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.