gpt4 book ai didi

java - 无法使用 spring 从我的数据库检索数据

转载 作者:行者123 更新时间:2023-12-01 13:09:50 25 4
gpt4 key购买 nike

我正在尝试使用 Spring CrudRepositorysave() 方法保存将其他对象作为属性的对象列表>。我的服务如下

@Service
@Transactional
public class DBDataManipulatorServiceImpl implements DBDataManipulatorService {

@Autowired
private AircraftRepository aircraftRepository;
@Autowired
private WorkPackageRepository workPackageRepository;
@Autowired
private MyJobRepository myJobRepository;

@Override
@Transactional
public List<MyJob> saveToDataBase(List<MyJob> jobs) {

List<MyJob> newJobs = new ArrayList<MyJob>();
MyJob myJob = null;

for (MyJob job : jobs) {
myJob = myJobRepository.save(job);
newJobs.add(myJob);
}

return newJobs;
} ...

myJob 类是:

@Entity
@Table(name="job", uniqueConstraints=@UniqueConstraint(columnNames={"jobId"}))
public class MyJob implements Serializable {


@Id
@Column(name = "jobId")
private String jobId;
private String jobStatus;

@ManyToOne()
private Aircraft aircraft;

@ManyToOne()
private WorkPackage workPackage;
....

飞机类别是:

@Entity
@Table(name="aircraft", uniqueConstraints=@UniqueConstraint(columnNames={"aircraftId"}))
public class Aircraft implements Serializable{


@Id
@Column(name = "aircraftId")
private String aircraftId;

@Column (name = "TYPE")
private String type;

@Column (name = "MODEL")
private String model;

当我执行以下测试时:

@Test
public void testSaveToDataBaseWithFiles() {

HTMLDataExtractorServiceImpl htmlDataExtractorService = new HTMLDataExtractorServiceImpl();
Map<String, MyJob> jobsMap = htmlDataExtractorService.parsePage("/Report.html");
List<MyJob> jobs = new ArrayList<MyJob>(jobsMap.values());

List<MyJob> myjobs = dbDataManipulatorService.saveToDataBase(jobs);

jobsMap = myJobsListToMyJobsMap(myjobs);

MyJob myJob = jobsMap.get("20316408");

assertEquals("SUCCESS",myJob.getJobStatus());
assertEquals("50005", myJob.getAircraft().getAircraftId());
assertEquals("WP6_27", myJob.getWorkPackage().getWorkPackageId());

}

我收到以下错误:

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.domain.Aircraft with id 50004; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.domain.Aircraft with id 50004

我知道save方法有问题,但不知道如何解决。请问有人可以帮忙吗?

最佳答案

很难猜测,因为我们不知道您从中得到什么

Map<String, MyJob> jobsMap = htmlDataExtractorService.parsePage("/Report.html");

但我的猜测是缺少级联配置。您的 jobsMap 似乎包含与 id 5004 的飞机关联的 MyJob 对象,但是该飞机之前似乎并未保留。如果您将关联标记为

@ManyToOne(cascade = CascadeType.ALL)

@ManyToOne(cascade = CascadeType.PERSIST)

JPA 应该会自动为您保存飞机。

关于java - 无法使用 spring 从我的数据库检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22972548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com