2017-08-06 8 views
1

最近、JPAとthymeleafでSpringブート(私は主にPython /フラスコとノードの背景から来ています)を使い始めましたが、私のデータベースにProjectを作成しようとしています。すべてうまくいっていた、私は、削除などを追加することができた。プロジェクト。thymeleafとSpringBootを使ってエンティティを保存できません

@Entity 
@Table(name = "project") 
public class Project { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name = "project_id") 
private int id; 

@Column(name = "title") 
@NotEmpty(message = "*Please provide a title") 
private String title; 

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

@OneToOne(cascade = CascadeType.ALL) 
@JoinTable(name = "project_lead", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) 
private User projectLead; 

@ManyToMany(cascade = CascadeType.ALL) 
@JoinTable(name = "project_user", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) 
private Set<User> users; 
... 
} 

ユーザークラスは次のようになります:

私はこのようになります私のプロジェクトのエンティティに変数を設定し、ユーザーを追加

私はときに私の新しいプロジェクトを作成することができるよ
@Entity 
@Table(name = "user") 
public class User { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name = "user_id") 
private int id; 

@Column(name = "email") 
@Email(message = "*Please provide a valid Email") 
@NotEmpty(message = "*Please provide an email") 
private String email; 

@Column(name = "username") 
@NotEmpty(message = "*Please provide a username") 
private String username; 

@Column(name = "password") 
@Length(min = 5, message = "*Your password must have at least 5 characters") 
@NotEmpty(message = "*Please provide your password") 
@Transient 
private String password; 

@Column(name = "enabled") 
private int enabled; 

@ManyToMany(cascade = CascadeType.ALL) 
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) 
private Set<Role> roles; 
... 
} 

プロジェクトのユーザーを指定しないでください。しかし、私は複数の選択を使用してユーザーを指定するときにプロジェクトを作成するのに問題があります。私のフォームでは私が持っている:

<form th:action="@{/secure/projects}" th:object="${project}" method="POST"> 
     <div class="form-group"> 
      <label th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="validation-message"></label> 
      <input type="text" class="form-control" th:field="*{title}" id="title" th:placeholder="Title" /> 
     </div> 

     <div class="form-group"> 
      <input type="text" class="form-control" th:field="*{description}" id="description" th:placeholder="Description" /> 
     </div> 

     <div class="form-group"> 
      <select th:field="*{users}" class="users-select form-control" multiple="multiple"> 
       <option th:each="user : ${allUsers}" th:value="${user}" th:text="${user.username}"></option> 
      </select> 
     </div> 


     <button name="Submit" value="Submit" type="Submit" th:text="Create"></button> 
    </form> 

I保つ取得番目は 『プロパティ '『必要なタイプに』はjava.lang.String型java.util.Setユーザーが「」プロパティ型の値を変換するのに失敗しました』 :field = "* {ユーザー}"。私は理解していないth:value = "$ {user}"は、クラスUserであると想定されるときにStringと見なされます。選択した結果を単純に取得し、それをループして、手動でコントローラーでオブジェクトプロジェクトに追加する方法はありますか?

私のコントローラは次のようになります。

@RequestMapping(value = "/projects", method=RequestMethod.GET) 
public ModelAndView showProjectForm() { 
    ModelAndView modelAndView = new ModelAndView(); 
    // Get authenticated user 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    User user = userService.findByEmail(auth.getName()); 

    modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")"); 
    modelAndView.addObject("project", new Project()); 
    modelAndView.addObject("allUsers", userService.findAll()); 
    modelAndView.setViewName("project_creation"); 
    return modelAndView; 
} 

@RequestMapping(value = "/projects", method=RequestMethod.POST) 
public ModelAndView processProjectForm(@Valid Project project, BindingResult bindingResult) { 
    ModelAndView modelAndView = new ModelAndView(); 
    // Get authenticated user 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    User user = userService.findByEmail(auth.getName()); 

    // Get all projects 
    List<Project> allProjects = projectService.findAll(); 

    // Check if project already exists 
    Project projectExist = projectService.findByTitle(project.getTitle()); 
    if(projectExist != null) { 
     bindingResult 
     .rejectValue("title", "error.project", 
       "There is already a project with this title"); 
    } 


    // Get all users 
    List<User> allUsers = userService.findAll(); 

    if(bindingResult.hasErrors()) { 
     modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")"); 
     modelAndView.addObject("project", new Project()); 
     modelAndView.addObject("allUsers", allUsers); 
     modelAndView.setViewName("project_creation"); 
    } else { 

     // Create project 
     project.setProjectLead(user); 
     projectService.saveProject(project); 
     modelAndView.addObject("userName", "Welcome " + user.getUsername() + " (" + user.getEmail() + ")"); 
     modelAndView.addObject("success", "Project successfully created!"); 
     modelAndView.addObject("project", new Project()); 
     modelAndView.addObject("projects", allProjects); 
     modelAndView.setViewName("redirect:/secure/dashboard"); 
    } 
    return modelAndView; 
} 

答えて

0

私は「型のプロパティ値を変換に失敗しました 『を解決することができましたjava.lang.Stringでjava.util.Set『』必要なタイプに』プロパティのために「ユーザー」。私は、文字列をUserオブジェクトに変換する方法を伝えるコンバータクラスを追加するだけでした。

@Component 
public class StringToUser implements Converter<String, User> { 

@Autowired 
private UserService userService; 

@Override 
public User convert(String arg0) { 
    Integer id = new Integer(arg0); 
    return userService.findOne(id); 
} 
} 

私のフォームのオプションを、ユーザーオブジェクトの代わりにuser.idという値に変更しました。コンバータは残りの部分を処理します:

<option th:each="user : ${allUsers}" th:value="${user.getId()}" th:text="${user.getUsername()}"></option> 
関連する問題