JPA (hibernate) onetomany relation

I am not sure what I am missing to make a bi-directional onetomany relationship (hibernate engine). Reduced version of the domain model:

class Person {
@OneToMany(mappedBy="personFrom", cascade = CascadeType.PERSIST)
    public List<Relationship> relationships;
}

class Relationship {
@ManyToOne
  public Person personFrom;

  @ManyToOne
  public Person personTo;
}

Some of the observations:
1. With the above display, a join table is not created.
2. When I delete mappedBy (@OneToMany (cascade = CascadeType.PERSIST)), a connection table is created and I maintain the connection through Person. The "personFrom" field is empty, but I think this is normal, since the relationship is maintained through the connection table.

I also tried to specify the join column in Relationship, it didn't make any difference. Any help greatly appreciated. thank.

Edit: 1

As with Dan's comment, if you need to see the full content of the domain class, I expanded it below.

class Relationship extends Model{

  @ManyToOne
  public RelationshipType relationshipType;

  @ManyToOne
  public Person personFrom;

  @ManyToOne
  public Person personTo;

  @ManyToOne
  public Person createdBy;

  @ManyToOne
  public Role roleFrom;

  @ManyToOne
  public Role roleTo;

    @Override
    public String toString() {
        return relationshipType.toString();
    }
}

class Person extends Model {

    public Date dateCreated;

    @Lob
    public String description;

    @OneToMany(cascade = CascadeType.ALL)
    public List<Role> roles;

    @OneToMany(mappedBy="personFrom", cascade = CascadeType.PERSIST)
    public List<Relationship> relationships;
}

, , personFrom, personTo .

Role extends Model {

    @ManyToOne
    public RoleType roleType;

    @ManyToOne
    public Person createdBy; 
}
+3
2

, .

OneToMany, "". , :

create table Person (
    id bigint not null,
    primary key (id)
)

create table Relationship (
    id bigint not null,
    personFrom_id bigint,
    personTo_id bigint,
    primary key (id)
)

alter table Relationship 
    add constraint FK499B69164A731563 
    foreign key (personTo_id) 
    references Person

alter table Relationship 
    add constraint FK499B691698EA8314 
    foreign key (personFrom_id) 
    references Person

( , ). , ManyToMany.

mappedBy (@OneToMany (cascade = CascadeType.PERSIST)), , Person. "personFrom" , , , .

unit test ( Hibernate API, ), , ( , ):

Person p1 = new Person();
Person p2 = new Person();
Relationship r = new Relationship();

// create the personFrom bi-directional association
r.setPersonFrom(p1);
List<Relationship> relationships = new ArrayList<Relationship>();
relationships.add(r);
p1.setRelationships(relationships); // these four lines should be moved to some 
                                    // link management method (see update below).

// create the personTo uni-directional association
r.setPersonTo(p2);

session.persist(p2);
session.persist(p1);

assertNotNull(p2.getId());
assertNotNull(p1.getId());
assertNotNull(r.getId());

Person ( 3 ).

, . , , ( , ).

: , , - . - :

public void addToRelationships(Relationship relationship) {
    if (this.relationships == null) {
       this.relationships = new ArrayList<Relationship>();
    }
    this.relationships.add(relationship);
    relationship.setPersonFrom(this);
}

1.2.6. Hibernate.

+2

? , PERSON_ID, :

class Relationship {
    @ManyToOne
    @JoinColumn(name="PERSON_ID")
    public Person personFrom;
    ...
}
0

Source: https://habr.com/ru/post/1756783/


All Articles