6

Below is my table design. can someone explain me how to configure my entity using spring data jpa?

PARENT_TABLE(
id primary key,
name
)

SECOND_CHILD_TABLE(
id primary key,
second_child_name,
parent_id references id on  parent_table,
first_child_id references id on first_child_table
)

FIRST_CHILD_TABLE(
id primary key,
first_child_name,
parent_id references id on  parent_table
)
Jens
  • 67,715
  • 15
  • 98
  • 113
user3560205
  • 101
  • 1
  • 2
  • 4
  • 1
    This question, is about the entity mapping, so it is related to JPA (or maybe Hibernate). But this has nothing to do with Spring Data JPA, so I would recommend, that you rephrase the question, in order to get the attention of the right people. – Ralph Jun 27 '14 at 06:45
  • Possible duplicate of [JPA 2: multiple column usage in foreign keys](http://stackoverflow.com/questions/4705088/jpa-2-multiple-column-usage-in-foreign-keys) – tak3shi Mar 14 '17 at 11:48

2 Answers2

7

If you are asking for JPA Mapping then should be as following.

@Entity
@Table(name="parent_table")
public class Parent {
    @Id
    @Column(name="ID", nullable=false, unique=true)
    // Require Generator config
    private Long id;

    @Column(name="NAME", nullable=false)
    private String name;
}

@Entity
@Table(name="first_child_table")
public class FirstChild {
    @Id
    @Column(name="ID", nullable=false, unique=true)
    // Require Generator config
    private Long id;

    @Column(name="FIRST_CHILD_NAME", nullable=false)
    private String name;

    @OneToOne
    @JoinColumn(name="parent_id", referencedColumnName="ID")
    private Parent parent;
}

@Entity
@Table(name="second_child_table")
public class SecondChild {
    @Id
    @Column(name="ID", nullable=false, unique=true)
    // Require Generator config
    private Long id;

    @Column(name="SECOND_CHILD_NAME", nullable=false)
    private String name;

    @OneToOne
    @JoinColumn(name="parent_id", referencedColumnName="ID")
    private Parent parent;

    @OneToOne
    @JoinColumn(name="first_child_id", referencedColumnName="ID")
    private FirstChild firstChild;
}
shazin
  • 21,379
  • 3
  • 54
  • 71
  • Shazin, Thanks for the prompt response. I would like to save my parent entity alone by having linking between parent and child in the jpa level...so if i persist parent table then child tables should also be persisted in the database. Can you slightly modify your design and let me know. (Note: i need a one to many relationship between parent and child) – user3560205 Jun 27 '14 at 07:18
  • There is no mapping to mulitple foreign keys in this answer – tak3shi Mar 14 '17 at 11:48
4
@Entity
@Table(name="parent_table")
public class Parent {
@Id
@Column(name="ID", nullable=false, unique=true)
// Require Generator config
private Long id;

@Column(name="NAME", nullable=false)
private String name; 

@OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "candidacy_id", nullable = false)
@Getter
@Setter
private List<FirstChild> firstChild = new ArrayList<>();


@OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "candidacy_id", nullable = false)
@Getter
@Setter
private List<SecondChild> secondChild = new ArrayList<>();
}

  @Entity
 @Table(name="first_child_table")
 public class FirstChild {
@Id
@Column(name="ID", nullable=false, unique=true)
// Require Generator config
private Long id;

@Column(name="FIRST_CHILD_NAME", nullable=false)
private String name;

@ManyToOne
@JoinColumn(name="parent_id", referencedColumnName="ID")
private Parent parent;
}

@Entity
@Table(name="second_child_table")
public class SecondChild {
@Id
@Column(name="ID", nullable=false, unique=true)
// Require Generator config
private Long id;

@Column(name="SECOND_CHILD_NAME", nullable=false)
private String name;

@ManyToOne
@JoinColumn(name="parent_id", referencedColumnName="ID")
private Parent parent;


}

And for the repository

     @Repository
     public interface ParentRepository extends CrudRepository<Parent, Integer> {


      }
paul
  • 12,873
  • 23
  • 91
  • 153
  • Hi Paul, My second child table has reference from both parent_table and first_child_table. The above will not work for my table design. Please let me know if I need to change in my table design if this is not feasible using JPA – user3560205 Jun 27 '14 at 08:58
  • you can add that relationship, but is a little weird to have a relationship between two entities when you already have with the father. I would do relationship between parent->first son->second son. Dony you see that having first-son and second son relationship youo will have two relationship with parent in second son? – paul Jun 27 '14 at 09:03
  • so in that case, if i change the relationship as you mentioned ( parent->first son->second son).... what would be the jpa design? can you modify your version? thanks in advance paul. – user3560205 Jun 27 '14 at 09:27
  • Try to figure out you first. Here we are to teach and learn. Now I´m bussy. ;) – paul Jun 27 '14 at 09:42
  • There is no mapping to mulitple foreign keys in this answer – tak3shi Mar 14 '17 at 11:48