Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有SpringMVC和Hibernate注释的主键联接列_Java_Jsp_Spring Mvc_Hibernate Annotations - Fatal编程技术网

Java 带有SpringMVC和Hibernate注释的主键联接列

Java 带有SpringMVC和Hibernate注释的主键联接列,java,jsp,spring-mvc,hibernate-annotations,Java,Jsp,Spring Mvc,Hibernate Annotations,有人能帮我解决这个小问题吗?(我的问题在下面) 作者类 import javax.persistence.*; @Entity @Table(name="authors") public class Author { @Id @GeneratedValue private Integer id; private String name; @OneToOne(cascade=CascadeType.ALL) @PrimaryKeyJoinColumn

有人能帮我解决这个小问题吗?(我的问题在下面)

作者类

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

}
传记类

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

}
主类

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注释和SpringMVC以及JSP作为视图。我的源代码如下:

AuthorDao>AuthorDaoImpl(BigraphyDao>BigraphyDaoImpl相同)

//导入
@存储库(“authorDao”)
公共类AuthorDaoImpl实现AuthorDao{
@自动连线
私人会话工厂会话工厂;
@凌驾
public void createAuthor(作者){
sessionFactory.getCurrentSession().saveOrUpdate(作者);
}
@凌驾
公共作者更新器(int-id){
return(Author)sessionFactory.getCurrentSession().get(Author.class,id);
}
@凌驾
public void deleteAuthor(int-id){
sessionFactory.getCurrentSession().createQuery(“从作者处删除,其中id=“+id”).executeUpdate();
}
@凌驾
@抑制警告(“未选中”)
公开列表阅读作者(){
return(List)sessionFactory.getCurrentSession().createCriteria(Author.class.List();
}
}
AuthorService>AuthorServiceImpl(传记服务>传记服务impl相同)

//导入
@服务(“作者服务”)
@事务性(传播=propagation.SUPPORTS,只读=true)
公共类AuthorServiceImpl实现AuthorService{
@自动连线
私家侦探;
@事务性(传播=propagation.REQUIRED,只读=false)
public void createAuthor(作者){
authorDao.createAuthor(作者);
}
@凌驾
公共作者更新器(int-id){
返回authorDao.updateAuthor(id);
}
@凌驾
public void deleteAuthor(int-id){
authorDao.deleteAuthor(id);
}
@凌驾
公开列表阅读作者(){
返回authorDao.readAuthors();
}
}
作者控制器(传记控制器相同)

//导入
@控制器
公共类AuthorController{
@自动连线
私人AuthorService AuthorService;
@RequestMapping(value=“/saveAuthor”,method=RequestMethod.POST)
公共模型和视图保存作者(@modeldattribute(“命令”)作者作者,BindingResult){
authorService.createAuthor(作者);
返回新的ModelAndView(“重定向:/createAuthor.html”);
}
@RequestMapping(value=“/createAuthor”,method=RequestMethod.GET)
public ModelAndView createAuthor(@modeldattribute(“命令”)Author Author,BindingResult){
映射模型=新的HashMap();
model.put(“authors”,authorService.readAuthor());
返回新的ModelAndView(“createAuthor”,model);
}
@RequestMapping(value=“/updateAuthor”,method=RequestMethod.GET)
公共模型和视图更新作者(@modeldattribute(“命令”)作者,BindingResult){
//这里的内容无关紧要
返回null;
}
@RequestMapping(value=“/deleteAuthor”,method=RequestMethod.GET)
公共模型和视图删除作者(@modeldattribute(“命令”)作者作者,BindingResult){
//这里的内容无关紧要
返回null;
}
@RequestMapping(value=“/readAuthors”,method=RequestMethod.GET)
公开列表阅读作者(){
返回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表单到数据库中相应的表。如果需要更多详细信息,请询问我。

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

还要注意的是,
标记不采用
属性。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>


提图洛:

传记:

重置 拯救


按Save时会发生什么?您是否看到错误?嗨@Willkeling,我没有看到错误,但无法将值保存在第二个表(空的传记表)中。我想我在传记路径对应的表单中遗漏了一些内容。我不知道如何设置传记路径。