PAGUE 6 MESES
LEVE 12 MESES
GARANTIR DESCONTO

Fórum Aplicação Desktop com Jpa #489989

24/08/2014

0

Boa Noite estou tentando usar JPA em uma aplicação desktop, seguindo a mesma ideia das aplicação web que tenho mesmo e o mesmo apresenta erro de nullpoint e uir qusando o depurador percebi que o EntityManage está nulo, ou seja, ele não está injetando, segue a baixo os código usando;

public abstract class BasicSession implements Serializable {
public final static boolean debug = false;
@PersistenceContext
private final EntityManager em ;
private static final long serialVersionUID = 1L;


public BasicSession(EntityManager em) {
this.em = em;
}

protected EntityManager getEm() {
return em;
}

protected <T> T getEntidade(Class<T> classToCast, Serializable pk) {
return getEm().find(classToCast, pk);
}

protected <T> T IncluirEntidade(Class<T> classToCast, Object entidade) {
getEm().persist(entidade);
return (T) entidade;
}
/**
*
* @param <T>
* @param classToCast
* @param entidade
* @return atualiza e retorna o objeto definido no paramentro entity
*/
protected <T> T setEntity(Class<T> classToCast, Object entidade) {
Object updateObj = getEm().merge(entidade);
return (T) updateObj;
}

protected void removerEntidade(Object entidade) {
Object updateObj = getEm().merge(entidade);
getEm().remove(updateObj);
}

protected <T> List<T> getList(Class<T> classTocast, String query, Object... values) {
Query qr = createQuery(query, values);

return qr.getResultList();
}

protected int executeCommands(String query, Object... values) {
Query qr = createQuery(query, values);
return qr.executeUpdate();
}

protected <T> T getPurePojo(Class<T> classTocast, String query, Object... values) {
Query qr = createQuery(query, values);
return (T) qr.getSingleResult();
}

protected Query createQuery(String query, Object... values) {
Query qr = getEm().createQuery(query);
if(values!=null){
for (int i = 0; i < values.length; i++) {
Object obj = values[i];
qr.setParameter(i+1, obj);

}

}

return qr;
}
}

public class BancoSession extends BasicSession{
private final static long serialVersionUID =1L;

public BancoSession(EntityManager em) {
super(em);
}



public Banco incluirBanco(Banco banco){
return IncluirEntidade(Banco.class,banco);
}

public Banco updateBanco(Banco banco){
return setEntity(Banco.class, banco);
}

public void removeBanco(Banco banco){
removerEntidade(banco);
}

public Banco getBaco(int idbanco){
return getEntidade(Banco.class, idbanco);
}

public Banco getBancoByName(String banco){
return getPurePojo(Banco.class,"select b from Banco b where b.banco =?1",banco);
}

public List<Banco> getBancos(){
return getList(Banco.class, "select bc from Banco bc");
}
}

public class Banco implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idbanco", nullable = false)

private Integer idbanco;
@Column(name = "banco", length = 50)
@NotNull
@NotEmpty(message = "Banco Obrigatorio")
private String banco;

public Banco() {
}

public Banco(Integer idbanco) {
this.idbanco = idbanco;
}

public Integer getIdbanco() {
return idbanco;
}

public void setIdbanco(Integer idbanco) {
this.idbanco = idbanco;
}

public String getBanco() {
return banco;
}

public void setBanco(String banco) {
this.banco = banco;
}

@Override
public int hashCode() {
int hash = 0;
hash += (idbanco != null ? idbanco.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 Banco)) {
return false;
}
Banco other = (Banco) object;
if ((this.idbanco == null && other.idbanco != null) || (this.idbanco != null && !this.idbanco.equals(other.idbanco))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.br.emobeli.entidades.Banco[ idbanco=" + idbanco + " ]";
}
John Lima

John Lima

Responder

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar