Map relationship between tables without Foreign key

Asked

Viewed 313 times

1

I have two tables in postgresql:

schema1.immobile

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 column cod_imovel but there is no relationship between the tables. I have the two tables mapped with Hibernate, the classes Imovel and Declaracao.

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

Is there any way to "force" a relationship in Hibernate even without the relationship existing in the bank?

I need something like:

@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
  • 1

    In case, to know which property is related to which declaration, use the column cod_imovel, right? Also, edit your question and place the class Declaração and tell me the cardinality of the relationship, please.

1 answer

0

In theory it’s possible, I confess I never tried, but I see no reason why Hibernate couldn’t do the mapping without FK.

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

In the entity Imovel:

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

And in the entity Declaracao:

@Column(name="idt_declaracao")
public Integer id;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.