Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Many-to-Many Join table entity with compound key "null id generated "

This are my entities:

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);
    }
}

And this is entity for join table

@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;
...
}

And when i do:

account.addIntegrations(integrations.getTarget());
account.setCustomer(customer);
accountService.save(account);

I got this in my log Caused by: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.dhl.dcc.domain.IntegrationAccount

I dont have many knowledge about this kind of mapping, can you please tell me how to improve this mapping (entity for join table have to be preserved) and how to save account with related integrations? Thanks.

like image 892
DominikM Avatar asked Feb 22 '13 11:02

DominikM


1 Answers

I know this question has already been marked as solved but I disagree with the accepted answer. This answer modifies the datamodel by adding a useless column (the new id) in the table INT_ACCOUNTS. There is another way to solve this problem in Hibernate without modifying the datamodel :

@Entity
@Table(name = "INT_ACCOUNTS")
public class IntegrationAccount implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "INT_ID_FK")
    private Integration integration;

    @Id
    @ManyToOne
    @JoinColumn(name = "ACC_ID_FK")
    private Account account;
}

@Entity
@Table(name = "INTEGRATIONS")
public class Integration {

    @Id
    @SequenceGenerator(name = "integrationSequence", sequenceName = "SQ_INTEGRATIONS", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "integrationSequence")
    @Column(name = "INT_ID")
    private Long id;
}

@Entity
@Table(name = "ACCOUNTS")
public class Account {

    @Id
    @SequenceGenerator(name = "accountSequence", sequenceName = "SQ_ACCOUNTS", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
    @Column(name = "ACC_ID")
    private Long id;
}
like image 107
overmeulen Avatar answered Sep 28 '22 02:09

overmeulen