Search code examples
javapostgresqlhibernatejpahibernate-mapping

How to map entities B on entities A using a Varchar column on table B?


I have some difficulties with a JPA scenario that i'm unable to resolve. I search on Stackoverflow a similar case, but i didn't found.

I only found this topic, but it's not exactly my case...

If it already exists, I'm sorry.

Here's my case :

I have two tables :

A :

id label ref_name
1 ARK Bruce
2 HAM Alfred

B :

id name lastname
1 Bruce Wayne
2 Alfred Pennyworth
3 Bruce Banner

Table B represents a list of persons. Table A has a label name, and a column to list all persons from Table B that has a particular name.

In Java I have these 2 entities :

public class B {

   private Integer id;

   private String name;

   private String lastname;
}
public class A {

   private Integer id;

   private String label;

   @ManyToMany
   @JoinColumn(name="ref_name", referencedColumnName="name")
   private List<B> persons;
}

So, I've tried this code, and i have the following error message :

ERROR: relation "A_persons" does not exist

I can't figure what's wrong with this ...

Thank you for your help !


Solution

  • By observing your use of many-to-many mapping, I am guessing you are trying to do many-to-many mapping without the use of a "junction-table". I found this Stackoverflow answer showing how this can be done. What do you think? https://stackoverflow.com/a/25018992

    public class A {
        private Integer id;
        private String label;
        @OneToMany
        @JoinColumn(name="ref_name",
            referencedColumnName="name")
        private List<B> persons;
    }