楼主: jieforest

Neo4j用Spring Data Neo4j建模

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
21#
 楼主| 发表于 2013-8-8 09:13 | 只看该作者
Listing 4 User and Movie node entity snippets
  1. 01.@NodeEntity
  2. 02.public class User {
  3. 03.@RelatedToVia                               #1
  4. 04.Set views;                          #1
  5. 05.. . .
  6. 06.
  7. 07.@NodeEntity
  8. 08.public class Movie {
  9. 09.@RelatedToVia(direction = Direction.INCOMING)               #2
  10. 10.Iterable views;                                            #2
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
22#
 楼主| 发表于 2013-8-8 09:14 | 只看该作者
#1 For each HAS_SEEN relationship between user and all movies he or she has seen, the Viewing class contains relationship information
#2 For each HAS_SEEN relationship between movie and all users who've seen it, the Viewing class contains relationship information

Within the User node entity, the RelatedToVia annotation (#1) on the views field essentially reads as "all the HAS_SEEN relationships (with any associated properties) between this user and any movies he or she has watched." (The HAS_SEEN relationship type is inferred because that is what is defined on the Viewing class itself.) The Viewing class represents the full context of the relationship between these two entities including the rating field.

Within the Movie node entity, the RelatedToVia annotation (#2) marks the views field as representative of "all the HAS_SEEN relationships (with any associated properties) between this movie and any users who have actually watched it."

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
23#
 楼主| 发表于 2013-8-8 09:14 | 只看该作者
In both cases the Viewing class serves to provide that extra context to the relationship that details the rating of each viewing, and can thus be thought of as the object through which (via) the relationship is fully understood by both sides.

In the next section we continue to detail some of the finer points around defining a variety of different types of relationships between node entities, including where the rich relationship details are required to fully understand the context.

Modeling relationships between node entities

Being able to model node and relationship entities with their simple associated properties in isolation will only take you so far. Models start getting interesting when you're able to actually connect them to explore the relationships between them, and in this section, we'll cover how to do that.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
24#
 楼主| 发表于 2013-8-8 09:15 | 只看该作者
The end of the previous section provided a sneak preview of how such a connection was established between the User and Movie entities through the Viewing relationship entity.

You saw how the HAS_SEEN relationship between Users and Movies was modeled as a physical POJO (Viewing class) and then referred to from the entities. In that particular case, a whole separate class (Viewing) was used to represent the relationship in context. But what about other, more simple relationships such as "John is a friend of Jack"? Do you also need a dedicated relationship entity class for such cases?

You'll be pleased to know the answer is no—they can be dealt with in a much more simple manner. Figure 4 recaps the relationships between node entities that you're potentially interested in referencing from node entities.



Figure 4 Social network model with relationship references between nodes highlighted

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
25#
 楼主| 发表于 2013-8-8 09:15 | 只看该作者
From the node entity's perspective, relationships are also simply modeled as normal Java object references and come in a variety of flavors depending on exactly what it is you're trying to convey. We've already previewed how you can use the Viewing class to reference the HAS_SEEN relationship, but let's tackle the other simpler relationships as well, such as referredBy and IS_FRIEND_OF.

The relationships involved on the User and Movie entities are shown in the next listing.

Listing 5 User and Movie node entity snippets
  1. 01.public class User {
  2. 02.User referredBy;                                                   #1
  3. 03.@RelatedTo(type = "IS_FRIEND_OF", direction = Direction.BOTH)      #2
  4. 04.Set friends;                              #2
  5. 05.@RelatedToVia                                                      #3
  6. 06.Set views;                                                #3
  7. 07.. . .
  8. 08.
  9. 09.public class Movie {
  10. 10.@RelatedToVia(direction = Direction.INCOMING)                      #4
  11. 11.Iterable views;                                           #4
  12. 12.. . .
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
26#
 楼主| 发表于 2013-8-9 10:24 | 只看该作者
#1 User (node entity) associated with this node through referredBy relationship
#2 Users (node entities) associated with this node through IS_FRIEND_OF relationship
#3 Relationship information (relationship entity) that exists between this user and movies (node entities) he or she viewed via HAS_SEEN relationship
#4 Read-only relationship information (relationship entity) that exists between this movie and users (node entities) who rated it via HAS_SEEN relationship

Basic relationships, defined as being represented by an underlying Neo4j relationship with no associated properties, to exactly zero or one other node entity can be modeled as a standard object reference within the node entity. By default, the property name will be used as the name to map to the relationship type in Neo4j in the absence of any meta-information to the contrary. The newly introduced concept of referrals of one user to another (modeled by the referredBy property at #1) is an excellent example of this. Note that this property must be a reference to another node entity.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
27#
 楼主| 发表于 2013-8-9 10:25 | 只看该作者
Basic relationships to zero or more other node entities are either modeled with a Set, List, Collection, or Iterable class, with the referenced node entity as the collection type. The use of a Set, List, or Collection class signifies that the field is modifiable from the containing node's perspective, while an Iterable class indicates this should be treated as read-only.

Based on the contained node entity class type and its annotations, SDN will be able to work out that your intention is for this field to represent a basic relationship. If, however, you'd like to overwrite any of the defaults inferred, you can add a @RelatedTo annotation. The friends relationships (#2) between users is an example. Note how in this case we added the @RelatedTo annotation to specify the underlying Neo4j relationship type as IS_FRIEND_OF rather than the default value that SDN would have inferred if it were not there—that is, the name of the referenced field, friends.

Rich relationships, defined as being represented by an underlying Neo4j relationship with associated properties, are also modeled with the same Collection class as basic relationships.

In this case, however, the type of the contained entity in the collection is a relationship entity rather than a node entity. To recap, a relationship entity represents the underlying Neo4j relationship along with any associated properties that were also modeled in the entity. This provides a neat way to access the rich information on the relationship itself, while still being able to get to the entity or entities on the other end. As with basic relationships, without any annotations, SDN can work out that you're creating such references based solely on the fact that the type of class contained in the collection has been defined as a relationship entity. Again, if you wish to override any of these relationship defaults assumed by SDN, you can apply the @RelatedToVia annotation. As you've already seen, the views field reference (#3) representing the relationship between a User and a Movie is a good example here as the additional stars rating serves to enhance the information of the relationship between these two entities.

Notice the different Collection class used for the views property in the case of the User and Movie nodes, namely Setand Iterable, respectively. This means that the views property can be modified from the User node perspective but not from the Movie node. Conceptually, users rate movies, movies don't apply ratings to users!

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
28#
 楼主| 发表于 2013-8-9 10:25 | 只看该作者
Both the @RelatedTo (#3) and @RelatedToVia (#4) annotations can take a type and optional direction element that can be used, specified to clarify whether relationships of a particular direction (valid values are INCOMING, BOTH, and the default OUTGOING) should be included in the collection of entities returned.

Note that for the friends property you need to specify the direction as BOTH as shown in the following snippet:
  1. 1.@RelatedTo(type = "IS_FRIEND_OF", direction = Direction.BOTH)
  2. 2.Set friends;
复制代码
Logically, a friends relationship is bidirectional, physically within Neo4j, however, relationships are only stored in one direction. By specifying BOTH you're telling Neo4j to consider any IS_FRIENDS_OF relationships associated with the user regardless of which direction the physical relationship is defined.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
29#
 楼主| 发表于 2013-8-9 10:25 | 只看该作者
Summary

We introduced you to the Spring Data Neo4j (SDN) framework that provides you with a variety of tools to be able to operate with a standard POJO-based domain model backed by the powerful Neo4j database.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
30#
 楼主| 发表于 2013-8-9 10:26 | 只看该作者
over.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表