【问题标题】:Primary Key Join Column with Spring MVC and Hibernate Annotations使用 Spring MVC 和 Hibernate 注释的主键连接列
【发布时间】:2013-12-03 23:45:56
【问题描述】:

谁能帮我解决这个小问题? (我的问题在底部)

作者类

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  

}  

传记课

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

} 

主类

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();

}

但我想将此关系应用于此架构:我将 Hibernate-Annotations 与 Spring-MVC 和 JSP 一起用于视图。我的源代码如下:

AuthorDao>AuthorDaoImpl(BigraphyDao>BigraphyDaoImpl 相同)

// 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 相同)

// 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();

}

}

作者控制器(传记控制器相同)

// 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 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>

如何将值(作者姓名和传记信息)从我的 jsp 表单发送到我数据库中的相应表。如果您需要更多详细信息,请直接问我。

【问题讨论】:

  • 按保存时会发生什么?您看到错误了吗?
  • 嗨@WillKeeling,我没有看到错误,但我无法在第二个表(空传记表)中保存值。我想我错过了与传记路径相关的形式。我不知道如何设置传记路径。

标签: java jsp spring-mvc hibernate-annotations


【解决方案1】:

要添加传记信息,只需将相应字段添加到biography 嵌套对象的表单中 - 在路径中使用点符号。

另请注意,&lt;form:input&gt; 标记不采用 value 属性。 Spring 会自动设置该值。

<form:form method="POST" action="/app/saveAuthor.html">
  <p>
    <form:label path="name">Titulo:</form:label>
    <form:input path="name" />
  </p>
  <!-- Biography information -->
  <p>
    <form:label path="biography.information">Biography:</form:label>
    <form:input path="biography.information" />
  </p>
  <p>
    <button type="reset" >Reset</button>
    <button type="submit" value="Save" >Save</button> 
  </p>
</form:form>

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2017-06-17
    相关资源
    最近更新 更多