Seam newbie: how to set component property value from lookup table automatically?
dzakharov.dmitri.zakharov.gmail.com May 1, 2009 7:54 PMHello everybody,
I am working through the project generated by seam generate
, based on existing database. I have a form to add a claim. A CLAIM has a field STATUS ID, which is a foreign key to CLAIM STATUS look up table. It contains the following values:
STATUS_ID NAME DESCRIPTION -------------------------------- 10 NEW ... 20 READY ... 30 SENT ... 40 REJECTED ...
On Add Claim
form Seam by default generated Claim Status
tab with select claimStatus
button. This forces the user to go and pick the status.
User must not be able to manipalate claim status directly. What I whould like to do is to set status automatically to NEW when a new Claim is added and get rid of Claim Status
tab.
So the first step, I commented out tab generated code in ClaimEdit.xhtml and now Save
button on Add Claim
form is disabled, because I can not select ClaimStatus. I found that there's some wiring
going on in ClaimHome.java:
public void wire() { ... ClaimStatus claimStatus = claimStatusHome.getDefinedInstance(); if (claimStatus != null) { getInstance().setClaimStatus(claimStatus); } ... } ... public boolean isWired() { if (getInstance().getClaimStatus() == null) return false; ... return true; }
If I comment out that claimStatus wiring code and try to save new Claim, I obviously get an error message, because claimStatus is not set:
Exception during request processing: Caused by javax.servlet.ServletException with message: "#{claimHome.persist}: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.domain.credits.model.Claim.claimStatus"
My Claim.java contains the following for claimStatus:
@Entity @Table(name = "CLAIM", catalog = "UNITED") public class Claim implements java.io.Serializable { private int claimId; private ClaimStatus claimStatus; ... @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "STATUS_ID", nullable = false) @NotNull public ClaimStatus getClaimStatus() { return this.claimStatus; } public void setClaimStatus(ClaimStatus claimStatus) { this.claimStatus = claimStatus; } ...
So one of the things I tried at this point is just to set claimStatus in ClaimHome.java persist() method as follows:
@Override public String persist() { Claim c = getInstance(); ... c.setClaimStatus(new ClaimStatus(10, "NEW")); return super.persist(); }
Now I am able to Save
a new Claim. But this doesn't look like an elegant way to hard code ClaimStatus values from database lookup table like this. Is there any other (better/right) way to use look up table to set values automatically in this situation?
Any comments and leads are highly appreciated.