1

I'm looking for a solution to my problem. I have 2 java class of domain. Graduacao and Aluno, in Graduacao I have an attribute "graus" that is a Collection. In Aluno class, I have collection attribute "List graduacao". I add the Graduacao in a JComboBox about ComboBoxModel and when user selected a Graduacao I have a JList that show the "graus" of Graduacao.

What I need is get Graduacao and graus that user choose and add to Aluno and persist after show results in a JTable with AbstractTableModel.

I'm trying this

@Entity
@Table(name="graduacao")
public class Graduacao {

    @Id @GeneratedValue
    private Integer id;

    @NotNull @Column(unique = true)
    private String graduacao;

    @ElementCollection
    @CollectionTable(name="graduacao_grau", joinColumns=@JoinColumn(name="id_graduacao"))
    @Column(name="grau")    
    private List<String> graus;

    //get and set


@Entity
@Table(name="aluno")
public class Aluno {
    @Id @GeneratedValue
    private Integer id;

    //informacoes gerais
    @NotNull
    private String nome;
    private String cpf;
    private String rg;
    private String nomePai;
    private String nomeMae; 
    @Temporal(TemporalType.DATE)
    private Date dtNascimento;
    @Temporal(TemporalType.TIMESTAMP)
    private Date dtCadastro;
    private String status;
    private String observacoes;

    //logradouro
    private String endereco;    
    private String bairro;
    private String complemento;
    private String cidade;
    private String cep;    
    @Enumerated(EnumType.STRING)
    private EstadoBrasileiro uf;

    //contato
    @ElementCollection
    @CollectionTable(name="telefone_aluno", joinColumns=@JoinColumn(name="id_aluno"))
    @Column(name="telefone")
    private List<String> telefones;
    private String email;

    //graduacao
    @OneToMany @JoinColumn(name="id_aluno")
    private List<Graduacao> graduacao;
    @Temporal(TemporalType.DATE)
    private Date dataGraduou;

    //federacao
    @OneToMany @JoinColumn(name="id_federacao")
    private List<Federacao> federacao;

    //get and set

here the print enter image description here

/** edit */ I solved the problem, here the project: http://www.4shared.com/zip/1Gbj-IZLce/project_example.html

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118

1 Answers1

3

A complete example is beyond the scope of StackOverflow. Two approaches are common, although neither is simple:

  • Create a custom TableModel that uses JPA queries and entities to implement the methods required by AbstractTableModel; a very simple example using JComboBox is shown here; a complete TableModel example with pagination is shown here.

  • Use org.jdesktop.beansbinding, shown here and mentioned here and here.

A number of ancillary links related to this topic are shown in this answer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I understand. But my problem is when user choose Graduacao in JComboBox and "graus" in JList. I need get all choice of user and create a another List to Aluno. I can get JComboBox choice and JList choice, my problem is to create another List. See my entity Graduacao has an attribute "graus", this attribute is a List and receive annotation ElementCollection and my entity Aluno has an attribute "graduacao" this attribute is a List. – FernandoPaiva Jul 27 '14 at 01:26
  • You'll need to write a JPA query that fetches the rows in `Aluno` for the selected `Graduacao` and `Graus`; sorry, I don't understand your data model well enough to be more specific. – trashgod Jul 27 '14 at 01:53
  • `Aluno` looks like a `JTable`, rather than a `JList`; in either case you would construct the corresponding model using the query results. – trashgod Jul 27 '14 at 02:00
  • ahhhh....I think understand you. I need create other model with results and setModel to my JTable. – FernandoPaiva Jul 27 '14 at 02:16
  • Yes, this simplified [example](http://stackoverflow.com/a/9134371/230513) may offer guidance. – trashgod Jul 27 '14 at 02:30
  • but how to I persist Aluno with List and "graus" that user choice ? – FernandoPaiva Jul 27 '14 at 02:32
  • You'll need to update the affected rows in `Aluno` in your table model's implementation of `setValueAt()`. – trashgod Jul 27 '14 at 02:38
  • for example: Graduacao g = (Graduacao)cbx_graduacao.getSelectedItem(); List grausList = g.getGraus(jlist_graus.getSelectedIndex()); List graduacaoList = new ArrayList(); graduacaoList.add(g); Aluno bean = new Aluno(); bean.setGraduacao(graduacaoList); AlunoDAO().persist(bean); This is your suggestion ? – FernandoPaiva Jul 27 '14 at 03:08
  • When a given `TableCellEditor` [concludes](http://stackoverflow.com/a/10067560/230513), `setValueAt()` will be called for the single updated cell; you may be able to persist just that value. Will you be adding rows to the table? – trashgod Jul 27 '14 at 10:55
  • hello, I solved the problem. If you know how I did here the project: http://www.4shared.com/zip/1Gbj-IZLce/project_example.html . Thanks my friend :D – FernandoPaiva Jul 28 '14 at 05:52
  • @FernandoPaiva: You are welcome; you may want to edit your question to show the link to the project. – trashgod Jul 28 '14 at 16:20