我正在尝试将 Neo4j TestContainers 与 Kotlin、Spring Data Neo4j、Spring Boot 和 JUnit 5 一起使用。我有很多测试需要使用测试容器。理想情况下,我希望避免在每个测试类中复制容器定义和配置。
目前我有类似的东西:
@Testcontainers
@DataNeo4jTest
@Import(Neo4jConfiguration::class, Neo4jTestConfiguration::class)
class ContainerTest(@Autowired private val repository: XYZRepository) {
companion object {
const val IMAGE_NAME = "neo4j"
const val TAG_NAME = "3.5.5"
@Container
@JvmStatic
val databaseServer: KtNeo4jContainer = KtNeo4jContainer("$IMAGE_NAME:$TAG_NAME")
.withoutAuthentication()
}
@TestConfiguration
internal class Config {
@Bean
fun configuration(): Configuration = Configuration.Builder()
.uri(databaseServer.getBoltUrl())
.build()
}
@Test
@DisplayName("Create xyz")
fun testCreateXYZ() {
// ...
}
}
class KtNeo4jContainer(val imageName: String) : Neo4jContainer<KtNeo4jContainer>(imageName)
Run Code Online (Sandbox Code Playgroud)
如何提取数据库服务器定义和@TestConfiguration?我尝试了不同的方法来创建基类并让 ContainerTest 扩展它,但它不起作用。据我了解,静态属性在 Kotlin …
使用spring-data-neo4j,我想创建两个类,@RelationshipEntity(type="OWNS")用于将Person类链接到a Pet和Car.
@RelationshipEntity(type="OWNS")
public class OwnsCar {
@Indexed
private String name;
@StartNode
private Person person;
@EndNode
private Car car;
}
@RelationshipEntity(type="OWNS")
public class OwnsPet {
@Indexed
private String name;
@EndNode
private Person person;
@StartNode
private Pet pet;
}
Run Code Online (Sandbox Code Playgroud)
这保存到图形数据库正常,没有任何问题,因为我可以查询的实际Node和Relationship,看看他们键入等.
但是当我尝试使用时,@RelatedTo(type="OWNS", elementClass=Pet.class)我要么获得类强制转换异常,要么在使用延迟初始化时,我得到的结果不正确.
@NodeEntity
public class Person {
@Indexed
private String name;
@RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Pet.class)
private Set<Pet> pets;
@RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Car.class)
private Set<Car> cars;
}
Run Code Online (Sandbox Code Playgroud)
我尝试打印我的人时得到的结果(我toString() …
我们有一个项目,我们使用Spring Data Neo4J.其中一个重要实体如下所示:
@NodeEntity
public class Category {
@GraphId
Long id;
String name;
@RelatedTo(direction = Direction.INCOMING, type = "CHILD")
Category parent;
@RelatedTo(direction = Direction.OUTGOING, type = "CHILD")
Set<Category> children;
}
Run Code Online (Sandbox Code Playgroud)
我们需要从名称已知的特定类别开始查找所有叶子类别(即没有任何子节点的类别).例如,给定如下所示的层次结构:
Electronics
Camera
Point and Shoot
SLR
Computing
Desktop
Laptop
Tablet
Netbook
Furniture
Tables
Office tables
Home tables
Chairs
Lounge chairs
Office chairs
Run Code Online (Sandbox Code Playgroud)
搜索"家具"应返回"办公桌","家庭桌","休闲椅"和"办公椅".同样,搜索"计算"应返回"桌面","笔记本电脑","平板电脑"和"上网本".
需要帮助来创建一个cypher查询,该查询可以放在Spring Data存储库方法上,以便为我提供从指定节点开始的所有叶节点.
编辑以下查询(使用关联的Spring Data存储库方法)在Wes的帮助下工作:
@Query(
"START category=node:__types__(className='org.example.domain.Category') " +
"MATCH category-[:CHILD*0..]->child " +
"WHERE category.name={0} AND NOT(child-[:CHILD]->()) " +
"RETURN child")
List<Category> findLeaves(String name);
Run Code Online (Sandbox Code Playgroud) 如何通过Spring Roo 1.2.x使用Neo4J?
书籍和在线文档似乎表明Neo4j是Spring Roo 1.2.4的搜索插件.虽然我可以手动添加spring-data-neo4j依赖项,但我真的想利用Roo功能.
Spring Roo 1.2.4.RELEASE [rev 75337cf]
roo> addon search graph
0 found, sorted by rank; T = trusted developer; R = Roo 1.2 compatible
ID T R DESCRIPTION -------------------------------------------------------------
[HINT] use 'addon info id --searchResultId ..' to see details about a search result
[HINT] use 'addon install id --searchResultId ..' to install a specific search result, or
[HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
Run Code Online (Sandbox Code Playgroud) 如何使用cypher查询搜索节点,其中一个节点属性具有字符串数组?
例如members-- > ["abc","xyz","pqr"].
我可以通过以相同的方式保持数组元素的顺序来找到节点,例如
START root=node(*) where has(root.members) and root.members=["abc","xyz","pqr"] return root;
但如果我不能/不能提供节点属性中元素的确切顺序,如何搜索节点?
我看到,与 spring-data-rest 存储库一起使用时,一组实体上的注释@RestResource(exported=false)不会导出该组,而是嵌入该组实体而不是渲染 url。
这是预期的行为吗?
exported=false至少可以说,这个属性不是具有误导性吗?该注释的确切语义是什么?
我将 spring-data-rest 与 neo4j 一起使用,即我的项目依赖于 spring-data-neo4j。
例如,如果我想按名称获取用户列表:
class UserRepository extands GraphRepository<User> {
List<User> findByName(String name);
}
Run Code Online (Sandbox Code Playgroud)
那么如何将加载深度设置为2?
我试图在SDN 4.0.0.RC2文档中找到答案,但它没有包含有关此问题的任何内容.
由于我现在没有回答并且我打开的github Ticket已经关闭,我将在github上创建一个示例来重现该bug以找出问题所在.一旦问题得到解决,我将在这个问题上添加我自己的答案,以提供真正的解决方案.
首先,我在SDN的Github上报告了这个问题.
无论如何,我在这里发布我的问题,看看是否有人有解决方案.
所以,我有一个玩家模型:
@NodeEntity
public class Player {
@GraphId Long id;
String name;
String email;
@Transient
String password;
int elo = 1200;
@RelatedTo(type="FRIEND_WITH", direction = Direction.BOTH)
Set<Player> friends = new HashSet<Player>();
//After this I have Getters and Setters
//no need to paste them all I guess
}
Run Code Online (Sandbox Code Playgroud)
及其存储库接口:
@RepositoryRestResource(collectionResourceRel="player", path="player")
public interface PlayerRepository extends PagingAndSortingRepository<Player, Long> {
}
Run Code Online (Sandbox Code Playgroud)
我使用POST请求创建了两个玩家,并验证它们是使用GET创建的.
然后我想添加玩家0作为玩家1的朋友:
curl -i -X POST -H 'Content-type: text/uri-list' -d 'localhost:8080/api/player/1' http://localhost:8080/api/player/0/friends
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
HTTP/1.1 500 …Run Code Online (Sandbox Code Playgroud) 我正在使用每个循环对每个值执行单个插入。
我们如何使用密码查询进行批量插入。
这是我的代码...
控制器
@PostMapping("/geohash")
public Set<String> create(@RequestParam String name, @RequestBody LatLng[] latLngs) {
double[][] polygonPoints = convertTo2dArrayOfLatLng(latLngs);
Set<String> geoHashesForPolygon = GeoHashUtils.geoHashesForPolygon(6, polygonPoints);
for (String geohash : geoHashesForPolygon) {
min = Math.min(min, geohash.length());
geohashes = neoService.create(name, geohash);
}
return geoHashesForPolygon;
}
Run Code Online (Sandbox Code Playgroud)
我想将每个插入geoHashesForPolygon为单个节点..
密码查询
@Query("MATCH (c:C) WHERE c.name = {name} CREATE (g: G{name : {geohash}} )<-[:cToG]-(c) RETURN c,g")
public GeohashOfCluster create(@Param("name") String name,@Param("geohash") String geohash);
Run Code Online (Sandbox Code Playgroud) 升级到 Neo4j v5.9.0 时,我尝试通过创建以下配置来强制执行 Neo4j v5 方言,如文档所示
import org.neo4j.cypherdsl.core.renderer.Dialect
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.neo4j.cypherdsl.core.renderer.Configuration as CypherConfiguration
@Configuration
class Neo4jConfiguration {
@Bean
fun cypherDslConfiguration(): CypherConfiguration =
CypherConfiguration.newConfig().withDialect(Dialect.NEO4J_5).build()
}
Run Code Online (Sandbox Code Playgroud)
当从弹簧执行器检查时,似乎存在以下 bean
"cypherDslConfiguration": {
"aliases": [],
"scope": "singleton",
"type": "org.neo4j.cypherdsl.core.renderer.Configuration",
"resource": "class path resource [com/foo/infra/neo4j/Neo4jConfiguration.class]",
"dependencies": [
"neo4jConfiguration"
]
}
Run Code Online (Sandbox Code Playgroud)
当使用存储库查询时,我仍然收到来自各种不同查询的以下警告
Neo.ClientNotification.Statement.FeatureDeprecationWarning: This feature is deprecated and will be removed in future versions.
UNWIND $__relationships__ AS relationship WITH relationship MATCH (startNode:`MyNode`) WHERE startNode.entityId = relationship.fromId MATCH (endNode) WHERE id(endNode) = relationship.toId MERGE …Run Code Online (Sandbox Code Playgroud)