Hibernate不会将记录插入数据库

时间:2019-06-11 16:03:24

标签: java hibernate

我尝试将记录添加到数据库。在调试中,我检查创建对象和创建的对象是否良好。当我使用 getCurrentSession()。save(entity); 时,这不会向数据库添加记录,但是会话是打开的。我有一个复合外键,要创建此键,我要创建一个额外的类。

班级:

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 +
                '}';
    }
}

外键的额外类:

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);
    }
}

道:

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;
    }
}

在另一个类中,我也这样做,而且一切都很好,仅在这个类中,它不会添加到数据库中。

0 个答案:

没有答案