0

I have two Java classes Match (a Cricket match) and ball. In a match, there will be many balls and every ball will be part of a match. I need hibernate mapping between these two classes in a way,

  1. if I call save on match object, it should be able to persist all containing balls in ball table and remaining match details in match table.

  2. If I call save or update on ball object, first it should go to match table to check if match id present in ball object is available there or not. If it is there, it should save or update the ball object in ball table.

If any one does not know Match-Ball relation in Cricket, he can think as Foot Ball Match - Goal relation. In a match there can be n number of Goals and every goal will be part of a match. Saving Match should also be able to save all the contained goals in goal table and saving goal object should check in match table with its id and then should save goal details in goal table.

Thanks.

2 Answers 2

1

I think it is @ManyToOne and @OneToMany annotations will work for you. If you will save object Match which will (must) contain list of Goals, every Goal will save with Match.

Match.java

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "match")
public class Match {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

  @JsonBackReference
  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinTable(name = "goal", joinColumns = {
        @JoinColumn(name = "match_id", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "goal_id",
                nullable = false, updatable = false) })
    protected List<Goal> goals;
}

Goal.java

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "goal")
public class Goal {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "match", joinColumns = {
        @JoinColumn(name = "goal_id", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "match_id",
                nullable = false, updatable = false) })
    protected Match matche;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks !! One Goal can not be part of many Matches. It is part of of one Match only. It is Many-to-one on Goal. Even if I am going to save Goal, I don't want to save match, i just want to check whether match is present there or not. If it is not present then it will also save match. Please update your answer based on this comment.
Thanks for update. However in Match class, it should be OneToMany. Please update accordingly.
1
  • Match class :

@OneToMany(mappedBy = "match", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) private Set<Ball> balls;

  • Ball class :

@ManyToOne @JoinColumn(name = "MATCH_ID", nullable = false) private Match match;

1 Comment

Thanks Pras !! It was useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.