Map relationship between tables without foreign key

1

I have two tables in postgresql:

schema1.imovel

idt_imovel  | int    
cod_imovel  | string
nome_imovel | string
etc... 

schema2.declaration

idt_declaracao | int
cod_imovel     | string
status         | string
etc...

The two tables contain the cod_imovel column but there is no relationship between the tables. I have the two tables mapped with hibernate, the Imovel and Declaracao classes.

For each entity of class Imovel , I need to know the status of the declaration.

Is there any way to "force" a relationship in hibernate even without the relationship in the database?

I need something like this:

@Entity
@Table(name = "imovel", schema = "schema1")

public class Imovel extends GenericModel {

    @Id
    @Column(name="idt_imovel")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "imovel_seq")
    @SequenceGenerator(name = "imovel_seq", sequenceName = "integracao_lotes.imovel_idt_imovel_seq", allocationSize = 1)
    public Integer id;

    @Column(name="nome_imovel")
    public String nomeImovel;

    @Column(name="cod_imovel")
    public String codigo; 

    @relacionamentofake //aqui precisa existir uma forma de mapear a tabela declaracao
    public Declaracao declaracao
    
asked by anonymous 27.04.2018 / 14:20

1 answer

0

In theory it is possible, I confess I have never tried, but I see no reason why Hibernate can not do the mapping without FK.

Since you need to map Declaracao to Imovel , you need to use some Declaracao key for the mapping. I assume you can use idt_declaracao

In entity Imovel :

@ManyToOne
@JoinColumn(name="idt_declaracao")
public Declaracao declaracao

And in the entity Declaracao :

@Column(name="idt_declaracao")
public Integer id;
    
27.04.2018 / 14:48