can anyone help me with this little problem for me? (My question is in the bottom)
Author class
import javax.persistence.*;
@Entity
@Table(name="authors")
public class Author {
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private Biography biography;
// Getters and Setters
}
Biography class
import javax.persistence.*;
@Entity
@Table(name="biographies")
public class Biography {
@Id
@Column(name="author_id")
private Integer authorId;
@Column(name="information")
private String information;
// Getters and Setters
}
Main class
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Author author = new Author();
author.setName("Jack London");
session.persist(author);
Biography biography = new Biography();
biography.setInformation("Jack London was an American author...");
biography.setAuthorId(author.getId());
author.setBiography(biography);
session.save(author);
session.getTransaction().commit();
session.close();
}
But i want to apply this relationship to this architecture: I using Hibernate-Annotations with Spring-MVC and JSP for the view. My source code is as follow:
AuthorDao>AuthorDaoImpl (BigraphyDao>BigraphyDaoImpl is same)
// imports
@Repository("authorDao")
public class AuthorDaoImpl implements AuthorDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void createAuthor(Author author) {
sessionFactory.getCurrentSession().saveOrUpdate(author);
}
@Override
public Author updateAuthor(int id) {
return (Author) sessionFactory.getCurrentSession().get(Author.class, id);
}
@Override
public void deleteAuthor(int id) {
sessionFactory.getCurrentSession().createQuery("DELETE FROM Author WHERE id="+id).executeUpdate();
}
@Override
@SuppressWarnings("unchecked")
public List<Author> readAuthors() {
return (List<Author>) sessionFactory.getCurrentSession().createCriteria(Author.class).list();
}
}
AuthorService>AuthorServiceImpl (BiographyService>BiographyServiceImpl is same)
// imports
@Service("authorService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AuthorServiceImpl implements AuthorService {
@Autowired
private AuthorDao authorDao;
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void createAuthor(Author author) {
authorDao.createAuthor(author);
}
@Override
public Author updateAuthor(int id) {
return authorDao.updateAuthor(id);
}
@Override
public void deleteAuthor(int id) {
authorDao.deleteAuthor(id);
}
@Override
public List<Author> readAuthors() {
return authorDao.readAuthors();
}
}
Author Controller (Biography Controller is same)
// imports
@Controller
public class AuthorController {
@Autowired
private AuthorService authorService;
@RequestMapping(value = "/saveAuthor", method = RequestMethod.POST)
public ModelAndView saveAuthor(@ModelAttribute("command") Author author, BindingResult result) {
authorService.createAuthor(author);
return new ModelAndView("redirect:/createAuthor.html");
}
@RequestMapping(value = "/createAuthor", method = RequestMethod.GET)
public ModelAndView createAuthor(@ModelAttribute("command") Author author, BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("authors", authorService.readAuthor());
return new ModelAndView("createAuthor", model);
}
@RequestMapping(value = "/updateAuthor", method = RequestMethod.GET)
public ModelAndView updateAuthor(@ModelAttribute("command") Author author, BindingResult result) {
// does not matter the content here
return null;
}
@RequestMapping(value = "/deleteAuthor", method = RequestMethod.GET)
public ModelAndView deleteAuthor(@ModelAttribute("command") Author author, BindingResult result) {
// does not matter the content here
return null;
}
@RequestMapping(value="/readAuthors", method = RequestMethod.GET)
public List<Author> readAuthors(){
return authorService.readAuthors();
}
JSP form
<form:form method="POST" action="/app/saveAuthor.html">
<p>
<form:label path="name">Titulo:</form:label>
<form:input path="name" value="${author.name}"/>
</p>
<!-- here is the problem with the biography values-->
<p>
<button type="reset" >Reset</button>
<button type="submit" value="Save" >Save</button>
</p>
</form:form>
How can i send values (author name and biography information) from my jsp form to its respective tables in my database. Please if you need more details just ask me.
To add the biography information, it should just be a case of adding the respective fields to the form for the
biography
nested object - using dot notation in the path.Also note that the
<form:input>
tag doesn't take avalue
attribute. Spring will set the value automatically.