N x N relationship in Java objects

1

I have a table item

(item_id (pk), descrição, ...),

A vendor table

(fornecedor_id (pk), nome, ...) 

and because it is a NxN relation I have an intermediate table vendor_list

(item_id (fk), fornecedor_id (fk), codigo, ...).

How do I create these 3 Java objects (corresponding to each table), given this relationship?

    
asked by anonymous 07.11.2016 / 19:07

1 answer

0

I'll share a solution with you.

You'll just need to set some details like field names to get the get and set and the hashcode and equals methods of the 4 classes

Class Item

@Entity
@Table(name = "item")
public class Item {

    @Id
    @Column(name = "item_id", length = 20, precision = 0)
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long            id;

    @Column(name = "descricao")
    private String          descricao;

    //implementar os metodos corretamente hashCode equals, recomendavel

}

Supplier Class

@Entity
@Table(name = "forncedor")
public class Fornecedor {

    @Id
    @Column(name = "forncedor_id", length = 20, precision = 0)
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long            id;

    @Column(name = "nome")
    private String          nome;

    //implementar os metodos corretamente hashCode equals, recomendavel

}

Relationship class, relationship fields must be equal and more Must be equal to table's DDL

@Table(name = "item_fornecedor")
@IdClass(ItemFornecedorPk.class)
public class ItemFornecedor {

    private ItemFornecedorPk    pk;

    @Id
    @ManyToOne
    @JoinColumn(name = "item_id_pk", referencedColumnName = "id")
    private Item        item;

    @Id
    @ManyToOne
    @JoinColumn(name = "forncedor_id_pk", referencedColumnName = "id")
    private Fornecedor          fornecedor;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "campoSoDaRelacao")
    private Date            campoSoDaRelacao;

    //implementar os metodos corretamente hashCode equals, obrigatório

}

Relationship key class

@Embeddable
public class ItemFornecedorPk {


    @Id
    @ManyToOne
    @JoinColumn(name = "forncedor_id_pk", referencedColumnName = "forncedor_id", insertable = false, updatable = false)
    private Fornecedor      fornecedor;

    @Id
    @ManyToOne
    @JoinColumn(name = "item_id_pk", referencedColumnName = "item_id", insertable = false, updatable = false)
    private Item        item;

    //implementar os metodos corretamente hashCode equals, obrigatório
}

Now it's put to the factory or the pesistyUnit read and it already works. of doing the lists also in the father of each one, but there only if it needs.

    
07.11.2016 / 20:47