0

我有这个模型和休眠映射:

@Entity
@Table(name = "candidate")
public class Candidate extends Person {

   private Set<Vacancy> vacancies= new HashSet<Vacancy>();

   @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   @JoinTable(name = "candidate_vacancy", joinColumns = @JoinColumn(name = "candidate_id"), inverseJoinColumns = @JoinColumn(name = "vacancy_id"))
        public Set<Vacancy> getVacancies() {
        return vacancies;
    }

    public void setVacancies(Set<Vacancy> vacancies) {
        this.vacancies = vacancies;
    }
}

人物类:

            @MappedSuperclass
            public abstract class Person {
            private Integer id;
            private String name;
            @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY )
                @Column (name = "id")
                public Integer getId() {
                    return id;
                }

                public void setId(Integer id) {
                    this.id = id;
                }
             @Column(name = "name")
             @NotEmpty
             public String getName() {
              return name;
             }

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


 }

空缺映射

@Entity
@Table(name = "vacancy")
@XmlRootElement(name="vacancy")

    public class Vacancy {
    ...
    @ManyToMany(mappedBy = "vacancies", fetch = FetchType.EAGER)
    public Set<Candidate> getCandidates() {
            return candidates;
    ...
       }
}

我有这样的html表单:

<form action="saveCandidate" method="POST">
        name: <input type="text" value="${candidate.name}" name="name" />
        <input  type="hidden" name="id" value="${candidate.id}"> 
                <input  type="submit" value="save changes" />
</form>

我在@controller 方法中处理我的表单:

 public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
    candidateService.update(candidate);
            return "candidateMenu";
   }

使用这种方式,我有问题。从我的数据库中删除与候选日期相关的空缺。我这样修复它:

public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
            Candidate candidate2 =  candidateService.findById(candidate.getId());
        candidate.setName(candidate2.getName());
        candidate.setSurname(candidate2.getSurname());
        candidate.setPhone(candidate2.getPhone());
        candidate.setResumeUrl(candidate2.getResumeUrl());
        candidateService.update(candidate2);
        return "candidateMenu";
    }

但我认为这是解决我的问题的非常糟糕的方法。

PS我知道 fetchType.lazy 不好,但我不想替换它。

4

1 回答 1

1

您需要先从数据库加载您的实体,然后让 spring 将请求参数绑定到您的实体属性。

所以像下面这样的东西可能会起作用

    @ModelAttribute
    public Candidate loadCandidate(@RequestParam(value="id",required=false) Long id) {
      Candidate candidate=null;
      if(id!=null){
        candidate=candidateService.findById(id);
      }
      else{
       candidate=new Candidate();
      }

      return candidate;
     }

    @RequestMapping(.......)
    public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
        candidateService.update(candidate);
          return "candidateMenu";
     }

由于loadCandidate()方法是用@ModelAttributeannotation 注释的,所以loadCandidate()将在控制器中的任何 requestmapping 处理程序之前调用。所以在你的情况下loadCandidate()将在之前被调用saveCandidate()

于 2013-08-14T08:49:27.107 回答