Persist data in associated tables from single screen
vikram.vikram.chhetry.gmail.com Feb 25, 2008 11:25 AMHi 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:-
Name----------TextBox
Phone 1-------TextBox
Phone 2-------TextBox
Save Button
Entity beans:-
User.java
@Entity @Table(name = "user") @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; } @Id @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; } }
UserPhone.java
@Entity @Table(name = "user_phone") @Name("userPhone") 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; } @Id @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; } }
AddUserAction.java
@In User user; public void addUser() { // implement your business logic here log.info("addUser.addUser() action called with: #{user.name}"); entityManager.persist(user); facesMessages.add("addUser #{user.name}"); }
xhtml
<h:form id="addUserForm"> <rich:panel> <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" value="#{user.name}"/> </s:decorate> <s:decorate id="numberDecoration" template="layout/edit.xhtml"> <ui:define name="label">Phone1</ui:define> <h:inputText id="number" required="true" value="#{user.userPhones}"/> </s:decorate> <s:decorate id="numberDecoration1" template="layout/edit.xhtml"> <ui:define name="label">Phone2</ui:define> <h:inputText id="number1" required="true" value="#{user.userPhones}"/> </s:decorate> <div style="clear:both"/> </rich:panel> <div class="actionButtons"> <h:commandButton id="addUser" value="addUser" action="#{addUser.addUser}"/> </div> </h:form>
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.
-Vikram