子实体元素不会在休眠和弹簧数据jpa的一对多映射中持续存在

时间:2018-07-31 13:43:45

标签: java spring hibernate spring-data-jpa

我已经将Spring Boot与Hibernate结合使用了。并大摇大摆地生成dto和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 个答案:

答案 0 :(得分:1)

您需要在拥有方设置ProjectEntity的ID(即ApplicationEntity

appEntity.setProjectApp(project);

否则,休眠(和您的数据库)不知道ApplicationEntity属于哪个父级。

答案 1 :(得分:0)

这里是一个与弹簧数据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);
}

}