java - 与 Hibernate 一对一关系,提交 JSP 表单时出现共享 key 错误

标签 java hibernate spring-mvc

我正在尝试创建 2 个表,第一个表有主键,另一个表有主键作为键。我使用Spring mvc + hibernate,提交jsp表单时显示此错误:

尝试从 null 一对一属性 [model.TableB.resource] 分配 id;嵌套异常是 org.hibernate.id.IdentifierGenerationException:尝试从 null 一对一属性 [model.TableB.resource] 分配 id

表A:

@Entity
public class TableA{

    @Id
    private String sNum

    @OneToOne(mappedBy = "resource",cascade = CascadeType.ALL)
    private TableB tableB;

    //other fields, getter setters
}

表B:

@Entity
public class TableB{

    @Id
    private String id

    @MapsId
    @OneToOne
    @JoinColumn(name="resourceId")
    private TableA resource;

    //other fields, getter setters
}

这些实体将以下内容输出到 mysql 中:

TableA
+------+---------------+---------------+-----------------+
| sNum | column1tableA | column2tableA | ..other columns |
+------+---------------+---------------+-----------------+
| PK   |               |               |                 |
+------+---------------+---------------+-----------------+


TableB
+-------------------+---------+-----------------+
|    resourceId     | column1 | ..other columns |
+-------------------+---------+-----------------+
| PK FK from TableA |         |                 |
+-------------------+---------+-----------------+

服务:

public class tableAService {
    @Autowired
    TableARepository tableARepository; //this repository extends from jpa repository

    public void create(TableA tableA){
    tableARepository.save(tableA); 
    }
}

Controller :

@RequestMapping(value = "/createTableA", method = RequestMethod.GET)
public String getCreateTableAPage(Model model){
    if(!model.containsAttribute("tableA"))
        model.addAttribute("tableA", new TableA());

    return "createTableA";
} // this returns the registration page

@RequestMapping(value="/doCreateRe", method = RequestMethod.POST)
public String doCreateRe(@Valid @ModelAttribute("tableA") TableA tableA, BindingResult result){
    if(result.hasErrors()){

        return "redirect:/createPage";
    }try{
        myservice.create(tableA);
        return "viewPage";
    }catch(Exception e){
        System.out.println(e.getMessage());
        return "error";
    }
} // this handles the onsubmit of the registration page

EDIT-2(参见下面的示例输出):提交表单时,表 A 的字符串 id(由用户在表单上手动输入)应保存为表 B 上的外键。例如,我想保存一个tableA类型的对象,我将在jsp表单上手动分配它的id,在同一页面/表单上填充其他tableA字段,加上相关的tableB对象(即tableA.tableB.someField)也存在于表单上(它将在提交“注册”表单时在表A和表B上创建一个新行,表B的键将是表A中的键 - 见下文)

createTableA页面JSP:

<form:form  modelAttribute="tableA" method="POST" action="doCreateRe">
  ...
 <form:input path="sNum" required="true"/> <!-- sample input is abc123 -->
 <form:input path="column1tableA" required="true"/> <!-- sample input is chocolate -->
 <form:input path="column2tableA" required="true"/> <!-- sample input is beer -->
 <form:input path="tableB.column1" required="true"/> <!-- sample input is isDelicious -->
 <form:input path="tableB.column2" required="true"/> <!-- sample input is isBetter -->
 ...
</form:form>

提交表单后应显示在数据库中的输出:

表A:

+------------+---------------+---------------+
|    sNum    | column1tableA | column2tableA |
+------------+---------------+---------------+
| abc123 PK  | chocolate     | beer          |
+------------+---------------+---------------+

表B:

+------------+-------------+----------+
| resourceId |   column1   | column2  |
+------------+-------------+----------+
| abc123 FK  | isDelicious | isBetter |
+------------+-------------+----------+ 

最佳答案

好吧,我终于找到了解决办法。对于 reference

在 TableA 实体上,我需要为其 setter 添加额外的行。

@Entity
public class TableA{

    @Id
    private String sNum

    @OneToOne(mappedBy = "resource",cascade = CascadeType.ALL)
    private TableB tableB;

    public TableB getTableB() {         
        return tableB;
    }

    public void setTableB(TableB tableB) {
        this.tableB = tableB;
        tableB.setResource(this); // added this line of code
    }

    //other fields, getter setters
 }

关于java - 与 Hibernate 一对一关系,提交 JSP 表单时出现共享 key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26874395/

相关文章:

java - 你能告诉我,我的请求映射有什么问题吗?

java - 程序无限循环

java - 如何从android中的谷歌应用引擎数据存储中获取数据

spring - 找不到 "http://tiles.apache.org/tags-tiles"的标签库描述符

java - 找不到类 org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor 的序列化程序

java - 如何在 hibernate 中使用注释进行 ondelete 级联

java - Spring MVC 创建对象请求

java - 请求调度器

java - netty writeAndFlush https 数据报未得到响应

spring-mvc - 如何在同一 Controller 中禁用某些调用 @ModelAttribute 方法的请求?