3
Guys, I’m starting to learn how to use Hibernate and there was a kind of "error" in persisting a record. When registering a player object I must also pass the team of this player, however, the team is already registered in the database, so I should only pass the existing record so far all right, only when I run the persistence of the player it also persists a team again.
Summarizing: When a player is registered, he also re-registers the team that was passed to that player, even though the team was already registered in the database. Can someone help me with that?
Player class:
public class Jogador {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "jogador_Cod")
private int id;
@Column(name="jogador_Nome", length=100,nullable=false)
private String nome;
@Column(name="jogador_DataNascimento" ,length=10,nullable=false)
private String data_Nascimento;
@Column(nullable=false, name="jogador_Salario")
private double salario;
@Column(nullable=false, name="jogador_Camisa")
private int numeroCamisa;
@Column(nullable=false, name="jogador_EmCampo")
private boolean emCampo;
@Column(nullable=false, name="jogador_cartaoAmarelo")
private boolean cartaoAmarelo;
@Column(nullable=false, name="jogador_qtdCartaoAmarelo")
private int qtdCartaoAmarelo;
@Column(nullable=false, name="jogador_qtdCartaoVermelho")
private int qtdCartaoVermelho;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="time_Cod")
private Time time;`}
Time class:
public class Time {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="time_Cod")
private int id;
@Column(nullable=false,name="time_Nome",length=100)
private String nome;
@Column(nullable=false,name="time_Estado",length=100)
private String estado;
@Column(nullable=false,name="time_Pontos")
private int pontos;
@OneToMany(mappedBy="time")
private List<Jogador> jogadores;}
EDIT 1: Persistence Method
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("conexaoDB");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
try {
et.begin();
//
Time t = new Time();
t.setEstado("PERNAMBUCO");
t.setPontos(0);
t.setNome("Sport Club do Recife");
//
Time t2 = new Time();
t2.setEstado("SAO PAULO");
t2.setPontos(0);
t2.setNome("SANTOS");
//
Jogador j = new Jogador();
j.setCartaoAmarelo(false);
j.setData_Nascimento("29/02/9090");
j.setEmCampo(false);
j.setNome("Carlinhos Bala");
j.setNumeroCamisa(10);
j.setQtdCartaoAmarelo(3);
j.setQtdCartaoVermelho(1);
j.setSalario(1090);
j.setTime(t);
//
em.persist(j);
et.commit();
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
em.close();
emf.close();
}
}
Publish the player’s persistence methods, @Junior
– Marcos Sousa
Added Mark, the first 2 times objects were the ones I entered in the bank, as you can see, when I create the player object I just step a team and it turns out I persisted the record again
– Junior
Post the error that occurs please.
– Roknauta
So Douglas, it’s not a mistake in itself, the problem is, I registered the 2 teams that are in the Dadosfutebol class, when registering the player I pass one of these 2 teams, so far so good, including the record is completed, however, when entering the player, even if the team is already registered in the database Hibernate itself generates another record or I have 1. Sport 2. Sao Paulo, and when registering the player Hibernate includes a new, 3.Sao Paulo
– Junior
Have you tried to take the
cascade = CascadeType.ALL
of your Manytoone?– DiegoAugusto
@Junior, in this method you do not persist
time
, you just urge them.– Marcos Sousa