Gostaria de saber se tem algum jeito de fazer o relacionamento para persistir dados em um banco de dados mysql usando JTA (java transaction api ), não gostaria de usar JPA. Eu consigo salvar dados sem relacionamento usando a api JTA tranquilamente segue abaixo como é o relacionamento e como quero salvar na tabela:
tabela Perfil
CREATE TABLE IF NOT EXISTS perfil (
cod_perfil BIGINT(20) NOT NULL ,
tipo VARCHAR(255) NULL DEFAULT NULL ,
subperfis BIGINT(20) NULL DEFAULT NULL ,
PRIMARY KEY (cod_perfil) ,
INDEX fk_perfil_perfil_idx (subperfis ASC) ,
CONSTRAINT fk_perfil_perfil
FOREIGN KEY (subperfis )
REFERENCES perfil (cod_perfil )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
classe Perfil
package cms.dao;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Perfil implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name="cod_perfil")
private Long perfil;
private String tipo;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="perfil_filho")
private Perfil fk_perfil;
@OneToMany(mappedBy="fk_perfil")
private List<Perfil> perfil_filho;
public Long getPerfil() {
return perfil;
}
public void setPerfil(Long perfil) {
this.perfil = perfil;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public Perfil getFk_perfil() {
return fk_perfil;
}
public void setFk_perfil(Perfil fk_perfil) {
this.fk_perfil = fk_perfil;
}
public List<Perfil> getPsubperfis() {
return perfil_filho;
}
public void setPsubperfis(List<Perfil> perfil_filho) {
this.perfil_filho = perfil_filho;
}
}
classe PerfilRepositorio
package transacoes.negocio;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import cms.dao.Perfil;
@Stateless
public class PerfilRepositorio {
@PersistenceContext(unitName="K19")
private EntityManager entidade;
public void adicionar(Perfil perfil){
this.entidade.persist(perfil);
}
public List<Perfil> getPerfil(){
TypedQuery<Perfil> query = this.entidade.createQuery("select x from Perfil x",
Perfil.class);
return query.getResultList();
}
public void atualizar(Perfil perfil){
this.entidade.refresh(perfil);
}
}
Ao tentar persistir deste jeito ocorre um erro gostaria de saber se tem alguma solução.