java - Spring Data: How to write a record in join table for unidirectional one-to-may? -


i have subscription class , payment class. when following, doesn't create record in join table. should use intermediate class or possible create such record without it? subscriptionrepository crudrepository spring-data.

@transactional public subscription activate(@valid subscription subscription, @valid payment payment) {     set<payment> payments = subscription.getpayments();     if (payments == null)         payments = new hashset<>();      payments.add(payment);      return subscriptionrepository.save(subscription); } 

classes:

subscription:

@entity public class subscription { ...     @onetomany(fetch = fetchtype.eager, cascade = cascadetype.all)     @jointable(             joincolumns = {@joincolumn(name = "subscription_id", referencedcolumnname = "id")},             inversejoincolumns = {@joincolumn(name = "payment_id", referencedcolumnname = "id", unique = true)}     )     @getter @setter     private set<payment> payments; } 

payment:

@entity public class payment {     @column     @id     @generatedvalue(strategy=generationtype.auto)     @jsonignore     private integer id;      @column(nullable = false)     private paymenttype paymenttype;      @past     @column(nullable = false)     private date date;      public enum paymenttype {         money,         promo_code,         trial     } } 

you forgot inject payments in subcription , repository , pojo seem fine

if (payments == null) {     payments = new hashset<>();     subscription.setpayments(payments);  } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -