Questo sono i miei soggetti:JPA molti-a-molti Registrazione entità tabella con composto chiave "id nullo generato"
public class Account extends AbstractEntity<Long> {
@Id
@SequenceGenerator(name = "accountSequence", sequenceName = "SQ_ACCOUNTS", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
@Column(name = "ACC_ID", nullable = false)
private Long id;
...
}
public class Integration extends AbstractEntity<Long> {
@Id
@SequenceGenerator(name = "integrationSequence", sequenceName="SQ_INTEGRATIONS", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "integrationSequence")
@Column(name = "INT_ID", nullable = false)
private Long id;
...
public void addIntegration(Integration integration) {
IntegrationAccount association = new IntegrationAccount();
// This does not help
//association.setIntAccountsPK(new IntAccountsPK(integration.getId(), this.getId()));
association.setAccount(this);
association.setIntegration(integration);
this.integrationAccounts.add(association);
integration.getIntAccountsCollection().add(association);
}
}
e questo è entità per unirsi a tavola
@Entity
@Table(name = "INT_ACCOUNTS")
public class IntegrationAccount {
@EmbeddedId
protected IntAccountsPK intAccountsPK;
@JoinColumn(name = "ACC_ID", referencedColumnName = "ACC_ID", insertable = false, updatable = false)
@ManyToOne
private Account account;
@JoinColumn(name = "INT_ID", referencedColumnName = "INT_ID", insertable = false, updatable = false)
@ManyToOne
private Integration integration;
...
}
@Embeddable
public class IntAccountsPK implements Serializable {
@Column(name = "INT_ID", nullable = false)
private Long intId;
@Column(name = "ACC_ID", nullable = false)
private Long accId;
...
}
e quando lo faccio :
account.addIntegrations(integrations.getTarget());
account.setCustomer(customer);
accountService.save(account);
ho ottenuto questo nel mio ceppo causato da: org.hibernate.id.IdentifierGenerationException: ha generato nulla id per: classe com.dhl.dcc.domain.IntegrationAccount
Non ho molte conoscenze su questo tipo di mappatura, puoi dirmi come migliorare questa mappatura (l'entità per la tabella di join deve essere preservata) e come salvare l'account con integrazioni correlate? Grazie.
Come un incantesimo come hai detto, grazie. Ho dovuto rimuovere anche "insertable = false, updatable false". – DominikM