提交JSP表单时,Hibernate与共享密钥错误的一对一关系

时间:2014-11-11 20:55:29

标签: java hibernate spring-mvc

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

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

表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); 
    }
}

控制器:

@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-fields以及相关的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 |
+------------+-------------+----------+ 

2 个答案:

答案 0 :(得分:1)

好吧,我终于找到了解决办法。对于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
 }

答案 1 :(得分:0)

问题可能是TableB实际上没有针对资源设置TableA。

因此,在您的创建服务代码中,您应该具有以下内容:

tableB.setResource(tableA);

检查您的创建代码并进行上述更改,它将起作用。

也有潜力 另一个问题可能是tableB对象未在tableA中初始化,因此当submited失败时。

在你的tableA课中试试这个:

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

或在构造函数中:

this.tableB = new TableB();

尝试的另一个选择是删除:

@MapsId
表B中的

来自资源字段。那就是:

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

将其更改为

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