简体   繁体   English

Spring和Hibernate:如何在一个表单中实现多个POJO

[英]Spring and Hibernate : How to implement more than 1 POJO in one form

I am new to Spring framework. 我是Spring框架的新手。

I have a case I couldn't solve my self. 我有一个案例,我无法解决自己。 I have two POJO, ie: PersonData and Address. 我有两个POJO,即:PersonData和Address。 Both of this POJO save on separated table on a database. 这两个POJO都保存在数据库的分离表上。 I mapped both of the POJO to the database tables using Hibernate. 我使用Hibernate将POJO映射到数据库表。

My problem is how to store the POJOs data(PersonData and Address) to relevant tables using one form. 我的问题是如何使用一个表单将POJO数据(PersonData和Address)存储到相关表。

My question is : How to insert data from one form to multiple tables using hibernate? 我的问题是:如何使用hibernate将数据从一个表单插入多个表?

Create one object that holds both of the POJOs and set this as your form. 创建一个包含两个POJO的对象,并将其设置为表单。 For example you could create an object with something like; 例如,您可以使用类似的东西创建一个对象;

class PersonFormObejct {
  private PersonData personData;
  private Address address;

  // getters and setters
}

Make this your form object rather than either of the two POJOs. 将其设为表单对象而不是两个POJO中的任何一个。

You should be able to access both of the member objects using the notation personFormObject.personData.foo or personFormObject.address.postcode. 您应该能够使用符号personFormObject.personData.foo或personFormObject.address.postcode访问两个成员对象。 This is assuming that you named the form object personFormObject of course. 这假设您当然将表单对象命名为personFormObject。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM