6

这是我的实体:

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 generated for:class com.dhl.dcc.domain.IntegrationAccount

我对这种映射了解不多,请告诉我如何改进这种映射(必须保留连接表的实体)以及如何通过相关集成保存帐户?谢谢。

4

2 回答 2

10

我知道这个问题已经被标记为已解决,但我不同意接受的答案。这个答案通过在 table 中添加一个无用的列(新的 id)来修改数据模型INT_ACCOUNTS。在 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;
}
于 2013-02-22T16:08:54.033 回答
0

您可以为您的 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;
...
}

奇迹般有效!

于 2013-02-22T11:25:48.633 回答