当前位置: 首页 > 知识库问答 >
问题:

子实体元素不在hibernate和Spring Data JPA的一对多映射中持久化

郜谦
2023-03-14

我已经在Hibernate中使用了spring boot。和api接口。有两个实体。项目实体是父实体,应用程序实体是子实体。建立了一个单人关系。但当我努力坚持的时候。我没有看到为一个项目添加应用程序。

项目实体:

@Entity
@Table(name="ProjectEntity")
public class ProjectEntity  {

    @Id
    @Column(name = "ProjectGuid", length = 36, nullable = false, unique = true)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long  id;

    @Column(name = "Name")
    private String name;

    @OneToMany(mappedBy="projectApp", cascade = CascadeType.ALL)
    private List<ApplicationEntity> apps=new ArrayList<>();

    public ProjectEntity() {
    }

    public ProjectEntity(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ApplicationEntity> getApps() {
        return apps;
    }

    public void setApps(List<ApplicationEntity> apps) {
        this.apps = apps;
    }
}

应用程序实体:

@Entity
@Table(name="ApplicationEntity")
public class ApplicationEntity   {
    @Id
    @Column(name = "Name", length = 36, nullable = false, unique = true)
    private String name;

    private String repositoryUrl;

    @ManyToOne
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "ProjectGuid")
    private ProjectEntity projectApp;

    public ApplicationEntity() {
    }

    public ApplicationEntity(String name, String repositoryUrl) {
        this.name = name;
        this.repositoryUrl = repositoryUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRepositoryUrl() {
        return repositoryUrl;
    }

    public void setRepositoryUrl(String repositoryUrl) {
        this.repositoryUrl = repositoryUrl;
    }

    public ProjectEntity getProjectApp() {
        return projectApp;
    }

    public void setProjectApp(ProjectEntity projectApp) {
        this.projectApp = projectApp;
    }
}

控制器操作:

ProjectEntity project = projectService.getProject(projectName);
List<ApplicationEntity> appList = new ArrayList<>();
ApplicationEntity appEntity = new ApplicationEntity(app.getName(), app.getRepositoryUrl());
applicationRepository.save(appEntity);
appList.add(appEntity);
project.setApps(appList);
projectRepository.save(project);

共有2个答案

东郭宏朗
2023-03-14

这里有一个与Spring Data JPA有千丝万缕关系的例子:

@Data
@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
}

@Data
@Entity
public class Question extends BaseEntity{
    private String questionText;
    private int anketId;
    private int subjectId;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "question")
    List<Answer> answers;
}

@Data
@Entity
public class Answer extends BaseEntity{
    private String answerText;
    private String code;
    private int score;
    private int priority;

    private boolean isValidAnswer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "question_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Question question;
}

@DataJpaTest
public class QuestionRepositoryTest {

@Autowired
TestEntityManager entityManager;

@Autowired
QuestionRepository sut;

@Test
public void it_should_create_question_wiht_answers() {

    Question question = new Question();
    question.setSubjectId(1);
    question.setAnketId(1);
    question.setQuestionText("test question");

    Answer answer = new Answer();
    answer.setAnswerText("answer");
    answer.setCode("1a");
    answer.setPriority(0);
    answer.setValidAnswer(true);

    question.setAnswers(Arrays.asList(answer));

    entityManager.persistAndFlush(question);

    List<Question> questionList = sut.findAll();

    assertThat(questionList).containsExactly(question);
    assertThat(questionList.get(0).getAnswers().size()).isGreaterThan(0);
}

}
黄兴业
2023-03-14

您需要在拥有方(即ApplicationEntity)上设置ProjectEntity的id

appEntity.setProjectApp(project);

否则,hibernate(和数据库)不知道applicationentity属于哪个父级。

 类似资料:
  • persistenceException:DB2 SQL错误:sqlcode=-206,sqlstate=42703,sqlerrmc=t0.id,driver=3.52.95{prepstmnt 1029586270

  • 当我映射同一个实体时,就像这里回答的那样: Hibernate与同一实体的多对多关联 在“tbl_friends”表中,我有相同含义的行。例如,我有id=1的用户和id=2的用户。在“tbl_friends”表中,当他们作为朋友链接时,我有两行 使用Hibernate或JPA引用是否可以在一行(1-2或2-1)中建立这种关系?

  • 下面的映射给出的错误为 从db.karateInvoiceDetail引用db.karateInvoice的外键的列数错误。应为1 想法是有一个表,它的组合键为(id、fiscalyear和companyId),而表的组合键为(seqNo、InvoiceId、InvoiceFiscalYear和InvoiceCompanyId)。

  • 我正在使用Spring Hibernate开发员工管理应用程序。我有两个实体,员工和部门。并且,员工实体有一个字段“部门”,该字段映射到部门实体,即@manytone。 并相应地在部门实体中进行“OneToMany映射” 我的JSP Spring表单映射到员工实体。它有一个表单:选择元素以选择值为部门ID的部门。 如何在实体中设置对象,以根据表单上的选择进行设置? 控制器

  • 尝试获取父实体(Msg)的实体,其中父PK (msg_id)是子实体中的FK时,尝试保持子实体(MsgRetry)时出错。 错误,如:org.hibernate.id.IdentifierGenerationException:试图从null一对一属性分配id 父实体,不需要知道子实体(至少我认为它不需要知道)。一旦子实体被持久化,我就会尝试也持久化父实体。我可以通过在子实体中没有父实体并调用关联

  • 问题内容: 我正在尝试将实体扩展为用于填充超类字段的非实体。问题是,当我尝试保存它时,Hibernate会抛出MappingException。这是因为即使我将ReportParser强制转换为Report,但运行时实例仍然是ReportParser,因此Hibernate抱怨它是一个未知的实体。 ReportParser仅用于填写字段。 尝试将其投射到报告中并保存 在转移到ORM之前,我已经使用