3

I need to model a relationship between clients and promotions. We need the history about clients promotions score CLIENTS_PROMOTIONS_HISTORY and the current score CLIENTS_PROMOTIONS

DB modelling is fine but I'm finding too hard to model the Entities. I intend to use CLIENTS_PROMOTIONS table for relationship purposes only and so that the Clients can be aware of their promotions + current score on each promotion. But can't figure out where to store the current score.

Entities and Diagram:

@Entity
@Table(name = "CLIENTS")
@SecondaryTable(name = "CLIENTS_PROMOTIONS",
                pkJoinColumns = @PrimaryKeyJoinColumn(name = "ID_CLIENT"))
class Client {

    @Id
    @Column(name = "ID_CLIENT", nullable = false, unique = true)
    Long id;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "ID_USER", nullable = false, unique = true)
    User user;

    @Column(name = "NAME", nullable = false)
    String name;

    @Column(name = "BIRTHDAY", nullable = false)
    Date birthday;

    @Column(name = "EMAIL", nullable = false)
    String email;

    @Column(name = "SEX", nullable = false)
    Character sex;

    @ManyToMany
    @JoinTable(name = "CLIENTS_PROMOTIONS", 
               joinColumns = @JoinColumn(name = "ID_CLIENT"),
               inverseJoinColumns = @JoinColumn(name = "ID_PROMOTION"))
    Set<Promotion> promotions;
}

-

@Entity
@Table(name = "PROMOTIONS")
class Promotion {

    @Id
    @Column(name = "ID_PROMOTION", nullable = false, unique = true)
    Long id;

    @Column(name = "ID_ESTABLISHMENT", nullable = false)
    Long establishmentId;

    @Column(name = "TITLE", nullable = false)
    String title;

    @Column(name = "LIMIT_SCORE", nullable = false)
    Long limitScore;

    @Column(name = "DESCRIPTION", nullable = false)
    String description;

    @Column(name = "REWARD", nullable = false)
    String reward;
}

my eer diagram

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Davi Arimateia
  • 717
  • 6
  • 15

1 Answers1

1

You will need to create an Entity for Client_Promotions and model @ManyToOne relationship of this Entity with the Promotion and Client entities, instead of a @ManyToMany.

fmodos
  • 4,472
  • 1
  • 16
  • 17
  • @DaviArimateia unfortunatelly I think yes, because that is not a single join table... you have a field that needs an Entity to reflect its value. – fmodos May 25 '14 at 18:20
  • 1
    I see =( it does make sense! If any one need to implement this solution, here is my suggestion http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns – Davi Arimateia May 26 '14 at 02:04