Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 如何从集合和直接引用中正确引用类_Jpa_One To Many_One To One_Many To One - Fatal编程技术网

Jpa 如何从集合和直接引用中正确引用类

Jpa 如何从集合和直接引用中正确引用类,jpa,one-to-many,one-to-one,many-to-one,Jpa,One To Many,One To One,Many To One,我有两个实体:Box和Link。 (两者都继承自_BaseClass,但我认为这并不相关——可能是……) 因此,一个框包含链接1、链接2和链接集合 \u基本实体: @MappedSuperclass public class _BaseEntity implements Comparable<_BaseEntity> { @Expose // @Id // @GeneratedValue() // protected long id; pub

我有两个实体:Box和Link。 (两者都继承自_BaseClass,但我认为这并不相关——可能是……)

因此,一个框包含链接1、链接2和链接集合

\u基本实体:

@MappedSuperclass
public class _BaseEntity implements Comparable<_BaseEntity> {
    @Expose //
    @Id //
    @GeneratedValue() //
    protected long id;

    public _BaseEntity() {}

    public long getID() {
        if (id == 0) return creationId;
        return id;
    }

    @Override public final int hashCode() {
        return (int) getID();
    }
    @Override public final boolean equals(final Object pObj) {
        if (pObj == null) return false;
        if (getClass() != pObj.getClass()) return false;
        final _BaseEntity other = (_BaseEntity) pObj;
        return id == other.id;
    }
    @Override public int compareTo(final _BaseEntity arg0) {
        return (int) (getID() - arg0.getID());
    }
}
@Entity
@Table(name = "PT_Box")
public class Box extends _BaseEntity {

    @Expose private String name;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link1;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link2;

    @Expose //
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private final ArrayList<Link> links = new ArrayList<>();

}
@Entity
@Table(name = "PT_Link")
public class Link extends _BaseEntity {

    @ManyToOne(fetch = FetchType.EAGER) //
    @JoinColumn(name = "parent_id") //
    private final Box parent; // dont expose for not looping!

    @Expose private String  name;
    @Expose private String  link;

    @Expose private Date    lastUpdate;
    @Expose private Date    nextUpdate;

}
问题:

@MappedSuperclass
public class _BaseEntity implements Comparable<_BaseEntity> {
    @Expose //
    @Id //
    @GeneratedValue() //
    protected long id;

    public _BaseEntity() {}

    public long getID() {
        if (id == 0) return creationId;
        return id;
    }

    @Override public final int hashCode() {
        return (int) getID();
    }
    @Override public final boolean equals(final Object pObj) {
        if (pObj == null) return false;
        if (getClass() != pObj.getClass()) return false;
        final _BaseEntity other = (_BaseEntity) pObj;
        return id == other.id;
    }
    @Override public int compareTo(final _BaseEntity arg0) {
        return (int) (getID() - arg0.getID());
    }
}
@Entity
@Table(name = "PT_Box")
public class Box extends _BaseEntity {

    @Expose private String name;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link1;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link2;

    @Expose //
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private final ArrayList<Link> links = new ArrayList<>();

}
@Entity
@Table(name = "PT_Link")
public class Link extends _BaseEntity {

    @ManyToOne(fetch = FetchType.EAGER) //
    @JoinColumn(name = "parent_id") //
    private final Box parent; // dont expose for not looping!

    @Expose private String  name;
    @Expose private String  link;

    @Expose private Date    lastUpdate;
    @Expose private Date    nextUpdate;

}
  • 链接被弄乱了。”link1'和link2'显示“link”的第一个元素
  • 反之亦然:如果我设置“link1”,那么列表“links”将显示它作为第一个元素
怀疑:

@MappedSuperclass
public class _BaseEntity implements Comparable<_BaseEntity> {
    @Expose //
    @Id //
    @GeneratedValue() //
    protected long id;

    public _BaseEntity() {}

    public long getID() {
        if (id == 0) return creationId;
        return id;
    }

    @Override public final int hashCode() {
        return (int) getID();
    }
    @Override public final boolean equals(final Object pObj) {
        if (pObj == null) return false;
        if (getClass() != pObj.getClass()) return false;
        final _BaseEntity other = (_BaseEntity) pObj;
        return id == other.id;
    }
    @Override public int compareTo(final _BaseEntity arg0) {
        return (int) (getID() - arg0.getID());
    }
}
@Entity
@Table(name = "PT_Box")
public class Box extends _BaseEntity {

    @Expose private String name;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link1;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link2;

    @Expose //
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private final ArrayList<Link> links = new ArrayList<>();

}
@Entity
@Table(name = "PT_Link")
public class Link extends _BaseEntity {

    @ManyToOne(fetch = FetchType.EAGER) //
    @JoinColumn(name = "parent_id") //
    private final Box parent; // dont expose for not looping!

    @Expose private String  name;
    @Expose private String  link;

    @Expose private Date    lastUpdate;
    @Expose private Date    nextUpdate;

}
我敢肯定,这是由于映射

  • 框:@OneToMany(mappedBy=“parent”)
  • 链接:@ManyToOne@JoinColumn(name=“parent\u id”)
它们还将这些链接链接到变量“link1”和“link2”中

问题:

@MappedSuperclass
public class _BaseEntity implements Comparable<_BaseEntity> {
    @Expose //
    @Id //
    @GeneratedValue() //
    protected long id;

    public _BaseEntity() {}

    public long getID() {
        if (id == 0) return creationId;
        return id;
    }

    @Override public final int hashCode() {
        return (int) getID();
    }
    @Override public final boolean equals(final Object pObj) {
        if (pObj == null) return false;
        if (getClass() != pObj.getClass()) return false;
        final _BaseEntity other = (_BaseEntity) pObj;
        return id == other.id;
    }
    @Override public int compareTo(final _BaseEntity arg0) {
        return (int) (getID() - arg0.getID());
    }
}
@Entity
@Table(name = "PT_Box")
public class Box extends _BaseEntity {

    @Expose private String name;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link1;

    @Expose //
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private Link link2;

    @Expose //
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "parent") //
    private final ArrayList<Link> links = new ArrayList<>();

}
@Entity
@Table(name = "PT_Link")
public class Link extends _BaseEntity {

    @ManyToOne(fetch = FetchType.EAGER) //
    @JoinColumn(name = "parent_id") //
    private final Box parent; // dont expose for not looping!

    @Expose private String  name;
    @Expose private String  link;

    @Expose private Date    lastUpdate;
    @Expose private Date    nextUpdate;

}
所以我的问题是:如何正确地管理/注释它?

  • 不知何故,联合国地图“链接1”和“链接2”
  • 将“link1”和“link2”注释为@Transient,以便仅由链接端设置引用?(如果真的行得通的话…)

您的映射不正确

首先,hashCode和equals()方法不应该使用生成的ID。您可能不应该有任何equals或hashCode方法。这是最安全的方法(参见)

其次,集合的类型必须是List,而不是ArrayList

第三:链接实体中需要三个不同的联接列(以及三个不同的父字段):

  • 要知道哪个框包含链接作为其链接1(OneToOne,OneToOne链接1关联的所有者方)
  • 要知道哪个框包含链接作为其链接2(OneToOne,OneToOne链接2关联的所有者方)
  • 要知道哪个框包含链接作为其链接列表的元素之一(多通,OneToMany链接协会的所有者端)

很酷,谢谢你的回答。首先:我知道问题所在,实际上我这里的实现已经减少了,它还有更多的内容,确保持久性不会出现问题。(EclipseLink/Payara甚至会在没有它的情况下引发问题。)第二:为什么它必须是列表,而不是ArrayList?我已经见过很多次了,但到目前为止,我在使用特定类型(ArrayList、HashSeh、TreeSet等)时从未遇到任何问题。第三:所以我确实需要在Link中使用三个单独的变量,每个变量都按照您编写的方式进行注释?或者有没有一种我不知道的方法,来注释同一个变量。因为JPA规范要求它。它还强制执行它,因为JPA引擎必须能够使用自己的集合实现来处理延迟加载。无论如何,编程到接口是一种最佳实践。3.是的,你需要3个独立的变量,按照我建议的方式注释。再次使用Thanx进行详细说明。我试试看,然后选择它作为解决方案。还有一点“但是”:
编程到接口无论如何是一种最佳实践。
并不完全正确<代码>接受尽可能通用的参数,返回尽可能具体的值。因为我总是可以毫无问题地转换type->supertype。超级类型->类型是危险的。另外,在一些JVM实现中,调用接口方法比调用已实现的方法慢。在JPAs自定义集合实现的情况下,遵循您的参数,这更像是JPA方面的“懒散”。好的,这最终很有效。必须扩展CTOR并创建一个
@临时框父项
并添加一个
@PostLoad
方法,将父对象设置为三种可能性之一。但总的来说,它运行得很好。又是唐克斯。