Springboot with Hibernate and relationship between postgres tables in different schemas

Asked

Viewed 233 times

0

I have the following problem: I created a database called Test and inside there are two schemas: schema1 and schema2. Within each schema has a table: schema1 -> Tabela1 schema2 -> table2

It turns out that if I try to make a relationship with Springboot using Annotations between Table 1 and table2, they don’t see themselves. with pure sql, with you, just inform the table with schema. schema1.Tabela1 schema2.table 2.

But how do I do Spring Boot? Here’s my code.

@Entity @Table(schema = "schema_1") 
public class Tabela1 implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Integer id;
private String name;
@OneToMany(mappedBy = "tabela1")
private List<Tabela2> tabela2= new ArrayList<>();
publicTabela1() {
    }
 }

second table

@Entity    
@Table(schema = "schema_2")
public class Tabela2 implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String number;
@ManyToOne
@JoinColumn(name = "fk_tabela1")
private Tabela1 tabela1;
public Tabela2() {
         }
     }

1 answer

2


In the Table1 class, you may need to map the schema to the Table2 class using the property "targetEntity":

@OneToMany(targetEntity=Tabela2.class, mappedBy = "tabela1") 
private List tabela2= new ArrayList();
  • It worked perfectly. Thank you very much

  • @Rodrigobatista, if the answer helped, mark your colleague’s answer as the correct one :)

Browser other questions tagged

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