Self ManyToMany使用JPA 2.0添加其他列 [英] Self ManyToMany with additional columns using JPA 2.0

查看:200
本文介绍了Self ManyToMany使用JPA 2.0添加其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Application实体,想在JPA中表达与属性的自我双向关系。
EX。 QuoteStore(提供商应用程序)为在线门户提供服务,许多(消费者应用程序)
和QuoteStore(消费者应用程序)消耗来自Siebel CRM的服务和许多(提供商应用程序)
当关系是ManyToMany时,上述方法很有效但不是自我(例如App2DB或App2BizCase)
对于自身双向ManyToMany与应用程序实体上的属性的关系;我把它分解为两个OneToMany关系,比如

I have Application entity, want to express self bi-directional relationship with attribute in JPA. EX. QuoteStore(provider Application) provides services to online portal and many(consumer applications) and QuoteStore(consumer Application) consumes services from Siebel CRM and many (provider applications) The above approach works well when relationship is ManyToMany but not self (ex. App2DB or App2BizCase) For Self bidirectional ManyToMany relationship with attributes on application entity; I decomposed it to two OneToMany Relationships like

App 1--One2Many--* App2AppLink *--ManyToOne-1 App
(Provider 1--One2Many--* App2AppLink *--ManyToOne-1 Consumer)






这是应用程序实体


Here is Application Entity

@Entity
public class ApplicationEO {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  /**
   * This application instance services are consumed by  Set<App2AppLinkEO> applications connections/links
   */
  @OneToMany(mappedBy = "provider")
  private Set<App2AppLinkEO> consumberLinks;

  /**
   * This application instance consumes services by Set<App2AppLinkEO> applications connections/links
   */
  @OneToMany(mappedBy = "consumer")
  private Set<App2AppLinkEO> providerLinks;

}

这是Application to Application关系表

Here is Application to Application relationship table

@Entity
@Table(name="APP2APPLINK")
public class App2AppLinkEO {

  @EmbeddedId
  @AttributeOverrides({@AttributeOverride(name = "entity1Id", column = @Column(name = "PROVIDER_ID")),
      @AttributeOverride(name = "entity2Id", column = @Column(name = "CONSUMBER_ID"))})

  private CompositePK id;

  @ManyToOne(optional = false)
    private ApplicationEO providerApp;

  @ManyToOne(optional = false)
   private ApplicationEO consumerApp;

  private String service;

  private String status;

  private String platform;
}






这里是复合初级密钥代码


And Here is Composite Primary Key code

@Embeddable
public class CompositePK implements Serializable {

  private long entity1Id;

  private long entity2Id;
}






以上设置生成关系表as:


Above setup generates relation table as:

CREATE TABLE APP2APPLINK (
                          PLATFORM VARCHAR(255), 
                          STATUS VARCHAR(255), 
                          SERVICE VARCHAR(255), 
                          PROVIDER_ID BIGINT NOT NULL, 
                          CONSUMBER_ID BIGINT NOT NULL, 
                          CONSUMERAPP_ID BIGINT, 
                          PROVIDERAPP_ID BIGINT, 
                          PRIMARY KEY (PROVIDER_ID, CONSUMBER_ID))

但为什么我会在下面看到额外的列?

But why do I see below extra columns there?

 CONSUMERAPP_ID BIGINT, 
 PROVIDERAPP_ID BIGINT, 


推荐答案

您没有在@ ManyToOne上设置@JoinColumn,因此它们获得默认的连接列名称。你需要给,

You are not setting the @JoinColumn on the @ManyToOne's so they get the default join column name. You need to give,

@JoinColumn(name="PROVIDER_ID", insertable=false, updateable=false)

@JoinColumn(name="CONSUMBER_ID", insertable=false, updateable=false)

当您从EmbeddedId编写字段时,您需要将它们标记为只读(或者您可以将其标记为insertable = false,updateable = false)

You need to mark them as read-only as you are writing the field from your EmbeddedId (or you could mark it as insertable=false, updateable=false)

但是真的你应该完全删除EmbeddedId,只需将@Id添加到@ ManyToOne并定义一个@IdClass。

But really you should remove the EmbeddedId entirely and just add @Id to the @ManyToOne's and define an @IdClass.

这篇关于Self ManyToMany使用JPA 2.0添加其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆