阻止在映射表中创建外键?

huangapple go评论96阅读模式
英文:

Prevent creation of foreign keys in mapping tables?

问题

如何在Hibernate中防止创建外键?我需要将一些尚不存在的子项插入到我的“oneToMany”关系中。Hibernate应该忽略子项尚不存在的事实。

目前,当我尝试插入这些不存在的子项时,我会收到外键异常。因此,我需要禁用外键的创建和使用。

如何实现这一点?

  1. @Entity
  2. @Table(name = "chunk", uniqueConstraints = {@UniqueConstraint(columnNames={"x", "y"})}, indexes = {@Index(columnList = "x,y")})
  3. @Access(value = AccessType.FIELD)
  4. @SelectBeforeUpdate(false)
  5. public class Chunk extends HibernateComponent{
  6. public int x;
  7. public int y;
  8. public Date createdOn;
  9. @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  10. @Fetch(FetchMode.JOIN)
  11. @BatchSize(size = 50)
  12. public Set<Identity> inChunk = new LinkedHashSet<>();
  13. @Transient
  14. public Set<ChunkLoader> loadedBy = new LinkedHashSet<>();
  15. public Chunk() {}
  16. public Chunk(int x, int y, Date createdOn) {
  17. this.x = x;
  18. this.y = y;
  19. this.createdOn = createdOn;
  20. }
  21. }

我们如何实现这一点?

英文:

How do we prevent the creation of foreign keys in hibernate ?
I need to insert some childs into my "oneToMany" relationship which do not exist yet. Hibernate should just ignore the fact that the childs do not exist yet.

Currently i receive a foreign key exception once i try to insert those non existing childs. So i need to disable the foreign key creation/useage.

  1. @Entity
  2. @Table(name = &quot;chunk&quot;, uniqueConstraints = {@UniqueConstraint(columnNames={&quot;x&quot;, &quot;y&quot;})}, indexes = {@Index(columnList = &quot;x,y&quot;)})
  3. @Access(value = AccessType.FIELD)
  4. @SelectBeforeUpdate(false)
  5. public class Chunk extends HibernateComponent{
  6. public int x;
  7. public int y;
  8. public Date createdOn;
  9. @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  10. @Fetch(FetchMode.JOIN)
  11. @BatchSize(size = 50)
  12. public Set&lt;Identity&gt; inChunk = new LinkedHashSet&lt;&gt;();
  13. @Transient
  14. public Set&lt;ChunkLoader&gt; loadedBy = new LinkedHashSet&lt;&gt;();
  15. public Chunk() {}
  16. public Chunk(int x, int y, Date createdOn) {
  17. this.x = x;
  18. this.y = y;
  19. this.createdOn = createdOn;
  20. }
  21. }

How do we achieve this ?

答案1

得分: 0

我找到了一个解决方案... 不必让 hibernate 完全自己创建联接表,你可以通过使用 "JoinTable" 来修改创建过程。

  1. @Entity
  2. @Table(name = "chunk", uniqueConstraints = {@UniqueConstraint(columnNames={"x", "y"})}, indexes = {@Index(columnList = "x,y")})
  3. @Access(value = AccessType.FIELD)
  4. @SelectBeforeUpdate(false)
  5. public class Chunk extends HibernateComponent{
  6. public int x;
  7. public int y;
  8. public Date createdOn;
  9. @OneToMany(fetch = FetchType.EAGER, cascade = {})
  10. @JoinTable(name = "chunk_identity", joinColumns = @JoinColumn(name = "identity_id"), inverseJoinColumns = @JoinColumn(name = "id"), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
  11. @Fetch(FetchMode.JOIN)
  12. @BatchSize(size = 50)
  13. public Set<Identity> inChunk = new LinkedHashSet<>();
  14. @Transient
  15. public Set<ChunkLoader> loadedBy = new LinkedHashSet<>();
  16. public Chunk() {}
  17. public Chunk(int x, int y, Date createdOn) {
  18. this.x = x;
  19. this.y = y;
  20. this.createdOn = createdOn;
  21. }
  22. }

这样我们就可以定义是否需要外键,或者是否可以简单地忽略它。

英文:

I found an solution... instead of letting hibernate create the join table completly by yourself you can modify the creation with "JoinTable".

  1. @Entity
  2. @Table(name = &quot;chunk&quot;, uniqueConstraints = {@UniqueConstraint(columnNames={&quot;x&quot;, &quot;y&quot;})}, indexes = {@Index(columnList = &quot;x,y&quot;)})
  3. @Access(value = AccessType.FIELD)
  4. @SelectBeforeUpdate(false)
  5. public class Chunk extends HibernateComponent{
  6. public int x;
  7. public int y;
  8. public Date createdOn;
  9. @OneToMany(fetch = FetchType.EAGER, cascade = {})
  10. @JoinTable(name = &quot;chunk_identity&quot;, joinColumns = @JoinColumn(name = &quot;identity_id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;id&quot;), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
  11. @Fetch(FetchMode.JOIN)
  12. @BatchSize(size = 50)
  13. public Set&lt;Identity&gt; inChunk = new LinkedHashSet&lt;&gt;();
  14. @Transient
  15. public Set&lt;ChunkLoader&gt; loadedBy = new LinkedHashSet&lt;&gt;();
  16. public Chunk() {}
  17. public Chunk(int x, int y, Date createdOn) {
  18. this.x = x;
  19. this.y = y;
  20. this.createdOn = createdOn;
  21. }
  22. }

This way we can define if we need an foreign key or if we can simply ignore it.

huangapple
  • 本文由 发表于 2020年10月7日 20:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64244141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定