2013-02-22 70 views
6

这是我的实体:JPA许多一对多加入与复合键表实体“空ID生成”

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

当我做:org.hibernate.id.IdentifierGenerationException:

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

我在我的日志 所致得到这个产生的空号为:类com.dhl.dcc.domain.IntegrationAccount

我没有关于这种映射的许多知识,你可以请告诉我如何改善这种映射(实体连接表都被保留),以及如何用相关的集成保存帐户?谢谢。

回答

1

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

@Entity 
@Table(name = "INT_ACCOUNTS", 
     [email protected](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; 
... 
} 

工程就像一个魅力!

+0

就像你说的一样魅力,谢谢。我不得不删除“insertable = false,可更新false”。 – DominikM 2013-02-22 12:20:03

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; 
} 
+0

不修改datamodel的解决方案会好得多。当我这样做你的方式我越来越 'Hibernate:插入到集成(名称)值(?) 休眠:插入到帐户(名称)值(?) 线程“主”异常java.lang.NullPointerException \t在org.hibernate.type.descriptor.java.AbstractTypeDescriptor.extractHashCode(AbstractTypeDescriptor.java:88) \t在org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:201)在org.hibernate.type.AbstractStandardBasicType \t .getHashCode(AbstractStandardBasicType.java:205)' – DominikM 2013-02-23 13:30:44

+0

奇怪。它对我来说非常合适。我添加了用于重新创建测试用例的映射的其余部分。你能给我更多关于你现在得到的错误的信息吗? – overmeulen 2013-02-25 10:28:11