Could Hibernate, when the tables are tied (@OneToMany @ManyToOne) be replaced by field Id?
-
The problem is, I have a precipitation/temporaryrelated class, but when I get an object that has an ancestors and descendants from the OBD, it's not only these objects, but also objects related to ancestors and descendants, and I'd have enough to receive only an Id an ancestor and list Id descendants, is it possible to tie the tables and only get them Id out of them?
It's like,
@Entity @Table(name="testpage") public class TestPage { @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.AUTO) private long Id; @Column(unique=true,nullable=false) private String Name; private int position; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn() @JoinTable(name="parent_testpage",joinColumns=@JoinColumn(name="page_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="parent_id", referencedColumnName="id")) private TestPage Parent; @OneToMany(fetch = FetchType.EAGER) @JoinTable(name="parent_testpage",joinColumns=@JoinColumn(name="parent_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="page_id", referencedColumnName="id")) private List<TestPage> Child;
...getters and setters
}
I would like that:
@Entity
@Table(name="testpage")
public class TestPage {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private long Id;
@Column(unique=true,nullable=false)
private String Name;
private int position;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn()
@JoinTable(name="parent_testpage",joinColumns=@JoinColumn(name="page_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="parent_id", referencedColumnName="id"))
private long ParentId;@OneToMany(fetch = FetchType.EAGER) @JoinTable(name="parent_testpage",joinColumns=@JoinColumn(name="parent_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="page_id", referencedColumnName="id")) private List<Long> ChildIds;
...getters and setters
}
-
I'll be right there for JPA. Hibernate has some of his own mechanisms - I don't know them.
Only a separate request, as Ruslan Nekura says, will help get id child-ov.
(But there's another option. He's a little lower.JPA doesn't work differently. He does not refer to individual fields of other entity. It's all on the target.
parent_Id is a field in the same entity. It can be used both at the same time and even at the same time:... @JoinTable(name="parent_testpage", joinColumns=@JoinColumn(name="page_id", referencedColumnName="id"), inverseJoinColumns=@JoinColumn(name="parent_id", referencedColumnName="id")) private TestPage Parent;
@Column(name="parent_id", insertable=false, updatable=false)
private Long parentId;
...
If used at the same time, one display must be marked as reading only. In the example, it is a parentId with annotation parameters insertable, updatable = false.
With a list of child-bearings, as mentioned above, this number will not pass.
And there's another method.
You can make a view in the base with the fields of parent_id and child_id.CREATE VIEW view_child_ids (long parent_id, long child_id) AS
SELECT parent_id, id FROM testpage WHERE parent_id IS NOT NULL;
Describe this view as Basic ElementCollection:
...
@ElementCollection
@CollectionTable(name = "view_child_ids",
joinColumns=@JoinColumn(name = "parent_id"))
@Column(name = "child_id")
private List<Long> childIds;
...
For this view, all rules/triggers will still need to be determined in case of cascade removal/change.
There are my shortcomings in this, but I won't mention them. They appear under certain circumstances. Maybe you'll be lucky, because nobody knows your whole idea. I prefer requests.Radical option is to leave JPA/Hibernate on an alternative way of accessing data, working on other principles. Active-recording.