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

この質問がすでに解決済みとしてマークされていることは知っていますが、受け入れられた回答には同意しません。この回答は、テーブルに役に立たない列 (新しい 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 フィールドを作成してから、2 つのフィールドに一意の制約を作成できます。

@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 に答える