Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Problema com chave estrangeira

Tô fazendo um projeto como estudo e tá dando um erro que não sei o que pode ser: @OneToOne or @ManyToOne on br.com.thiago.rpg.model.Hero.playerID references an unknown entity: int

As entidades:

package br.com.thiago.rpg.model;

@Entity
@Table(name="player")
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long playerId;
    private String playerName;

    public Player() {
    }

    public Player(Long playerId, String playerNome) {
        this.playerId = playerId;
        this.playerName = playerNome;
    }

    public Player(String playerNome) {
        this.playerName = playerNome;
    }

}
package br.com.thiago.rpg.model;

@Entity
@Table(name="hero")
public class Hero {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long HeroId;
    private String HeroName;
    private int level;
    private int strenght;
    private int hp;
    private int speed;
    @ManyToOne
    private int playerID;
    @ManyToOne
    private Race RaceId;


    public Hero() {
    }

    public Hero(String charName, int level, int strenght, int hp, int speed, int player, Race RaceId, Long HeroId) {
        super();
        this.HeroName = charName;
        this.level = level;
        this.strenght = strenght;
        this.hp = hp;

        this.speed = speed;
        this.playerID = player;
        this.RaceId = RaceId;
        this.HeroId = HeroId;
    }


    public Hero(String charName, int level, int strenght, int hp, int speed, int playerId,Race RaceId) {
        super();
        this.HeroName = charName;
        this.level = level;
        this.strenght = strenght;
        this.hp = hp;
        this.speed = speed;
        this.playerID = playerId;
        this.RaceId = RaceId;
    }

}
package br.com.thiago.rpg.model;

@Entity
@Table(name="race")
public class Race {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Long raceId;
    private String raceName;

    public Race() {

    }

    public Race(String raceName) {
        this.raceName = raceName;
    }

    public Race(String raceName, long raceId) {
        this.raceName = raceName;
        this.raceId = raceId;
    }
package br.com.thiago.rpg.teste;

import br.com.thiago.rpg.dao.HeroDao;
import br.com.thiago.rpg.dao.PlayerDao;
import br.com.thiago.rpg.dao.RaceDao;
import br.com.thiago.rpg.model.Hero;
import br.com.thiago.rpg.model.Player;
import br.com.thiago.rpg.model.Race;

public class CadastroDePlayerEHeros {

    public static void main(String[] args) {

        Player p = new Player("Thiago");
        Race human = new Race("Human", 1l);
        Hero h1 = new Hero("Karas", 1, 10, 10, 10, 1, human);

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("rpg");
        EntityManager em = factory.createEntityManager();

        PlayerDao playerDao = new PlayerDao(em);
        HeroDao heroDao = new HeroDao(em);
        RaceDao raceDao = new RaceDao(em);

        em.getTransaction().begin();
        heroDao.cadastrar(h1);
        em.getTransaction().commit();
        em.close();
    }
}

O erro ocorre quando tento dar o cadastrar no h1.

2 respostas

Essas são as tables no MySQL. O cadastrar funcionou no no Player normal.

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

solução!

Já resolvi o erro,

Eu tava colocando o PlayerID no Hero como Int ao invés de colcoar como Player.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software