Hibernate doesn't insert record to database

190
June 12, 2019, at 3:00 PM

I try to add a record to the database. In debug, I check to create object and object created is good. When I use getCurrentSession().save(entity); this doesn't add a record to the database but the session is open. I have a composite foreign key, to create this key I create an extra class.

class:

package nba_statistics.entities;
import org.hibernate.annotations.Cascade;
import javax.persistence.*;
@Entity(name = "PlayerMatchPositions")
@Table(name = "player_match_positions")
public class PlayerMatchPositions {
    public void setId(PlayerMatchPositionsId id) {
        this.id = id;
    }
    @EmbeddedId
    private PlayerMatchPositionsId id;
    @ManyToOne
    @MapsId("player_id")
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "player_id")
    private Players player;
    @ManyToOne
    @MapsId("match_id")
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "match_id")
    private Matches match;
    @ManyToOne
    @MapsId("position_id")
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "position_id")
    private Positions position;
    public PlayerMatchPositions(){};
    public PlayerMatchPositions(Players player, Matches match, Positions position) {
        id = new PlayerMatchPositionsId();
        this.player = player;
        this.match = match;
        this.position = position;
    }
    public Players getPlayer() {
        return player;
    }
    public void setPlayer(Players player) {
        this.player = player;
    }
    public Matches getMatch() {
        return match;
    }
    public void setMatch(Matches match) {
        this.match = match;
    }
    public Positions getPosition() {
        return position;
    }
    public void setPosition(Positions position) {
        this.position = position;
    }
    @Override
    public String toString() {
        return "PlayerMatchPositions{" +
                "id=" + id +
                ", player=" + player +
                ", match=" + match +
                ", position=" + position +
                '}';
    }
}

Extra class to foreign key:

package nba_statistics.entities;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
public
class PlayerMatchPositionsId implements Serializable {
    @Column(name = "player_id")
    int playerId;
    @Column(name = "match_id")
    int matchId;
    @Column(name = "position_id")
    int positionId;
    public PlayerMatchPositionsId(){}
    public PlayerMatchPositionsId(int playerId, int matchId, int positionId) {
        this.playerId = playerId;
        this.matchId = matchId;
        this.positionId = positionId;
    }
    public int getPlayerId() {
        return playerId;
    }
    public void setPlayerId(int playerId) {
        this.playerId = playerId;
    }
    public int getMatchId() {
        return matchId;
    }
    public void setMatchId(int matchId) {
        this.matchId = matchId;
    }
    public int getPositionId() {
        return positionId;
    }
    public void setPositionId(int positionId) {
        this.positionId = positionId;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PlayerMatchPositionsId)) return false;
        PlayerMatchPositionsId that = (PlayerMatchPositionsId) o;
        return Objects.equals(playerId, that.playerId) &&
                Objects.equals(positionId, that.positionId)&&
                Objects.equals(matchId, that.matchId);
    }
    @Override
    public int hashCode() {
        return Objects.hash(playerId, positionId, matchId);
    }
}

Dao:

package nba_statistics.dao.classes;
import nba_statistics.dao.interfaces.IPlayerMatchPositionsDao;
import nba_statistics.entities.*;
public class PlayerMatchPositionsDao extends Dao implements IPlayerMatchPositionsDao {
    public void persist(PlayerMatchPositions entity) {
        getCurrentSession().save(entity);
    }
    @Override
    public int getData(Players player, Matches match, Positions position){
        if(player == null)
        {
            return 1;
        }
        if(match==null)
        {
            return 2;
        }
        if(position==null)
        {
            return 3;
        }
        PlayerMatchPositions playerMatchPositions = new PlayerMatchPositions();
        playerMatchPositions.setPlayer(player);
        playerMatchPositions.setMatch(match);
        playerMatchPositions.setPosition(position);
        playerMatchPositions.setId(new PlayerMatchPositionsId(player.getId(),match.getId(),position.getId()));
        persist(playerMatchPositions);
        return 0;
    }
}

In other class, I do it similar and all is good, only in this class, it doesn't add to the database.

READ ALSO
Find ROM Size Programatically

Find ROM Size Programatically

I am trying to create a test application for a device which checks the flash ROM size on the device for the tester to verify

178
Is it possible to identify what's the nearest street on google maps?

Is it possible to identify what's the nearest street on google maps?

Is there a way to know the nearest street without using the Directions API of google? Like Uber and 99 does

135
How to add colum JTable to Jcombobox in Java Eclipse

How to add colum JTable to Jcombobox in Java Eclipse

I add the column value of JTable to JComBoBox full of null notifications

177
App Crashes when decimals operated using Rhino Library

App Crashes when decimals operated using Rhino Library

I am using Rhino Library to eavaluate a string in android studioWhile Doing it I got an error, the app crashes when decimals are multiplied

141