简体   繁体   English

分离的实体传递给持久对象:一对一关系

[英]detached entity passed to persist: one to one to relationship

I have an Entity Survey like this: 我有一个这样的实体调查:

@Entity
public class SurveyData {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SURVEY_ID")
private Long Id;

@ManyToOne
@JsonBackReference
@JoinColumn(name = "client_id")
public Client client;

@OneToOne
@JsonManagedReference
@JoinColumn(name = "surveyresult_id")
private SurveyDataResults surveyDataResults;

private Character unit;
..and other fields

And another Entity the SurveyDataResults like this : 另一个实体的SurveyDataResults如下所示:

 @Entity
 @Table(name = "surveydataresults")
public class SurveyDataResults {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SURVEYRESULT_ID")
private Long Id;
..and other fields

SurveyData gets filled from a POST form, and after that i saved, there's some calculations made with those fields and then saved on the SurveyDataResult. SurveyData从POST表单中填写,在我保存之后,使用这些字段进行了一些计算,然后保存在SurveyDataResult中。 When I submit the form for the first time, it gets saved on SurveyData and it holds a reference to the first SurveyDataResults, with id 1 where all the results are. 当我第一次提交表单时,它会保存在SurveyData上,并保存对第一个SurveyDataResults的引用,其中ID为1,所有结果都在其中。 When I fill out the form for the second time with values I get an error : detached entity passed to persist:SurveyDataResults 当我第二次使用值填写表单时,出现错误:分离实体传递给persist:SurveyDataResults

The problem I see, is that if I fill out the form one after another, the results of the second form submitted get saved on the SurveyDataResults with ID=1. 我看到的问题是,如果我一个接一个地填写表单,则提交的第二个表单的结果将保存在ID = 1的SurveyDataResults上。 Therefore the detached entity issue,because it has already been saved once. 因此,分离实体问题,因为它已被保存一次。 How do I make it that after every form submission the mapping to be accordingly ? 如何确保在每个表单提交后都相应地映射?

EDIT: 编辑:

My Post Controller : 我的后控制器:

@RequestMapping(value = "/user/calculate", method = RequestMethod.POST)
public ModelAndView createNewSurvey(@Valid SurveyData survey_data, BindingResult bindingResult) {

    long clientId = survey_data.client.getId();
    SurveyData newSurvey = surveyService.saveSurvey(survey_data);
    Long surveyId = newSurvey.getId();
    calculateService.CalculateFirst(surveyId);
    calculateService.CalculateSecond(surveyId);
    calculateService.CalculateThird(surveyId);
    =
    return new ModelAndView("redirect:/user/surveys?getId=" + clientId);
}

And in the calculateService I save every field using .save like this : surveyServiceResults.saveSurveyResults(surveyresults); 然后在calculateService中,使用.save这样保存每个字段,如下所示:surveyServiceResults.saveSurveyResults(surveyresults);

 @Service("calculateService")
public class CalculateServiceImpl implements CalculateService {
@Autowired
private SurveyDataRepository surveyDataRepository;
SurveyDataResults surveyresults = new SurveyDataResults();
@Autowired
private SurveyServiceResults surveyServiceResults;

 public void CalculateFirst(Long id){
    SurveyData survey = surveyDataRepository.findOne(id);
    Integer c=survey.getA()+survey.getB();
    surveyResults.setC(c);
    surveyServiceResults.saveSurveyResults(surveyresults);
}
 public void CalculateSecond(Long id){
    SurveyData survey = surveyDataRepository.findOne(id);
    Integer D=survey.getB()+survey.getM();
    surveyResults.setD(D);
    surveyServiceResults.saveSurveyResults(surveyresults);
}

Service: 服务:

public interface SurveyServiceResults {

public void saveSurveyResults(SurveyDataResults surveyresults);

} }

Implementation: 执行:

@Service("surveyServiceResults")

public class SurveyServiceResultsImpl implements SurveyServiceResults { 公共类SurveyServiceResultsImpl实现SurveyServiceResults {

@Autowired
private SurveyDataResultsRepository surveyDataResultsRepository;

@Override
public void saveSurveyResults(SurveyDataResults surveyresults) {
    surveyDataResultsRepository.save(surveyresults);
}
}

Repository : 仓库:

@Repository("surveyDataResults")
public interface SurveyDataResultsRepository  extends 
JpaRepository<SurveyDataResults, Long>{}

This is the error I get : 这是我得到的错误:

detached entity passed to persist: com.test.test1.model.SurveyDataResults; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.test.test1.model.SurveyDataResults

An error is certainly in the CalculateServiceImpl class, where you declare the surveryresults as a class member field. CalculateServiceImpl类中肯定存在错误,您在其中将surveryresults声明为类成员字段。 This is wrong to do in classes like services and repositories, you should try not to hold state information in such classes. 在诸如服务和存储库之类的类中这样做是错误的,您应尽量不要在此类中保存state信息。 This surveyresults once the service is instantiated (from Spring) is the same through all the calls to CalculateXXX , that's why you get the detached entity error. 通过从CalculateXXX进行的所有调用(从Spring实例化)该服务后,此surveyresults都是相同的,这就是为什么出现分离的实体错误的原因。

SurveyDataResults surveyresults = new SurveyDataResults();

So remove this line from there, and load and work with SurveyDataResults from inside your CalculateXXX methods 因此,从此处删除此行,并从CalculateXXX方法内部加载并使用SurveyDataResults

An example can be the following, however I dont know if it will work as expected because you have not posted all your code (see my inline comment for example) 下面是一个示例,但是我不知道它是否会按预期工作,因为您尚未发布所有代码(例如,请参阅我的嵌入式注释)

public void CalculateFirst(Long id){
    SurveyData survey = surveyDataRepository.findOne(id);
    Integer c=survey.getA()+survey.getB();
    SurveyDataResults  surveyResults = survey.getSurveyresults();
    if (surveyResults == null) {
        surveyResults = new SurveyDataResults();
        surveyResults.setSurvey(survey); //not present in your code but I assume it exists
    }
    surveyResults.setC(c);
    surveyServiceResults.saveSurveyResults(surveyresults);
}

UPDATE UPDATE

Another point is the way you are saving the surveyResults, via the SurveyDataResultsRepository . 另一点是您通过SurveyDataResultsRepository保存SurveyResults的SurveyDataResultsRepository If you do not set the survey via a setter method in SurveyDataResults like the one in my code example above, you have no relation of SurveyData and SurveyDataResults . 如果您没有像上面我的代码示例中那样通过SurveyDataResults的设置方法设置survey ,则没有SurveyDataSurveyDataResults关系。 If you don't have a SurveyData field inside the SurveyDataResults entity, then you should set the SurveyDataResults to SurveyData and save the SurveyData by calling the SurveyDataRepository.save and not via the SurveyDataResultsRepository . 如果你没有一个SurveyData的场内SurveyDataResults实体,那么你应该设置SurveyDataResultsSurveyData并保存SurveyData通过调用SurveyDataRepository.save而不是通过SurveyDataResultsRepository

eg 例如

public void CalculateFirst(Long id){
    SurveyData survey = surveyDataRepository.findOne(id);
    Integer c=survey.getA()+survey.getB();
    SurveyDataResults  surveyResults = survey.getSurveyresults();
    if (surveyResults == null) {
        surveyResults = new SurveyDataResults();
        survey.setSurveyDataResults(surveyResults);
    }
    surveyResults.setC(c);
    surveyDataRepository.save(survey);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修复了在 JPA 中传递以保持一对一关系的分离实体 - Fix detached entity passed to persist in one to one relationship in JPA 独立实体通过以一对多关系持久化Spring - Detached entity passed to persist Spring in one to many relationship 传递给持久化的分离实体 - detached entity passed to persist 分离的实体已传递以保留 - Detached entity passed to persist Spring Data JPA:一对一实例化问题:PersistentObjectException:分离实体传递给持久化 - Spring Data JPA: one-to-one instantiation problem: PersistentObjectException: detached entity passed to persist 无法在一对一关系 Spring 引导上插入 null object | 嵌套异常是.PersistentObjectException:传递给持久化的分离实体 - Can not insert null object on One to One relation Spring boot | nested exception is .PersistentObjectException: detached entity passed to persist 通过获取分离实体以保持@ManyToMany双向关系 - Getting detached entity passed to persist with @ManyToMany bidirectional relationship Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship Hibernate:将现有的子实体添加到具有 OneToMany 双向关系的新实体并持久化(“分离的实体传递给持久化”) - Hibernate: add existing child entity to new entity with OneToMany bidirectional relationship and persist it ('detached entity passed to persist') org.hibernate.PersistentObjectException:传递给持久化的分离实体:多对一单向 - org.hibernate.PersistentObjectException: detached entity passed to persist: Many to one unidirectional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM