Architecture Problem (validation)
eethyo Nov 5, 2007 10:26 AMHi,
I have a problem with the Validation.
Validator:
package com.ccm30.validators;
import java.util.List;
import javax.persistence.NoResultException;
import org.hibernate.mapping.Property;
import org.hibernate.validator.PropertyConstraint;
import org.hibernate.validator.Validator;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityController;
import com.ccms30.entities.CcmsProductline;
import com.ccms30.entities.CcmsProductlineController;
@Name("myProductlineListValidator")
public class MyProductlineListValidator extends EntityController implements
Validator<MyProductlineList>, PropertyConstraint {
// part of the Validator<Annotation> contract,
// allows to get and use the annotation values
public void initialize(MyProductlineList parameters) {
}
// part of the property constraint contract
public boolean isValid(Object value) {
if (value == null){
System.out.println("Value is null");
return true;
}
if (!(value instanceof String))
{
System.out.println("Value is not a string");
return false;
}
String string = (String) value;
String trimmed = string.trim();
try{
CcmsProductline cpl = (CcmsProductline) createQuery(
"select cpl from CcmsProductline cpl where cpl.name=:name")
.setParameter("name", trimmed).getSingleResult();
System.out.println("Value is in List");
return true;
}catch(NoResultException e)
{
System.out.println("Value is not in List");
return false;
}
}
public void apply(Property arg0) {
}
}
Entity:
package com.ccms30.entities;
// Generated 16.10.2007 13:32:22 by Hibernate Tools 3.2.0.b9
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.richfaces.model.TreeNode;
import com.ccm30.validators.MyProductlineList;
import com.ccms30.treenodes.CcmsTree;
/**
* CcmsProductline generated by hbm2java
*/
@Entity
@Table(name = "CCMS_PRODUCTLINE", schema = "CCMSDB")
public class CcmsProductline extends CcmsTree implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private CcmsProductline ccmsProductline;
private CcmsUser ccmsUser;
private String name;
private String description;
private Date modified;
private Set<CcmsProductline> ccmsProductlines = new HashSet<CcmsProductline>(
0);
private Set<CcmsProduct> ccmsProducts = new HashSet<CcmsProduct>(0);
private Set<CcmsPolicy> ccmsPolicies = new HashSet<CcmsPolicy>(0);
@Transient
private String ccmsParentProductlineName = initCcmsParentProductlineName();
public CcmsProductline() {
}
public CcmsProductline(Integer id, String name) {
this.id = id;
this.name = name;
}
public CcmsProductline(Integer id, CcmsProductline ccmsProductline,
CcmsUser ccmsUser, String name, String description, Date modified,
Set<CcmsProductline> ccmsProductlines,
Set<CcmsProduct> ccmsProducts, Set<CcmsPolicy> ccmsPolicies) {
this.id = id;
this.ccmsProductline = ccmsProductline;
this.ccmsUser = ccmsUser;
this.name = name;
this.description = description;
this.modified = modified;
this.ccmsProductlines = ccmsProductlines;
this.ccmsProducts = ccmsProducts;
this.ccmsPolicies = ccmsPolicies;
}
@Id @SequenceGenerator(name = "generator", sequenceName = "ID_SEQUENCE") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
@NotNull
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_PRODUCTLINE_ID")
public CcmsProductline getCcmsProductline() {
return this.ccmsProductline;
}
public void setCcmsProductline(CcmsProductline ccmsProductline) {
this.ccmsProductline = ccmsProductline;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CREATOR")
public CcmsUser getCcmsUser() {
return this.ccmsUser;
}
public void setCcmsUser(CcmsUser ccmsUser) {
this.ccmsUser = ccmsUser;
}
@Column(name = "NAME", nullable = false, length = 200)
@NotNull
@Length(max = 200)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "DESCRIPTION", length = 4000)
@Length(max = 4000)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Temporal(TemporalType.DATE)
@Column(name = "MODIFIED", length = 7)
public Date getModified() {
return this.modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ccmsProductline")
public Set<CcmsProductline> getCcmsProductlines() {
return this.ccmsProductlines;
}
public void setCcmsProductlines(Set<CcmsProductline> ccmsProductlines) {
this.ccmsProductlines = ccmsProductlines;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ccmsProductline")
public Set<CcmsProduct> getCcmsProducts() {
return this.ccmsProducts;
}
public void setCcmsProducts(Set<CcmsProduct> ccmsProducts) {
this.ccmsProducts = ccmsProducts;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "CCMS_POLICY_PRODUCTLINE_Z", joinColumns = @JoinColumn(name = "policy_id"), inverseJoinColumns = @JoinColumn(name = "productline_id"))
public Set<CcmsPolicy> getCcmsPolicies() {
return this.ccmsPolicies;
}
public void setCcmsPolicies(Set<CcmsPolicy> ccmsPolicies) {
this.ccmsPolicies = ccmsPolicies;
}
public String initCcmsParentProductlineName()
{
if(ccmsProductline!=null)
return ccmsProductline.getName();
else
return "";
}
@Transient
@MyProductlineList
public String getCcmsParentProductlineName() {
System.out.println("operation called");
return ccmsParentProductlineName;
}
@Transient
public void setCcmsParentProductlineName(String ccmsParentProductlineName) {
this.ccmsParentProductlineName = ccmsParentProductlineName;
}
@Override
public String receiveType() {
// TODO Auto-generated method stub
return "CcmsProduct/CcmsProductline";
}
}I always get a "Stackoverflow" Exception.
I think i know why, because my Productline i get wants to validate its value and so on. how to avoid this?
Where should I set my validator?
Can you help me?!