简体   繁体   中英

JPA : how to avoid bi-directional values during fetching?

I am using JPA in my project. Initially i haven't any problem when i declared my entities without bi-directional relationship. because my requirement was like, we used jpa only for fetching the values from db. so, in design perspective we removed unwanted bi-directional fields. example: Entity A has B as child but B haven't A .

The second thing, we updated our code for requirement 2. it is persisting entities in database with UUID as a key. Since some of the tables related this UUID as a primary key. so i used @GenerateValue and created a entity. it works for master table. but i not mapped to child table. throwed cant not insert NULL exception .

I googled and found a solution for this issue. Google said, using @MapsId will solve this issue. I used @MapsId . It dosen't worked for me. Again googled and got a point that, if we use bi-directional relationship, then the @MapsId maps the UUID value. so, i introduced bi-directional with @MapsId and mappedby attributes in @OneToMany . Then my entity got persist in the database.

Now my problem is, the requirement one's performance got down. because, due to my update (bi-directional relationship) each and every entity gives data from the database with bi-directional objects ( huge numbers - millions).

 My Code: 
 `Parent{
    @-----(mappedby = "parent")
    @PrimaryKeyJoinColumn
    private Set<Child> childs;
 }`

 `Child{
    @JoinColumn(name = "id")
    private Parent parent;
 }`

My Questions:

  • How to avoid parent object from child objects during fetching?
  • I will use child object for persisting but not for fetching. how to do?

    if i use parent.getChild(), i need child values but, if i use parent.getChild().getParent() i need null... how should i config?

Thanks in advance. Please reply for my quires.

Thanks, King

Two type of fetch in jpa EAGER and LAZY fetch

The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched ( fetch in one query ). ie one to many relation school has many student then you repetitive the school object school name,school address and all the student student id,student name list are fetch

The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed( fetch when needed as sub-queries). ie when you fetch the school object then to student object load on demand at runtime when you call the school's getStudents() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM