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

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

Prevent creation of foreign keys in mapping tables?

问题

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

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

如何实现这一点?

@Entity
@Table(name = "chunk", uniqueConstraints = {@UniqueConstraint(columnNames={"x", "y"})}, indexes = {@Index(columnList = "x,y")})
@Access(value = AccessType.FIELD)
@SelectBeforeUpdate(false)
public class Chunk extends HibernateComponent{

    public int x;
    public int y;
    public Date createdOn;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(FetchMode.JOIN)
    @BatchSize(size = 50)
    public Set<Identity> inChunk = new LinkedHashSet<>();

    @Transient
    public Set<ChunkLoader> loadedBy = new LinkedHashSet<>();

    public Chunk() {}
    public Chunk(int x, int y, Date createdOn) {
        this.x = x;
        this.y = y;
        this.createdOn = createdOn;
    }
}

我们如何实现这一点?

英文:

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.

@Entity
@Table(name = &quot;chunk&quot;, uniqueConstraints = {@UniqueConstraint(columnNames={&quot;x&quot;, &quot;y&quot;})}, indexes = {@Index(columnList = &quot;x,y&quot;)})
@Access(value = AccessType.FIELD)
@SelectBeforeUpdate(false)
public class Chunk extends HibernateComponent{

    public int x;
    public int y;
    public Date createdOn;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(FetchMode.JOIN)
    @BatchSize(size = 50)
    public Set&lt;Identity&gt; inChunk = new LinkedHashSet&lt;&gt;();

    @Transient
    public Set&lt;ChunkLoader&gt; loadedBy = new LinkedHashSet&lt;&gt;();

    public Chunk() {}
    public Chunk(int x, int y, Date createdOn) {
        this.x = x;
        this.y = y;
        this.createdOn = createdOn;
    }
}

How do we achieve this ?

答案1

得分: 0

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

@Entity
@Table(name = "chunk", uniqueConstraints = {@UniqueConstraint(columnNames={"x", "y"})}, indexes = {@Index(columnList = "x,y")})
@Access(value = AccessType.FIELD)
@SelectBeforeUpdate(false)
public class Chunk extends HibernateComponent{

    public int x;
    public int y;
    public Date createdOn;

    @OneToMany(fetch = FetchType.EAGER, cascade = {})
    @JoinTable(name = "chunk_identity", joinColumns = @JoinColumn(name = "identity_id"), inverseJoinColumns = @JoinColumn(name = "id"), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @Fetch(FetchMode.JOIN)
    @BatchSize(size = 50)
    public Set<Identity> inChunk = new LinkedHashSet<>();

    @Transient
    public Set<ChunkLoader> loadedBy = new LinkedHashSet<>();

    public Chunk() {}
    public Chunk(int x, int y, Date createdOn) {
        this.x = x;
        this.y = y;
        this.createdOn = createdOn;
    }
}

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

英文:

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

@Entity
@Table(name = &quot;chunk&quot;, uniqueConstraints = {@UniqueConstraint(columnNames={&quot;x&quot;, &quot;y&quot;})}, indexes = {@Index(columnList = &quot;x,y&quot;)})
@Access(value = AccessType.FIELD)
@SelectBeforeUpdate(false)
public class Chunk extends HibernateComponent{

    public int x;
    public int y;
    public Date createdOn;

    @OneToMany(fetch = FetchType.EAGER, cascade = {})
    @JoinTable(name = &quot;chunk_identity&quot;, joinColumns = @JoinColumn(name = &quot;identity_id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;id&quot;), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @Fetch(FetchMode.JOIN)
    @BatchSize(size = 50)
    public Set&lt;Identity&gt; inChunk = new LinkedHashSet&lt;&gt;();

    @Transient
    public Set&lt;ChunkLoader&gt; loadedBy = new LinkedHashSet&lt;&gt;();

    public Chunk() {}
    public Chunk(int x, int y, Date createdOn) {
        this.x = x;
        this.y = y;
        this.createdOn = createdOn;
    }
}

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:

确定