gpt4 book ai didi

jpa - Java EE 6 JPA 2 ManyToOne 关系创建无效的外键

转载 作者:行者123 更新时间:2023-12-04 14:26:01 25 4
gpt4 key购买 nike

我正在尝试创建两个实体,其中两个实体都具有 EmbeddedIds。其中一个实体有 2 个对另一个实体的引用,其中这两个引用都关联为 ManyToOne。

示例代码写在下面;

@Embeddable
public class ItemPK {
@Column(nullable = false, length = 100)
private String itemId;
@Column(name = "item_client_id", nullable = false)
private int clientId;
...
}

@Entity
@Table(name = "item")
public class Item {
@EmbeddedId
private ItemPK id;

@ManyToOne
@JoinColumn(name = "item_client_id")
private Client client;

@OneToMany(mappedBy="item", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<RelatedItem> relatedItems;

@OneToMany(mappedBy="relatedItem", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<RelatedItem> relatedItemsRHS;
...
}

@Embeddable
public class RelatedItemPK {
@Column(name = "itemId", length = 100, nullable = false)
private String itemId;
@Column(name = "item_client_id", nullable = false)
private int clientId;
@Column(name = "relatedItemId", length = 100, nullable = false)
private String relatedItemId;
@Column(name = "related_item_client_id", nullable = false)
private int relatedItemClientId;
...
}

@Entity
@Table(name = "related_item")
public class RelatedItem {
@EmbeddedId
private RelatedItemPK id;

@ManyToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumns({
@JoinColumn(name="itemId", referencedColumnName="itemId", insertable=false, updatable=false),
@JoinColumn(name="item_client_id", referencedColumnName="item_client_id", insertable=false, updatable=false)
})
private Item item;
@ManyToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumns({
@JoinColumn(name="related_item_client_id", referencedColumnName="item_client_id", insertable=false, updatable=false),
@JoinColumn(name="relatedItemId", referencedColumnName="itemId", insertable=false, updatable=false)
})
private Item relatedItem;
...
}

问题是在为 RelatedItem 实体创建外键时,我收到了 SQLException。这是第二个失败的多对一关系。外键生成sql如下,
ALTER TABLE related_item ADD CONSTRAINT FK_related_item_related_item_client_id FOREIGN KEY (related_item_client_id, relatedItemId) REFERENCES item (item_client_id, itemId)

由于 item 表首先由 itemId 索引,然后由 item_client_id 索引,因此该语句会导致 MySQL 产生错误。

我想切换列的位置,以便 SQL 应如下所示,
ALTER TABLE related_item ADD CONSTRAINT FK_related_item_relatedItemId FOREIGN KEY (relatedItemId, related_item_client_id) REFERENCES item (itemId,item_client_id)

我尝试更改“JoinColumn”的顺序,但结果没有改变。我还尝试重命名字段以检查持久性提供程序是否按列名选择顺序,但结果再次没有改变。

那么,有没有办法强制执行列排序?

附言我使用以下东西:
  • MySQL 5.1
  • EclipseLink 2.0.0
  • Java EE 6
  • JPA 2
  • GlassFish v3

  • 编辑:EclipseLink 生成以下 SQL,但无法运行;
    CREATE TABLE related_item (SIMILARITY DOUBLE, widget_id INTEGER NOT NULL, relatedItemId VARCHAR(100) NOT NULL, itemId VARCHAR(100) NOT NULL, related_item_client_id INTEGER NOT NULL, item_client_id INTEGER NOT NULL, PRIMARY KEY (widget_id, relatedItemId, itemId, related_item_client_id, item_client_id));
    CREATE TABLE item (IMAGEURL VARCHAR(2048), STATUS VARCHAR(64), URL VARCHAR(2048), PRICE DOUBLE, STOCK INTEGER, DESCRIPTION TEXT(64000), NAME VARCHAR(255), ITEMID VARCHAR(100) NOT NULL, item_client_id INTEGER NOT NULL, PRIMARY KEY (ITEMID, item_client_id));
    ALTER TABLE related_item ADD CONSTRAINT FK_related_item_itemId FOREIGN KEY (itemId, item_client_id) REFERENCES item (itemId, item_client_id);
    ALTER TABLE related_item ADD CONSTRAINT FK_related_item_related_item_client_id FOREIGN KEY (related_item_client_id, relatedItemId) REFERENCES item (item_client_id, itemId);
    ALTER TABLE item ADD CONSTRAINT FK_item_item_client_id FOREIGN KEY (item_client_id) REFERENCES client (ID);

    最佳答案

    请包括堆栈跟踪。但是,我强烈建议您跳过 @JoinColumn 标签,除非您有 非常自己指定外键的好理由。通过在方向之一中指定mappedBy 属性,JPA 可以自行确定要做什么。

    Java EE 6 和 JPA 在启用约定优于配置方面投入了大量精力,这意味着大多数情况下,事情将开箱即用。这对您程序员来说是可取的,因为您需要担心的样板代码较少,而对于 JPA 和 Jave EE 容器实现者来说是可取的,因为它使他们可以自由地选择性能最佳的解决方案。通过自己声明外键关系,您和 JPA 都失去了这种优势。

    编辑:事实上,我怀疑指定 mappingBy 和指定 @JoinTable 都可能是您问题的根本原因。但是我需要查看堆栈跟踪才能确定。

    关于jpa - Java EE 6 JPA 2 ManyToOne 关系创建无效的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4462918/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com