1 2 Previous Next 16 Replies Latest reply on Mar 16, 2008 5:50 AM by Yuriy ZUbarev

    Persist data in associated tables from single screen

    Vikram Chhetry Newbie

      Hi All,
      I am asking this question second time as I am still not able to fix this problem. I searched for it but could not get the solution.

      I have two tables(This is just a test scenario):-

      1.) user (user_id,name)

      2.) user_phone(user_phone_id,number,user_id)

      I want to insert user details and 2 different phone numbers for a user from a single screen.

      My screen should look like this:-


      Phone 1-------TextBox

      Phone 2-------TextBox

      Save Button

      Entity beans:-

      @Table(name = "user")
      public class User implements java.io.Serializable {
           private Integer userId;
           private String name;
           private List<UserPhone> userPhones = new ArrayList<UserPhone>();
           public User() {
           public User(String name, List<UserPhone> userPhones) {
                this.name = name;
                this.userPhones = userPhones;
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "user_id", unique = true, nullable = false)
           public Integer getUserId() {
                return this.userId;
           public void setUserId(Integer userId) {
                this.userId = userId;
           @Column(name = "name", length = 20)
           @Length(max = 20)
           public String getName() {
                return this.name;
           public void setName(String name) {
                this.name = name;
           @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
           public List<UserPhone> getUserPhones() {
                return this.userPhones;
           public void setUserPhones(List<UserPhone> userPhones) {
                this.userPhones = userPhones;


      @Table(name = "user_phone")
      public class UserPhone implements java.io.Serializable {
           private Integer userPhoneId;
           private User user;
           private String number;
           public UserPhone() {
           public UserPhone(User user, String number) {
                this.user = user;
                this.number = number;
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "user_phone_id", unique = true, nullable = false)
           public Integer getUserPhoneId() {
                return this.userPhoneId;
           public void setUserPhoneId(Integer userPhoneId) {
                this.userPhoneId = userPhoneId;
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "user_id")
           public User getUser() {
                return this.user;
           public void setUser(User user) {
                this.user = user;
           @Column(name = "number", length = 20)
           @Length(max = 20)
           public String getNumber() {
                return this.number;
           public void setNumber(String number) {
                this.number = number;


           User user;
           public void addUser() {
                // implement your business logic here
                log.info("addUser.addUser() action called with: #{user.name}");
                facesMessages.add("addUser #{user.name}");


          <h:form id="addUserForm">
                  <f:facet name="header">addUser</f:facet>
                  <s:decorate id="nameDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Name</ui:define>
                      <h:inputText id="name" required="true"
                  <s:decorate id="numberDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Phone1</ui:define>
                      <h:inputText id="number" required="true"
                  <s:decorate id="numberDecoration1" template="layout/edit.xhtml">
                      <ui:define name="label">Phone2</ui:define>
                      <h:inputText id="number1" required="true"
                  <div style="clear:both"/>
              <div class="actionButtons">
                  <h:commandButton id="addUser" value="addUser" 

      I still do not know if seam supports this type of scenario. If it does then what is the right way of doing it and if it doesn't then how do I do it using hibernate.

      Can anyone please help me on this with some links.

      Any Help would be appreciated.


        1 2 Previous Next