Jpa 如何从集合和直接引用中正确引用类
我有两个实体:Box和Link。 (两者都继承自_BaseClass,但我认为这并不相关——可能是……) 因此,一个框包含链接1、链接2和链接集合 \u基本实体: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
@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”)
@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,以便仅由链接端设置引用?(如果真的行得通的话…)
- 要知道哪个框包含链接作为其链接1(OneToOne,OneToOne链接1关联的所有者方)
- 要知道哪个框包含链接作为其链接2(OneToOne,OneToOne链接2关联的所有者方)
- 要知道哪个框包含链接作为其链接列表的元素之一(多通,OneToMany链接协会的所有者端)
编程到接口无论如何是一种最佳实践。
并不完全正确<代码>接受尽可能通用的参数,返回尽可能具体的值。因为我总是可以毫无问题地转换type->supertype。超级类型->类型是危险的。另外,在一些JVM实现中,调用接口方法比调用已实现的方法慢。在JPAs自定义集合实现的情况下,遵循您的参数,这更像是JPA方面的“懒散”。好的,这最终很有效。必须扩展CTOR并创建一个@临时框父项
并添加一个@PostLoad
方法,将父对象设置为三种可能性之一。但总的来说,它运行得很好。又是唐克斯。