Problem to retrieve data using Hibernate

1

I can list the Customer and their Animals, but I can not list the Breeds of animals.

Follow the classes involved in the relationship:

Client Class

@Entity
public class Cliente extends Pessoa {

@Column(name="forma_pagamento", length=20, nullable=false)
private String formaPagamento;

@OneToMany(mappedBy="cliente", cascade=CascadeType.PERSIST)
private List<Animal> animais;

public String getFormaPagamento() {
    return formaPagamento;
}

public void setFormaPagamento(String formaPagamento) {
    this.formaPagamento = formaPagamento;
}

public List<Animal> getAnimais() {
    return animais;
}

public void setAnimais(List<Animal> animais) {
    this.animais = animais;
}

}

Animal Class

@Entity
public class Animal {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_ANIMAL")
@SequenceGenerator(name="ID_ANIMAL", sequenceName="SEQ_ID_ANIMAL", initialValue=1 ,allocationSize=1)
private int id;

@Column(length=25, nullable=false)
private String nome;

private int idade;

@Column(nullable=false)
private char sexo;

@ManyToOne
@JoinColumn(name="id_cliente")
private Cliente cliente;

@ManyToOne
@JoinColumn(name="raca")
private Raca raca;

public int getId() {
    return id;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public int getIdade() {
    return idade;
}

public void setIdade(int idade) {
    this.idade = idade;
}

public char getSexo() {
    return sexo;
}

public void setSexo(char sexo) {
    this.sexo = sexo;
}

public Cliente getCliente() {
    return cliente;
}

public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

public Raca getRaca() {
    return raca;
}

public void setRaca(Raca raca) {
    this.raca = raca;
}

}

Race Class

@Entity
public class Raca {

@Id
private String nome;

@OneToMany(mappedBy="raca")
private List<Animal> animais;

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public List<Animal> getAnimais() {
    return animais;
}

public void setAnimais(List<Animal> animais) {
    this.animais = animais;
}

}

Problem solved, just overwritten method toString of class Raca and resolved.

    
asked by anonymous 09.04.2017 / 05:44

1 answer

0

Hello,

With the instance of the animal you had access only to the race object, so you can access the attribute "race class name" simply add the getNome () as below:

animal.getRaca (). getNome ();

    
12.04.2017 / 01:40