How should i design the following scenario
ralf Mar 26, 2009 11:54 AMI have three Beans:
1. EntityHandler (handles EntityA, EntityB, ... EntityZ)
2. ProfileHandlerA (handles ProfileA via Conversations)
3. ProfileHandlerB (handles ProfileB via Conversations)
Entity1 and Entity2 contain a lot of EntityA ... EntityZ or Sets of them
Lets start with dhe EntityHandler (not to confuse with the EntityManager): the EntityHandler is some Kind of Wrapper for many many Entities /
List of Entities which i would like to use in many different views (e.g. as a <s:selectItem> ). So i want to follow the rule, to code this functionality only once.
Lets talk about the ProfileHandlerA and ProfileHandlerB:
They manage ProfileA or ProfileB, which contain Entities, which i were selected in the View, which was filled by the Content of the EntityHandler.
My Idea: is to put the EntityHandler in the ApplicationScope, to load them only once the application starts up.
But i have one Problem.
Everythings works fine, if i create a new Instance of Entity1 with the ProfileHandlerA in a Conversation and persist it. In my different views i can go back and forward.
But if i load an existing Instance of the Entity1, in the view the s:selectItem is not selected.
I tried to load the Entity Lists in the ProfileHandler and show them in the view, it works --> but it sucks - because i dont want to recode the functionaliy. This must be possible!
Where is my error in reasoning? Can Someone give me a hint, how to design the architecture?
My EntityHandler:
@Stateful
@Scope(ScopeType.APPLICATION )
@Name("entityHandler")
public class EntityHandler implements EntityHandlerLocal{
@PersistenceContext( type = PersistenceContextType.EXTENDED )
private EntityManager entityManager;
@Factory( value="possibleSizes" )
public List<Size> getSizeList( ){
//noinspection unchecked
return (List<Size>) entityManager.createQuery( "from Sizeorder by id" ).getResultList();
}
// ... and many many more
}
My ProfileHandlerA:
@Stateful
@Scope(ScopeType.CONVERSATION)
@Name("profileHandlerA")
public class ProfileHandlerA implements ProfileHandlerALocal{
@Logger
private Log logger;
@PersistenceContext(type = PersistenceContextType.EXTENDED )
private EntityManager entityManager;
private ProfileA profilA
...
public ProfileA ProfileA() {
return this.profileA;
}
...
@begin( nested = true )
public void select( ){
this.profilA = entityManager.createQuery( "from ProfileA where ..." ).getSingleResult();
}
@End
public void save( ){
this.entityManager.persist( this.profilA );
}
...
}My ProfileA:
@Entity
@Name("ProfilA")
@Table(name="ProfilA")
public class ProfilAimplements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false) @Length(min = 36, max = 36)
private String uuid;
@ManyToOne( optional = true, fetch = FetchType.LAZY ) @JoinColumn( nullable = true )
private Size size;
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST ) @JoinColumn( nullable = true )
private Set<EntityB> bEntities;
// many another Entities...
// empty constructor, getter, setter ...
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ProfilA)) return false;
ProfilA that = (ProfilA) o;
if (id != that.getId()) return false;
return true;
}
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
}My View:
<rich:tab label="Single">
<h:form id="singledatenform">
<s:validateAll>
<h:panelGrid columns="1">
<h:column><h:commandButton action="#{benutzerProfilHandler.actionSpeichern}" value="Änderungen speichern" /></h:column>
<h:column>
<h:panelGrid columns="2">
<h:column>Ich suche nach einer/einem:</h:column>
<h:column>
<h:selectOneMenu id="cmbGeschlechtDu" value="#{profileHandlerA.profilA.size}">
<s:selectItems value="#{possibleSizes}" var="size"
label="#{size.name}" noSelectionLabel="please select" hideNoSelectionLabel="false"/>
<s:convertEntity/>
</h:selectOneMenu>
</h:column>
<h:column> </h:column>
<h:column><h:message for="cmbSize" showSummary="true" /></h:column>
</h:panelGrid>
</h:column>
<h:column><h:commandButton action="#{profileHandlerA.save}" value="Änderungen speichern" /></h:column>
</h:panelGrid>
</s:validateAll>
</h:form>
</rich:tab>