Ir para o conteúdo
  • Revista PROGRAMAR: Já está disponível a edição #59 da revista programar. Faz já o download aqui!

knoker

Java EJB "Registos criados que não aparecem"

Mensagens Recomendadas

knoker    0
knoker

Boas,

estou a usar Ejb pela primeira vez, e estou me a deparar com um problema no mínimo estranho.

Tenho a Classe Snippet que por sua vez contem uma Colecção de Revisions, quando crio um objecto snippet com as respectivas revisions ele grava-o na DB sem problema, mas em seguida tento ir busca-lo e a collection vem vazia... O que é que me estará a falhar?

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pt.com.node.thorsnippets.dao;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author knoker
*/
@javax.ejb.Stateless
@Entity
@Table(name = "snippet")
@XmlRootElement
@NamedQueries({
   @NamedQuery(name = "Snippet.findAll", query = "SELECT s FROM Snippet s"),
   @NamedQuery(name = "Snippet.findByIdSnippet", query = "SELECT s FROM Snippet s WHERE s.idSnippet = :idSnippet"),
   @NamedQuery(name = "Snippet.findByOwner", query = "SELECT s FROM Snippet s WHERE s.owner = :owner"),
   @NamedQuery(name = "Snippet.findByIsPublic", query = "SELECT s FROM Snippet s WHERE s.isPublic = :isPublic")})
public class Snippet implements Serializable {
   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idSnippet")
   private Integer idSnippet;
   @Basic(optional = false)
   @Column(name = "isPublic")
   private boolean isPublic;
   @Basic(optional = false)
   @Column(name = "title")
   private String title;
   @JoinTable(name = "snippet_tag", joinColumns = {
    @JoinColumn(name = "idSnippet", referencedColumnName = "idSnippet")}, inverseJoinColumns = {
    @JoinColumn(name = "tag", referencedColumnName = "tag")})
   @ManyToMany
   private Collection<Tag> tagCollection;
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "idSnippet", fetch = FetchType.EAGER)
   private Collection<Revision> revisionCollection;
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "snippet", fetch = FetchType.EAGER)
   private Collection<RatingSnippet> ratingSnippetCollection;
   @Basic(optional = false)
   @Column(name = "owner")
   private int owner;
   @JoinColumn(name = "language", referencedColumnName = "language")
   @ManyToOne(optional = false)
   private Language language;
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "idSnippet", fetch = FetchType.EAGER)
   private Collection<Comment> commentCollection;
   public Snippet() {
   }
   public Snippet(Integer idSnippet) {
    this.idSnippet = idSnippet;
   }
   public Snippet(Integer idSnippet, int owner, boolean isPublic) {
    this.idSnippet = idSnippet;
    this.owner = owner;
    this.isPublic = isPublic;
   }
   public Snippet(Integer id, String title, User owner, Language language, boolean isPublic) {
    this.idSnippet = id;
    this.title = title;
    this.owner = owner.getIdUser();
    this.language = language;
    this.isPublic = isPublic;
   }
   public Snippet(String title, User owner, Language language, boolean isPublic) {
    this.title = title;
    this.owner = owner.getIdUser();
    this.language = language;
    this.isPublic = isPublic;
   }
   public Integer getIdSnippet() {
    return idSnippet;
   }
   public void setIdSnippet(Integer idSnippet) {
    this.idSnippet = idSnippet;
   }
   public int getOwner() {
    return owner;
   }
   public void setOwner(int owner) {
    this.owner = owner;
   }
   public boolean getIsPublic() {
    return isPublic;
   }
   public void setIsPublic(boolean isPublic) {
    this.isPublic = isPublic;
   }
   public String getTitle() {
    return title;
   }
   public void setTitle(String title) {
    this.title = title;
   }
   public Collection<Tag> getTagCollection() {
    return tagCollection;
   }
   public void setTagCollection(Collection<Tag> tagCollection) {
    this.tagCollection = tagCollection;
   }
   public Collection<Revision> getRevisionCollection() {
    return revisionCollection;
   }
   public void setRevisionCollection(Collection<Revision> revisionCollection) {
    this.revisionCollection = revisionCollection;
   }
   @XmlTransient
   public Collection<RatingSnippet> getRatingSnippetCollection() {
    return ratingSnippetCollection;
   }
   public void setRatingSnippetCollection(Collection<RatingSnippet> ratingSnippetCollection) {
    this.ratingSnippetCollection = ratingSnippetCollection;
   }
   public Language getLanguage() {
    return language;
   }
   public void setLanguage(Language language) {
    this.language = language;
   }
   public Collection<Comment> getCommentCollection() {
    return commentCollection;
   }
   public void setCommentCollection(Collection<Comment> commentCollection) {
    this.commentCollection = commentCollection;
   }
   @Override
   public int hashCode() {
    int hash = 0;
    hash += (idSnippet != null ? idSnippet.hashCode() : 0);
    return hash;
   }
   @Override
   public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Snippet)) {
	    return false;
    }
    Snippet other = (Snippet) object;
    if ((this.idSnippet == null && other.idSnippet != null) || (this.idSnippet != null && !this.idSnippet.equals(other.idSnippet))) {
	    return false;
    }
    return true;
   }
   @Override
   public String toString() {
    return "pt.com.node.thorsnippets.dao.Snippet[ idSnippet=" + idSnippet + " ]";
   }
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pt.com.node.thorsnippets.dao;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.codehaus.jackson.annotate.JsonIgnore;
/**
*
* @author knoker
*/
@javax.ejb.Stateless
@Entity
@Table(name = "revision")
@XmlRootElement
@NamedQueries({
   @NamedQuery(name = "Revision.findAll", query = "SELECT r FROM Revision r"),
   @NamedQuery(name = "Revision.findByIdRevision", query = "SELECT r FROM Revision r WHERE r.idRevision = :idRevision"),
   @NamedQuery(name = "Revision.findByStamp", query = "SELECT r FROM Revision r WHERE r.stamp = :stamp"),
   @NamedQuery(name = "Revision.findByDescription", query = "SELECT r FROM Revision r WHERE r.description = :description")})
public class Revision implements Serializable {
   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idRevision")
   private Integer idRevision;
   @Basic(optional = false)
   @Column(name = "stamp")
   @Temporal(TemporalType.TIMESTAMP)
   private Date stamp;
   @Lob
   @Column(name = "content")
   private String content;
   @Column(name = "description")
   private String description;
   @JoinColumn(name = "idSnippet", referencedColumnName = "idSnippet")
   @ManyToOne(optional = true)
   private Snippet idSnippet;
   @JoinColumn(name = "idUser", referencedColumnName = "idUser")
   @ManyToOne(optional = false)
   private User idUser;
   public Revision() {
   }
   public Revision(Integer idRevision) {
    this.idRevision = idRevision;
   }
   public Revision(Integer idRevision, Date stamp) {
    this.idRevision = idRevision;
    this.stamp = stamp;
   }
   public Revision(Snippet snippet, User user, String content, String description,Date stamp) {
    this.idSnippet = snippet;
    this.idUser = user;
    this.content = content;
    this.description = description;
    this.stamp = stamp;
   }
   public Integer getIdRevision() {
    return idRevision;
   }
   public void setIdRevision(Integer idRevision) {
    this.idRevision = idRevision;
   }
   public Date getStamp() {
    return stamp;
   }
   public void setStamp(Date stamp) {
    this.stamp = stamp;
   }
   public String getContent() {
    return content;
   }
   public void setContent(String content) {
    this.content = content;
   }
   public String getDescription() {
    return description;
   }
   public void setDescription(String description) {
    this.description = description;
   }

   @XmlTransient
   @JsonIgnore
   public Snippet getIdSnippet() {
    return idSnippet;
   }
   public void setIdSnippet(Snippet idSnippet) {
    this.idSnippet = idSnippet;
   }
   public User getIdUser() {
    return idUser;
   }
   public void setIdUser(User idUser) {
    this.idUser = idUser;
   }
   @Override
   public int hashCode() {
    int hash = 0;
    hash += (idRevision != null ? idRevision.hashCode() : 0);
    return hash;
   }
   @Override
   public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Revision)) {
	    return false;
    }
    Revision other = (Revision) object;
    if ((this.idRevision == null && other.idRevision != null) || (this.idRevision != null && !this.idRevision.equals(other.idRevision))) {
	    return false;
    }
    return true;
   }
   @Override
   public String toString() {
    return "pt.com.node.thorsnippets.dao.Revision[ idRevision=" + idRevision + " ]";
   }
}

para o ir buscar estou a usar

protected EntityManager em = Persistence.createEntityManagerFactory("PU").createEntityManager();
.....
em.find(entityClass, id);

em que a entityClass é Snippet.class

Alguém me consegue ajudar?

Cumprimentos,

Eduardo Oliveira

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!

Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.

Entrar Agora


×

Aviso Sobre Cookies

Ao usar este site você aceita os nossos Termos de Uso e Política de Privacidade. Este site usa cookies para disponibilizar funcionalidades personalizadas. Para mais informações visite esta página.