0

I've been trying to implement @ManyToMany relationship with extra column in spring-data-jpa.

Exactly as in this turorial https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-hsql/ , but unfortunately it doesn't work in my case.

Here are my classes:

@Entity
public class AccountEvent implements Serializable {

  @Id
  @ManyToOne(optional = false)
  @JoinColumn(name = "event_id")
  private Event event;

  @Id
  @ManyToOne(optional = false)
  @JoinColumn(name = "account_id")
  private Account account;

  @Column(name = "is_confirmed", nullable = false)
  private boolean isConfirmed;

  public AccountEvent() {
  }

  public AccountEvent(Event event, Account account) {
    this.event = event;
    this.account = account;
    this.isConfirmed = false;
  }

  public AccountEvent(Event event, Account account, boolean isConfirmed) {
    this.event = event;
    this.account = account;
    this.isConfirmed = isConfirmed;
  }

  public Event getEvent() {
    return event;
  }
  public void setEvent(Event event) {
    this.event = event;
  }

  public Account getAccount() {
    return account;
  }
  public void setAccount(Account account) {
    this.account = account;
  }

  public boolean isConfirmed() {
    return isConfirmed;
  }
  public void setConfirmed(boolean confirmed) {
    isConfirmed = confirmed;
  }
}

Account entitiy

@Entity
public class Account {

  @Id
  @Column(name = "id", updatable = false, nullable = false)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
  @SequenceGenerator(name = "user_id_seq", sequenceName = "user_id_seq", allocationSize = 1)
  private Long id;

[...]

  @OneToMany(mappedBy = "account")
  private Set<AccountEvent> accountEvents;

}

Event entity

@Entity
public class Event implements Serializable {

  @Id
  @NotNull
  @Column(name = "id", updatable = false)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "event_id_seq")
  @SequenceGenerator(name = "event_id_seq", sequenceName = "event_id_seq", allocationSize = 1)
  private Long id;

[...]

  @OneToMany(mappedBy = "event", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  private Set<AccountEvent> accountEvents;
}

And method in which i am trying to persist this data.

  @Override
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void signUpForAnEvent(String login, Event event) throws AppException {
    Account account = accountRepository.findByLogin(login);

    AccountEvent accountEvent = new AccountEvent(event, account);
    event.getAccountEvents().add(accountEvent);

    logger.info("Signing up user: " + account.toString() + " , for event: " + event.toString());
    eventRepository.save(event);
  }

Every time i try to persist (save method) the data, i recieve such exception

org.postgresql.util.PSQLException: ERROR: null value in column "event_id" violates not-null constraint
Szczegóły: Failing row contains (f, null, null).

What am I doing wrong?

Xpress
  • 1
  • 1
  • Your `AccountEvent` primary key configuration seems incorrect. My understanding is that you don't have a dedicated ID column for this table and rely on `account_id` and `event_id`. In this case, you may consider [a composite key](https://stackoverflow.com/a/13033039/4906586) – Al-un Nov 13 '17 at 23:11
  • I've been trying such solution, but it didn't work either. Still putting nulls in these IDs. – Xpress Nov 14 '17 at 06:22
  • My bad, I read wrongly your exception. It's saying that `event_id` is `null`. It means that when instantiating your `AccountEvent`, your `Account` is correct but your `Event` has a null ID. In your `signUpForAnEvent`, is your `Event` entity managed? If yes, is it already persisted (otherwise, the ID will not be generated by the SequenceGenerator)? – Al-un Nov 14 '17 at 10:36
  • Unfortunately account_id isn't set as well. Failing row contains (f, null, null). And yes event and account in this method are already persisted. So sequence don't have to be used at all, because they are already in DB with correct IDs. – Xpress Nov 14 '17 at 12:42
  • [Here](https://stackoverflow.com/a/32920550/8935440) is the solution. Thanks @peeskillet – Xpress Nov 18 '17 at 12:57

0 Answers0