Spring, 1 form 2 entities
up vote
0
down vote
favorite
I want to save 2 x entity
, author
and book
from a single form. How I can do this?
I know I must first save author, but I don't know how. Author
is in a one-to-many relationship with book
.
I have set cascadetype.ALL
. I'm using thymeleaf.
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" th:field="*{author.name}"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
authorCommand
@Setter
@Getter
@NoArgsConstructor
public class AuthorCommand {
private long id;
private String name;
private String lastName;
private Set<BookCommand> bookCommandSet = new HashSet<>();
}
bookCommand
@Setter
@Getter
@NoArgsConstructor
public class BookCommand {
private long id;
private String title;
private String isbn;
private String description;
private Set<CategoryCommand> categorySet = new HashSet<>();
private AuthorCommand author;
}
bookController
@RequestMapping(value = "book/new", method = RequestMethod.GET)
public String newBook(Model model){
model.addAttribute("book", new BookCommand());
return "book/form";
}
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand){
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
java spring thymeleaf
add a comment |
up vote
0
down vote
favorite
I want to save 2 x entity
, author
and book
from a single form. How I can do this?
I know I must first save author, but I don't know how. Author
is in a one-to-many relationship with book
.
I have set cascadetype.ALL
. I'm using thymeleaf.
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" th:field="*{author.name}"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
authorCommand
@Setter
@Getter
@NoArgsConstructor
public class AuthorCommand {
private long id;
private String name;
private String lastName;
private Set<BookCommand> bookCommandSet = new HashSet<>();
}
bookCommand
@Setter
@Getter
@NoArgsConstructor
public class BookCommand {
private long id;
private String title;
private String isbn;
private String description;
private Set<CategoryCommand> categorySet = new HashSet<>();
private AuthorCommand author;
}
bookController
@RequestMapping(value = "book/new", method = RequestMethod.GET)
public String newBook(Model model){
model.addAttribute("book", new BookCommand());
return "book/form";
}
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand){
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
java spring thymeleaf
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (long
can't be null so "unknown" must be represented by some other value, e.g. -1).
– Thomas
Nov 19 at 13:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to save 2 x entity
, author
and book
from a single form. How I can do this?
I know I must first save author, but I don't know how. Author
is in a one-to-many relationship with book
.
I have set cascadetype.ALL
. I'm using thymeleaf.
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" th:field="*{author.name}"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
authorCommand
@Setter
@Getter
@NoArgsConstructor
public class AuthorCommand {
private long id;
private String name;
private String lastName;
private Set<BookCommand> bookCommandSet = new HashSet<>();
}
bookCommand
@Setter
@Getter
@NoArgsConstructor
public class BookCommand {
private long id;
private String title;
private String isbn;
private String description;
private Set<CategoryCommand> categorySet = new HashSet<>();
private AuthorCommand author;
}
bookController
@RequestMapping(value = "book/new", method = RequestMethod.GET)
public String newBook(Model model){
model.addAttribute("book", new BookCommand());
return "book/form";
}
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand){
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
java spring thymeleaf
I want to save 2 x entity
, author
and book
from a single form. How I can do this?
I know I must first save author, but I don't know how. Author
is in a one-to-many relationship with book
.
I have set cascadetype.ALL
. I'm using thymeleaf.
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" th:field="*{author.name}"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
authorCommand
@Setter
@Getter
@NoArgsConstructor
public class AuthorCommand {
private long id;
private String name;
private String lastName;
private Set<BookCommand> bookCommandSet = new HashSet<>();
}
bookCommand
@Setter
@Getter
@NoArgsConstructor
public class BookCommand {
private long id;
private String title;
private String isbn;
private String description;
private Set<CategoryCommand> categorySet = new HashSet<>();
private AuthorCommand author;
}
bookController
@RequestMapping(value = "book/new", method = RequestMethod.GET)
public String newBook(Model model){
model.addAttribute("book", new BookCommand());
return "book/form";
}
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand){
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
java spring thymeleaf
java spring thymeleaf
edited Nov 19 at 14:40
MTCoster
2,68521838
2,68521838
asked Nov 19 at 13:21
Paweł Hozer
21
21
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (long
can't be null so "unknown" must be represented by some other value, e.g. -1).
– Thomas
Nov 19 at 13:29
add a comment |
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (long
can't be null so "unknown" must be represented by some other value, e.g. -1).
– Thomas
Nov 19 at 13:29
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (
long
can't be null so "unknown" must be represented by some other value, e.g. -1).– Thomas
Nov 19 at 13:29
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (
long
can't be null so "unknown" must be represented by some other value, e.g. -1).– Thomas
Nov 19 at 13:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Well, you can always send additional parameters aside from your model, using the name
tag in your inputs.
HTML
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" name="author"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Controller
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand,
@RequestParam("author") String name,
BindingResult bindingResult){
if( bindingResult.hasErrors()) {
return "redirect:/book/new";
}
Author author = new Author();
author.setName(name);
authorService.saveAuthor(author);
savedBook.setAuthor(author);
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
A few things to considered, I don't know how you are separating the author name, but I would add two different inputs, one for the first name and another for the last name. Also, I am assuming the author's id is using @GeneratedValue
.
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Well, you can always send additional parameters aside from your model, using the name
tag in your inputs.
HTML
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" name="author"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Controller
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand,
@RequestParam("author") String name,
BindingResult bindingResult){
if( bindingResult.hasErrors()) {
return "redirect:/book/new";
}
Author author = new Author();
author.setName(name);
authorService.saveAuthor(author);
savedBook.setAuthor(author);
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
A few things to considered, I don't know how you are separating the author name, but I would add two different inputs, one for the first name and another for the last name. Also, I am assuming the author's id is using @GeneratedValue
.
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
add a comment |
up vote
0
down vote
Well, you can always send additional parameters aside from your model, using the name
tag in your inputs.
HTML
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" name="author"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Controller
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand,
@RequestParam("author") String name,
BindingResult bindingResult){
if( bindingResult.hasErrors()) {
return "redirect:/book/new";
}
Author author = new Author();
author.setName(name);
authorService.saveAuthor(author);
savedBook.setAuthor(author);
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
A few things to considered, I don't know how you are separating the author name, but I would add two different inputs, one for the first name and another for the last name. Also, I am assuming the author's id is using @GeneratedValue
.
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
add a comment |
up vote
0
down vote
up vote
0
down vote
Well, you can always send additional parameters aside from your model, using the name
tag in your inputs.
HTML
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" name="author"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Controller
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand,
@RequestParam("author") String name,
BindingResult bindingResult){
if( bindingResult.hasErrors()) {
return "redirect:/book/new";
}
Author author = new Author();
author.setName(name);
authorService.saveAuthor(author);
savedBook.setAuthor(author);
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
A few things to considered, I don't know how you are separating the author name, but I would add two different inputs, one for the first name and another for the last name. Also, I am assuming the author's id is using @GeneratedValue
.
Well, you can always send additional parameters aside from your model, using the name
tag in your inputs.
HTML
<form th:object="${book}" th:action="@{/book/}" method="post">
<input type="hidden" class="form-control" th:field="*{id}"/>
<label>title</label>
<input type="text" class="form-control" th:field="*{title}"/>
<label>isbn</label>
<input type="text" class="form-control" th:field="*{isbn}"/>
<label>description</label>
<input type="text" class="form-control" th:field="*{description}"/>
<label>cat</label>
<ul>
<li th:each="category : ${book.getCategorySet()}" th:text="category.getCategory()"></li>
</ul>
<label>author</label>
<input type="text" class="form-control" name="author"/>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
Controller
@RequestMapping(value = "book", method = RequestMethod.POST)
public String saveOrUpdate(@ModelAttribute("book") BookCommand bookCommand,
@RequestParam("author") String name,
BindingResult bindingResult){
if( bindingResult.hasErrors()) {
return "redirect:/book/new";
}
Author author = new Author();
author.setName(name);
authorService.saveAuthor(author);
savedBook.setAuthor(author);
BookCommand savedBook = bookService.saveBookCommand(bookCommand);
return "redirect:/book/show/"+savedBook.getId();
}
A few things to considered, I don't know how you are separating the author name, but I would add two different inputs, one for the first name and another for the last name. Also, I am assuming the author's id is using @GeneratedValue
.
edited Nov 19 at 20:28
answered Nov 19 at 17:52
Alain Cruz
1,6841818
1,6841818
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
add a comment |
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
When I try your solve, I got error : Validation failed for object='book'. Error count: 1
– Paweł Hozer
Nov 19 at 20:22
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Yes, author's id is using @generatedvalue
– Paweł Hozer
Nov 19 at 20:23
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
Are you validating your entity? If you are validating your book entity and you are expecting your author to be not null, your binding will fail.
– Alain Cruz
Nov 19 at 20:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375549%2fspring-1-form-2-entities%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You basically have two options: 1) create the author first, i.e. use 2 forms and only allow the user to select an existing author for a new book or 2) handle new/unknown authors in your controller, especially when the autor's id is unknown (
long
can't be null so "unknown" must be represented by some other value, e.g. -1).– Thomas
Nov 19 at 13:29