-
1. Re: Seam Hibernate create entities
herberson Jul 12, 2010 3:07 PM (in response to nishp1)Your mapping should be something like this:
@Entity @Table(name="CTA") public class CTA implements Serializable { @Id @Column(name="cta_id") private Integer ctaId; //PK @Column(name="cta_description") private String description; } @Entity @Table(name="LOB") public class LOB implements Serializable { @Id @Column(name="lob_id") private Integer lobId; //PK @Column(name="lob_id") private String description; } @Entity @Table(name="APPLICATION") public class Application implements Serializable { @Id @Column(name="app_id") private Integer applicationId; //PK @Column(name="app_description") private String description; @ManyToOne(targetEntity=Lob.class, fetch = FetchType.LAZY) @JoinColumn(name = "lob_id", nullable = false) private Lob applicationLob; @ManyToMany(targetEntity=CTA.class, fetch=FetchType.LAZY) @JoinTable(name = "application_cta", joinColumns = @JoinColumn(name = "app_id"), inverseJoinColumns = @JoinColumn(name = "cta_id")) private List<CTA> ctaList; }
-
2. Re: Seam Hibernate create entities
nishp1 Jul 17, 2010 4:28 PM (in response to nishp1)Thanks Herberson. That was helpful. I also added a request entity. Goal here is, I am creating a form that allows users to create a request with following properties. Form has 3 fields, lob,application, cta and notes. Employee will be chosen from session and date would be todays date. How can I hibernate select today's date?
Request.java
@Entity @Table(name = "request") public class Request implements java.io.Serializable { private Long requestId; private Application application; private Employee employee; private RequestStatus requestStatus; private Cta cta; private String requestNotes; private Date dateCreated; public Request() { } public Request(Application application, Employee employee, RequestStatus requestStatus, Cta cta, String requestNotes, Date dateCreated) { this.application = application; this.employee = employee; this.requestStatus = requestStatus; this.cta = cta; this.requestNotes = requestNotes; this.dateCreated = dateCreated; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "request_id", unique = true, nullable = false) public Long getRequestId() { return this.requestId; } public void setRequestId(Long requestId) { this.requestId = requestId; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "application_id", nullable = false) public Application getApplication() { return this.application; } public void setApplication(Application application) { this.application = application; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "request_creator", nullable = false) public Employee getEmployee() { return this.employee; } public void setEmployee(Employee employee) { this.employee = employee; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "request_status_id", nullable = false) public RequestStatus getRequestStatus() { return this.requestStatus; } public void setRequestStatus(RequestStatus requestStatus) { this.requestStatus = requestStatus; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "cta_id", nullable = false) public Cta getCta() { return this.cta; } public void setCta(Cta cta) { this.cta = cta; } @Column(name = "request_notes", nullable = false) public String getRequestNotes() { return this.requestNotes; } public void setRequestNotes(String requestNotes) { this.requestNotes = requestNotes; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "date_created", nullable = false, length = 19) public Date getDateCreated() { return this.dateCreated; } public void setDateCreated(Date dateCreated) { this.dateCreated = dateCreated; } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((requestId == null) ? 0 : requestId.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Request)) return false; Request other = (Request) obj; if (requestId == null) { if (other.requestId != null) return false; } else if (!requestId.equals(other.requestId)) return false; return true; } }
CreateRequest.xhtml
<h:form id="requestManagerForm"> <p>Line of Business: <h:selectOneMenu value="#{requestManager.lob}" required="true"> <s:selectItems value="#{lobs}" var="lob" label="#{lob.lobDescription}" noSelectionLabel="Select a LOB"/> <s:convertEntity /> <a:support event="onchange" action="#{requestManager.loadApps()}" ajaxSingle="true" reRender="appField,ctaField"/> </h:selectOneMenu> </p> <p>Application: <h:selectOneMenu id="appField" value="#{requestManager.app}" required="true"> <s:selectItems value="#{requestManager.applications}" var="app" label="#{app.applicationName}" noSelectionLabel="Select an Application"/> <s:convertEntity /> <a:support event="onchange" action="#{requestManager.loadCtas()}" ajaxSingle="true" reRender="ctaField"/> </h:selectOneMenu> </p> <p>CTA: <h:selectOneMenu id="ctaField" value="#{requestManager.cta}" required="true"> <s:selectItems value="#{requestManager.ctas}" var="cta" label="#{cta.ctaDescription}" noSelectionLabel="Select a CTA"/> <s:convertEntity /> </h:selectOneMenu> </p> <p>Statement of work:</p> <rich:editor value="#{requestManager.notes}" width="400" height="200"/> <h:commandButton id="CreateRequest" value="Submit" action="#{requestManager.CreateRequest}"/> </h:form>
RequestManager.java
@Stateless @Name("requestManager") @Scope(ScopeType.PAGE) public class RequestManagerBean implements RequestManager { @Logger private Log log; @In StatusMessages statusMessages; private Lob lob; private Application app; private Cta cta; private Request request; private String notes; @Out(required = false) private List<Lob> lobs; private List<Application> applications; private List<Cta> ctas; @PersistenceContext EntityManager entityManager; public void CreateRequest() { // implement your business logic here log.info("requestManager.CreateRequest() action called"); statusMessages.add("CreateRequest"); log.info("LOB = " + lob); log.info("APP = " + app); log.info("CTA = " + cta); log.info("NOTES = " + notes); request = new Request(); request.setApplication(app); request.setCta(cta); request.setRequestNotes(notes); request.setEmployee(entityManager.find(Employee.class, new Long(1))); request.setRequestStatus(entityManager.find(RequestStatus.class, 1)); entityManager.persist(request); } @Factory("lobs") public void loadLobs() { lobs = entityManager.createQuery("from Lob").getResultList(); } public void loadApps() { applications = lob.getApplications(); } public void loadCtas() { ctas = app.getCtas(); } public Lob getLob() { return lob; } public void setLob(Lob lob) { this.lob = lob; } public Application getApp() { return app; } public void setApp(Application app) { this.app = app; } public Cta getCta() { return cta; } public void setCta(Cta cta) { this.cta = cta; } public Request getRequest() { return request; } public void setRequest(Request request) { this.request = request; } public String getNotes() { return notes; } public void setNotes(String notes) { this.notes = notes; } public List<Lob> getLobs() { return lobs; } public void setLobs(List<Lob> lobs) { this.lobs = lobs; } public List<Application> getApplications() { return applications; } public void setApplications(List<Application> applications) { this.applications = applications; } public List<Cta> getCtas() { return ctas; } public void setCtas(List<Cta> ctas) { this.ctas = ctas; } }
-
3. Re: Seam Hibernate create entities
nishp1 Jul 17, 2010 4:40 PM (in response to nishp1)Also, what scope would be appropriate for this kind of form? When I click on back button, I want form to not remember previous selected data.
-
4. Re: Seam Hibernate create entities
nishp1 Jul 20, 2010 5:22 PM (in response to nishp1)I am using PAGE Scope. The problem is that when click on back button, all drop down menus still have previous values. Is there a way of reseting the page once its cleared?
-
5. Re: Seam Hibernate create entities
herberson Jul 20, 2010 5:53 PM (in response to nishp1)Try use CONVERSATION scope and read Chapter 4. The contextual component model chapter in Seam documentation, to me, you seem need a better understand the different scope types and how they can change your way on doing something.
In CONVERSATION scope you will need tell to when the conversation begins and when her ends, that's why I suggest you read that chapter in documentation.
I rarely use a scope different of CONVERSATION, only attributes annotated with @DataModelSelection on my Seam components have PAGE scope and just because that is the default scope type.
About your code I din't see any fail that could be the cause to behavior your had describe, remaing your way of define the scopes as possible failure.