What is the best way to map entity with JPA composite key?

2

I have two tables to map to two entities. I need to list it on the screen like this:

Id | Produto | Cliente | Razão Social  | País   | Estado
-----------------------------------------------------------------
1  | TV      | 000001  | Jao da Silva  | Brasil | São Paulo
2  | PC      | 000001  | Jao da Silva  | Brasil | Rio de Janeiro
3  | TV      | 000002  | Maria Pereira | EUA    | Florida

Since the country and state is a product reservation region control, it is not the customer's location. The second table is generic with compound key ... So far so good. Just map using InheritanceType and create IdClass . The problem is that I do not have the TYPE field ( DiscriminatorColumn ) in the RESERVATIONS table

The ERP system is old, I can not modify the tables. I already tried to do the query without mapping with DTO doing join left, already tried subquery. But I could not.

What is the best way to solve it?

Reservations:

|ID| PRODUTO|CLIENTE|PAIS|ESTADO|
---------------------------------
|1 | 1      |000001 |001 |SP    |
|2 | 2      |000001 |001 |RJ    |
|3 | 1      |000002 |002 |FL    |

Elements:

|TIPO|ID |DESCRICAO     |
--------------------
|NAZ |001|Brasil        |
|NAZ |002|EUA           |
|EST |SP |São Paulo     |
|EST |RJ |Rio de Janeiro|
|EST |FL |Florida       |

Entity 1:

@Entity 
public class Reserva implements Serializable {

    @Id
    @Column(name = "ID")
    private String id;

    @Column(name = "PRODUTO")
    private String produto;

    @ManyToOne
    private Cliente cliente;

    @Column(name = "PAIS")
    private String pais;

    @Column(name = "ESTADO")
    private String estado;

    //Getters & Setters
}

Entity 2:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TIPO")
@IdClass(ElementoPK.class)
public abstract class Elemento implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "TIPO")
    private String tipo;

    @Id
    @Column(name = "ID")
    private String id;

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

    //Getters & Setters
}

public class ElementoPK implements Serializable {

    private String tipo;
    private String id;

    //Getters & Setters
}

@Entity
@DiscriminatorValue("NAZ")
public class Pais extends Elemento implements Serializable {
}

@Entity
@DiscriminatorValue("EST")
public class Estado extends Elemento implements Serializable {
}
    
asked by anonymous 23.05.2018 / 15:17

0 answers