Leang
Leang

Reputation: 160

JPA Design Model For OneToMany Relation

I have a database design like this: tb_address( id, address ) and tb_store( id, name, address_id ) and tb_user( id, name, address_id ) where both address_id are a reference key of tb_address. I want to design java model using spring-data-jpa. I had already create model one by using tool in Intellij to generate database script, but I am not getting what I want. What I got is like this tb_address( id, address, store_id, user_id ) and I don't want that. So Please help me.

Upvotes: 1

Views: 107

Answers (1)

chubock
chubock

Reputation: 844

Seems like you don't get what you want because of the design of your Models or Entities. I think this is what you're looking for:

@Entity
@Table(name = "td_address")
public class Address {

    @Id
    private Long id;
    private String address;

    //getters and setters

}

@Entity
@Table(name = "tb_store")
public class Store {

    @Id
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "address_id")
    private Address address;

    //getters and setters

}

@Entity
@Table(name = "tb_user")
public class User {

    @Id
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "address_id")
    private Address address;

    //getters and setters
}

Upvotes: 1

Related Questions