Java JPA具有复合键的多对多联接表实体;“生成空id”;

Java JPA具有复合键的多对多联接表实体;“生成空id”;,java,hibernate,jpa,many-to-many,jointable,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 = "account

这是我的实体:

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);
    }
}
当我这样做的时候:

account.addIntegrations(integrations.getTarget());
account.setCustomer(customer);
accountService.save(account);
我把这个记在日志里了 导致原因:org.hibernate.id.IdentifierGenerationException:为类com.dhl.dcc.domain.IntegrationAccount生成的空id


我对这种映射不太了解,您能告诉我如何改进这种映射(必须保留联接表的实体)以及如何使用相关集成保存帐户吗?谢谢。

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

工作起来很有魅力

我知道这个问题已经被标记为已解决,但我不同意接受的答案。这个答案通过在表
INT\u 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;
}

就像你说的一样,谢谢你。我还必须删除“insertable=false,Updateable false”。不修改数据模型的解决方案会更好。当我按照您的方式执行时,我得到的是
Hibernate:insert-into-integration(name)值(?)Hibernate:insert-into-account(name)值(?)线程“main”中的异常java.lang.NullPointerException位于org.Hibernate.type.descriptor.java.AbstractTypeDescriptor.extractHashCode(AbstractTypeDescriptor.java:88)org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:201)org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:205)
。它对我非常有效。我添加了用于重新创建测试用例的其余映射。你能给我更多关于你现在得到的错误的信息吗?
@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;
}