[jboss-user] [JBoss Seam] - Re: Please help ! Can't persist data into related tables

nickarls do-not-reply at jboss.com
Wed Feb 6 17:39:37 EST 2008


POJO quick and dirty style:

Let's say you have an entity


  | @Entity
  | public class User {
  | 	@Id	@GeneratedValue
  | 	private long id;
  | 	private String name;
  | 
  | 	public long getId() {
  | 		return id;
  | 	}
  | 
  | 	public void setId(long id) {
  | 		this.id = id;
  | 	}
  | 
  | 	public String getName() {
  | 		return name;
  | 	}
  | 
  | 	public void setName(String name) {
  | 		this.name = name;
  | 	}
  | }
  | 

and then an action class (you could save a few lines by extending EntityController)


  | @Name("userActions")
  | public class UserActions {
  | 	@In
  | 	private EntityManager entityManager;
  | 	@In
  | 	private FacesMessages facesMessages;
  | 	
  | 	private User user = new User();
  | 	
  | 	public void insert() {
  | 		entityManager.persist(user);
  | 		facesMessages.add("Added user #0", user.getName());
  | 	}
  | 
  | 	public User getUser() {
  | 		return user;
  | 	}
  | 
  | 	public void setUser(User user) {
  | 		this.user = user;
  | 	}
  | }
  | 

and the view


  |     <h:messages globalOnly="true" styleClass="message"/>
  |     <h:form>
  |     	<h:inputText value="#{userActions.user.name}"/>
  |     	<h:commandButton action="#{userActions.insert}" value="Insert!"/>
  |     </h:form>
  | 

For each click on the button, the userActions.insert is called. Since the input text is connected to userActions.user.name, it populates the newly created entity in the bean and persist has something to work on.

If you would have a 

  | Name("user")
  | 

on the user class, you would have linked the input field to user.name and then injected the user into the userAction bean with


  | @In(create=true)
  | private User user;
  | 

and persist would then have had something to work on. Likewise, if the entity resides in another bean, you connect the input field to someotherbean.user.name and then inject that bean (or the user from that bean) using the @In annotation again. So that your action class has some data to work on.

It is really worth to give the in/outjection part of the manual a good read since it is pretty much the core of the framework...

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4127201#4127201

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4127201



More information about the jboss-user mailing list