Question

Morning Stack members,

Would you please help me with this issue ?

Im trying to read a persistent set property from an object but it returns only one row and I have in database table two inserted rows for such id

  • How can I load in dataTable all the rows ?
  • How can I read all the fields loaded in the LinkHashSet table as id,name,description,etc...

Bean :

@Inject
private Pessoa pessoa;

Entity Pessoa :

@Entity
@Table(name = "pessoa",uniqueConstraints = {@UniqueConstraint(columnNames = { "idt_pessoa" }) })
@Inheritance(strategy = InheritanceType.JOINED)
@OneToMany
@JoinColumn(name = "idt_pessoa")
private Set<ContatoPessoa> contatoPessoas = new LinkedHashSet<ContatoPessoa>();

public Set<ContatoPessoa> getContatoPessoas() {
    return contatoPessoas;
}

public void setContatoPessoas(Set<ContatoPessoa> contatoPessoas) {
    this.contatoPessoas = contatoPessoas;
}

ContatoPessoa entity :

@Entity
@Table(name="contato_pessoa")
public class ContatoPessoa implements Entidade{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idt_contatoPessoa")
private Long id;

@Column(length=100)
private String des_areaContatoPessoa;

@Column(length=100)
private String des_emailContatoPessoa;

In xhtml :

<p:dataTable id="dTContato" var="contatoPessoas"
value="#{pessoaMB.pessoa.contatoPessoas}"
rowKey="#{pessoa.contatoPessoas.id}">
<p:column headerText="#{bundle.pessoa_lblid}">
<p:outputLabel value="#{contatoPessoas.key}"/>
</p:column>

Ive tried with different combinations in xhtml p:outputLabel but with no success at all

I watched the persistent set from an object pessoa and it has values. The bag is an ArrayList

I have no reputation to post images. sorry

Était-ce utile?

La solution

Ok, now I see what's going on... You can't use Set in data table. You have to use some ordered collection. see this question for detail.... I would suggest create a new Method

public List<ContatoPessoa> getContatoPessoasList() {
    return new ArrayList<ContatoPessoa>(contatoPessoas);
}

and change your binding to: value="#{pessoaMB.pessoa.contatoPessoasList}"

Autres conseils

To me it seems like you are using wrong var variable in your outputLabel. In your table you have defined var="car". So instead of:

<p:outputLabel value="#{pessoa.contatoPessoas.id}"/>

You should use access your values through this car variable, something like:

<p:outputLabel value="#{car.id}"/>

UPDATE

also your rowKey attribute seems wrong, change it like this:

rowKey="#{contatoPessoas.id}"

UPDATE

As @Ammar noted above there is also problem with set which is not supported in JSF older than version 2.2. So another solution is to upgrade to JSF 2.2

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top