我对java和spring非常陌生,因此我尝试制作一个简单的应用程序,以链表方式将车辆测量添加到车辆:
我在弄清楚是否使用springframework.data.neo4j注解的neo4j.ogm注解时遇到了很多麻烦。目前我决定使用springframework注解,因为ogm注解会导致不正确的id执行选项。在我的代码片段中,我将相似的成员合并到同一个片段中以减少冗余。
这是我的版本。gradle:
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
test {
useJUnitPlatform()
}
这是我的应用程序根目录:
package com.example.neo4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
@SpringBootApplication
@EnableNeo4jRepositories
public class Test2Application {
public static void main(String[] args) {
SpringApplication.run(Test2Application.class, args);
}
@Bean
CommandLineRunner demo(VehicleRepository vehicleRepository, VehicleMeasurementRepository vehicleMeasurementRepository, VehicleMeasurementService vehicleMeasurementService) {
return args -> {
vehicleRepository.deleteAll();
vehicleMeasurementRepository.deleteAll();
Vehicle vehicle = new Vehicle("vin", "Ford", "Bronco");
vehicleRepository.save(vehicle);
VehicleMeasurement first = new VehicleMeasurement(5D, 198D, 100D);
VehicleMeasurement second = new VehicleMeasurement(10D, 198D, 98D);
VehicleMeasurement third = new VehicleMeasurement(15D, 198D, 97D);
vehicleMeasurementService.addNewMeasurementByVin("vin", third);
vehicleMeasurementService.addNewMeasurementByVin("vin", second);
vehicleMeasurementService.addNewMeasurementByVin("vin", first);
};
}
}
以下是我的实体:
package com.example.neo4j;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
@Data
@Node
@NoArgsConstructor
public class Vehicle {
@Id @GeneratedValue
private Long id;
private String vin, make, model;
public Vehicle(String vin, String make, String model) {
this.vin = vin;
this.make = make;
this.model = model;
}
@Relationship(type = "HAS_HEAD_MEASUREMENT", direction = Relationship.Direction.OUTGOING)
@JsonIgnoreProperties({"next", "previous"})
private VehicleMeasurement head;
@Relationship(type = "HAS_TAIL_MEASUREMENT", direction = Relationship.Direction.OUTGOING)
@JsonIgnoreProperties({"next", "previous"})
private VehicleMeasurement tail;
public VehicleMeasurement getHead() {
return head;
}
}
@Node
@Data
@NoArgsConstructor
public class VehicleMeasurement {
@Id @GeneratedValue
private Long id;
private Double mileage, engineTemperature, oilLife;
public VehicleMeasurement(Double mileage, Double engineTemperature, Double oilLife) {
this.mileage = mileage;
this.engineTemperature = engineTemperature;
this.oilLife = oilLife;
}
@Relationship(type = "NEXT", direction = Relationship.Direction.OUTGOING)
@JsonIgnoreProperties({"head", "tail", "vehicleHeadReference", "vehicleTailReference"})
private VehicleMeasurement next;
}
以下是我的存储库:
package com.example.neo4j;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import java.util.List;
public interface VehicleRepository extends Neo4jRepository<Vehicle, Long> {
Vehicle findVehicleByVin(String vin);
}
public interface VehicleMeasurementRepository extends Neo4jRepository<VehicleMeasurement, Long> {
@Query("MATCH (v:Vehicle {vin: $vin})-[*]->(vm:VehicleMeasurement)-[*]-(v)" +
"RETURN DISTINCT vm ORDER BY vm.mileage ASCENDING")
List<VehicleMeasurement> findVehicleMeasurementsByVehicleVin(String vin);
}
这是我的服务:
package com.example.neo4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Iterator;
import java.util.Optional;
@Service
public class VehicleMeasurementService {
@Autowired
VehicleMeasurementRepository vehicleMeasurementRepository;
@Autowired
VehicleRepository vehicleRepository;
public void addNewMeasurementByVin(String vin, VehicleMeasurement measurement) {
Vehicle targetVehicle = vehicleRepository.findVehicleByVin(vin);
Iterator<VehicleMeasurement> vehicleMeasurements = vehicleMeasurementRepository.findVehicleMeasurementsByVehicleVin(vin).iterator();
Optional<VehicleMeasurement> left = Optional.empty();
Optional<VehicleMeasurement> right = Optional.empty();
while (vehicleMeasurements.hasNext()) {
var next = vehicleMeasurements.next();
if (next.getMileage() <= measurement.getMileage()) {
left = Optional.of(next);
}
if (measurement.getMileage() <= next.getMileage() && right.isEmpty()) {
right = Optional.of(next);
}
}
if (left.isEmpty() && right.isEmpty()){
targetVehicle.setHead(measurement);
targetVehicle.setTail(measurement);
} else if (left.isEmpty()) {
targetVehicle.setHead(measurement);
measurement.setNext(right.get());
vehicleMeasurementRepository.save(right.get());
} else if (right.isEmpty()) {
VehicleMeasurement leftValue = left.get();
leftValue.setNext(measurement);
vehicleMeasurementRepository.save(leftValue);
targetVehicle.setTail(measurement);
}
vehicleMeasurementRepository.save(measurement);
vehicleRepository.save(targetVehicle);
}
}
我运行add方法3次,并在它们之间进行调试,以查看以下结果:
最后一张图是问题所在,当我在调试过程中钻取节点时,我看到了预期的引用,但是当它运行完之后,节点98和节点97之间的连接就不存在了。
任何建议都将非常感谢,风格,使用方法,或任何有帮助的。我预计我使用Spring Data 不正确。我尝试了一个双向引用的格式副本,它会破坏堆栈,即使我的忽略属性注解也是如此。
暂无答案!
目前还没有任何答案,快来回答吧!