JPA多对多连接表实体与复合键"生成空id"

时间:2013-02-22 11:20:46

标签: java hibernate jpa many-to-many jointable

这是我的实体:

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

这是连接表的实体

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

当我这样做时:

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

我在日志中得到了这个 引起:org.hibernate.id.IdentifierGenerationException:为以下内容生成的null id:class com.dhl.dcc.domain.IntegrationAccount

我对这种映射没有太多了解,请您告诉我如何改进此映射(必须保留连接表的实体)以及如何使用相关集成保存帐户?感谢。

2 个答案:

答案 0 :(得分:9)

我知道这个问题已被标记为已解决,但我不同意接受的答案。此答案通过在表INT_ACCOUNTS中添加无用列(新id)来修改数据模型。还有另一种方法可以在不修改数据模型的情况下解决Hibernate中的这个问题:

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

答案 1 :(得分:1)

您可以为IntegrationAccount创建ID字段,然后为两个字段创建唯一约束。

@Entity
@Table(name = "INT_ACCOUNTS",
       uniqueConstraints=@UniqueConstraint(columnNames={"ACC_ID", "INT_ID"}))
public class IntegrationAccount  {

    @Id
    private Long id;

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

像魅力一样!