David Nagl
David Nagl

Reputation: 117

Hibernate Join Table

i have a MySQL database which is updated from Java with Entities. I need a join table between two tables which contains 3 columns.

1 column from table Bar ("bar_id")

2 columns from table Owner ("owner_id", "bought")

Could you please tell me if that is possible or how I could realize that.

I want a join table which looks like this:

'bar_id' | 'owner_id' | 'bought'
--------------------------------

BaseEntity.java

@MappedSuperclass
public class BaseEntity {
    private int id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId(){ return this.id; }
    public void setId(int id){ this.id = id; }
}

Bar.java

@Entity
@Table(name="bar")
public class Bar extends BaseEntity{

    private String name;
    private String bought;
    private List<Fan> fan;
    private List<Owner> owner;

    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="bought")
    public String getBought() {
        return bought;
    }
    public void setBought(String bought) {
        this.bought = bought;
    }

    @ManyToMany(mappedBy="bar", targetEntity=Owner.class)
    public List<Owner> getOwner() {
        return owner;
    }
    public void setOwner(List<Owner> owner) {
        this.owner = owner;
    }

    @ManyToMany
    @JoinColumn(name="fan")
    public List<Fan> getFan() {
        return fan;
    }
    public void setFan(List<Fan> fan) {
        this.fan = fan;
    }
}

Owner.java

@Entity
@Table(name="owner")
public class Owner extends BaseEntity{

    private String firstname;
    private String lastname;
    private String birthday;
    private java.sql.Date bought;
    private List<Bar> bar;

    @Column(name="firstname")
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Column(name="lastname")
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Column(name="birthday")
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Column(name="bought")
    public java.sql.Date getBought() {
        return bought;
    }
    public void setBought(java.sql.Date bought) {
        this.bought = bought;
    }

    @ManyToMany
    @JoinColumn(name="bar")
    public List<Bar> getBar() {
        return bar;
    }
    public void setBar(List<Bar> bar) {
        this.bar = bar;
    }
}

Upvotes: 0

Views: 3562

Answers (1)

shankarsh15
shankarsh15

Reputation: 1967

You can create a Join table by the using @JoinTable annotation.

Please have a look at this post which explains how to achieve it.

How to create join table with JPA annotations?

Upvotes: 1

Related Questions