Android 是否尝试使用ORMLite插入值?

Android 是否尝试使用ORMLite插入值?,android,sqlite,ormlite,Android,Sqlite,Ormlite,我正在尝试使用ORMLite在SQLite中插入值。当我尝试执行createOrUpdate时,不会抛出任何异常,但值不会插入SQLite 我该怎么解决呢 实体 @DatabaseTable(tableName = "cliente") public class Cliente implements Serializable { private static final long serialVersionUID = 1L; @DatabaseField(generatedI

我正在尝试使用ORMLite在SQLite中插入值。当我尝试执行
createOrUpdate
时,不会抛出任何异常,但值不会插入SQLite

我该怎么解决呢

实体

@DatabaseTable(tableName = "cliente")
public class Cliente  implements Serializable {
    private static final long serialVersionUID = 1L;

    @DatabaseField(generatedId = true)
    private Integer id;

    @DatabaseField
    private Integer pessoa_id;

    @DatabaseField
    private Integer id_vinculo;

    @DatabaseField
    private String nome;

    @DatabaseField
    private String telefone;

    //gets and sets


    @DatabaseTable(tableName = "venda")
    public class Venda implements Serializable {
        private static final long serialVersionUID = 1L;

        @DatabaseField(generatedId = true)
        private Integer id;

        @DatabaseField
        private Date data;

        @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
        private Cliente cliente;

        @ForeignCollectionField(eager = true)
        private Collection<ItensVenda> itens = new ArrayList<ItensVenda>();

        @DatabaseField
        private Integer status;


    @DatabaseTable(tableName = "produto")
    public class Produto implements Serializable {
        private static final long serialVersionUID = 1L;

        @DatabaseField(generatedId = true)
        private Integer id;

        @DatabaseField
        private String nome;

        @DatabaseField
        private String descricao;

        @DatabaseField
        private Integer quantidade;

        @DatabaseField
        private BigDecimal preco_venda;

        @DatabaseField
        private BigDecimal preco_compra;

        @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
        private ItensVenda item;


@DatabaseTable(tableName = "itensvenda")
public class ItensVenda implements Serializable {
    private static final long serialVersionUID = 1L;

    @DatabaseField(generatedId = true)
    private Integer id;

    @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
    private Venda venda;

    @ForeignCollectionField(eager = false)
    private Collection<Produto> produtos = new ArrayList<Produto>();

    @DatabaseField
    private Integer quantidade;

    @DatabaseField
    private Integer brinde;

    @DatabaseField
    private Integer entregaFutura;
DAO

public class ClienteSQLiteDAO extends BaseDaoImpl<Cliente, Integer> {

    public ClienteSQLiteDAO(ConnectionSource connectionSource) throws SQLException {
        super(Cliente.class);
        setConnectionSource(connectionSource);
        initialize();
    }
}


public class ProdutoSQLiteDAO extends BaseDaoImpl<Produto, Integer> {

    public ProdutoSQLiteDAO(ConnectionSource connectionSource) throws SQLException {
        super(Produto.class);
        setConnectionSource(connectionSource);
        initialize();
    }
}


public class VendaSQLiteDAO extends BaseDaoImpl<Venda, Integer> {

    public VendaSQLiteDAO(ConnectionSource connectionSource) throws SQLException {
        super(Venda.class);
        setConnectionSource(connectionSource);
        initialize();
    }
}


public class ItemVendaSQLiteDAO extends BaseDaoImpl<ItensVenda, Integer> {

    public ItemVendaSQLiteDAO(ConnectionSource connectionSource) throws SQLException {
        super(ItensVenda.class);
        setConnectionSource(connectionSource);
        initialize();
    }
}
公共类ClienteSQLiteDAO扩展了BaseDaoImpl{
公共客户端SQLITEDAO(ConnectionSource ConnectionSource)引发SQLException{
超级(客户级);
setConnectionSource(connectionSource);
初始化();
}
}
公共类ProdutoSQLiteDAO扩展了BaseDaoImpl{
公共ProdutoSQLiteDAO(connectionsourceconnectionsource)抛出SQLException{
超级(生产级);
setConnectionSource(connectionSource);
初始化();
}
}
公共类VendaSQLiteDAO扩展了BaseDaoImpl{
public VendaSQLiteDAO(ConnectionSource ConnectionSource)抛出SQLException{
超级(文达级);
setConnectionSource(connectionSource);
初始化();
}
}
公共类ItemVendaSQLiteDAO扩展了BaseDaoImpl{
public ItemVendaSQLiteDAO(ConnectionSource ConnectionSource)抛出SQLException{
超级(ItensVenda.class);
setConnectionSource(connectionSource);
初始化();
}
}
插入

    /** inicia objetos de persistencia */
        private void initPersistence(){
            dh = new DatabaseHelper(this.context);
            try {
                clienteDAO = new ClienteSQLiteDAO(dh.getConnectionSource());
                produtoDAO = new ProdutoSQLiteDAO(dh.getConnectionSource());
                vendaDAO = new VendaSQLiteDAO(dh.getConnectionSource());
                itensVendaDAO = new ItemVendaSQLiteDAO(dh.getConnectionSource());
            } catch (SQLException e) {
                Log.e("ERRO SQLException Method InitPersistence", getClass().getSimpleName() + "-> " + e.getLocalizedMessage());
            }
        }


 /** adiciona venda ao SQLite */
    private void insertVenda(){
        if(venda.getCliente() == null){
            Toast.makeText(context, "Informe o cliente", Toast.LENGTH_SHORT).show();
        }else{
            try {
                // insere o cliente
                Dao.CreateOrUpdateStatus clienteInsert = clienteDAO.createOrUpdate(cliente);
                //insere venda
                Dao.CreateOrUpdateStatus vendaInsert = vendaDAO.createOrUpdate(venda);
                // insere o produto
                for(Produto p : itemVenda.getProdutos()){
                    Dao.CreateOrUpdateStatus produtoInsert = produtoDAO.createOrUpdate(p);
                    if(produtoInsert.isCreated()){
                        for(ItensVenda i : venda.getItens()){
                            i.addProduto(p);
                            i.setVenda(venda);
                            Dao.CreateOrUpdateStatus itemInsert = itensVendaDAO.createOrUpdate(i);
                        }
                    }
                }

                isVendaAberta();

            } catch (SQLException e) {
                Log.e("SQLException", "VENDAS PRODUTO LIST ADAPTER-> " + e.getLocalizedMessage());
            }
        }
    }


    /** verifica se existe venda em aberta, status = 1 */
        private void isVendaAberta(){
            List<Venda> list = new ArrayList<Venda>();
            try {
                QueryBuilder<Venda, Integer> qb = vendaDAO.queryBuilder();
                Where where = qb.where();
                where.eq("status", 1);
                PreparedQuery<Venda> pq = qb.prepare();
                list = vendaDAO.query(pq);
                //se existir venda aberta seta o id da venda, se naum cria novo objeto de venda
                if(list.size() > 0){
                    venda = list.get(0);
                    Log.i("VENDA->", venda.getId() + "");
                    for(ItensVenda i : venda.getItens()){
                        Log.i("ITEM QTD->", i.getQuantidade() + "");
                        for(Produto p : i.getProdutos()){
                            Log.i("PRODUTO NOME->", p.getNome() + "");
                        }
                    }
                }else{
                    venda = new Venda();
                    Log.i("NAUM EXISTE VENDA->", "criou novo objeto, venda id nulo");
                }
            } catch (SQLException e) {
                Log.e("SQLException isVendaAberta VendaProdutoListAdapter->", e.getLocalizedMessage());
            }
        }
/**inicia objetos de persistencia*/
私有void initPersistence(){
dh=新的DatabaseHelper(this.context);
试一试{
clienteDAO=newclientesqlitedao(dh.getConnectionSource());
produtoDAO=newprodutosqlitedao(dh.getConnectionSource());
vendaDAO=新的VendaSQLiteDAO(dh.getConnectionSource());
itensVendaDAO=newitemvendasqlitedao(dh.getConnectionSource());
}捕获(SQLE异常){
Log.e(“ERRO SQLException Method InitPersistence”,getClass().getSimpleName()+”->“+e.getLocalizedMessage());
}
}
/**adiciona venda ao SQLite*/
私有void insertVenda(){
if(venda.getCliente()==null){
Toast.makeText(上下文,“客户信息”,Toast.LENGTH_SHORT.show();
}否则{
试一试{
//客户插图
Dao.CreateOrUpdateStatus clienteInsert=clienteDAO.createOrUpdate(cliente);
//文达插图
Dao.CreateOrUpdateStatus vendaInsert=vendaDAO.createOrUpdate(venda);
//产品插图
for(Produto p:itemVenda.getProduto()){
Dao.CreateOrUpdateStatus produtoInsert=produtoDAO.createOrUpdate(p);
if(produtoInsert.isCreated()){
for(ItensVenda i:venda.getItens()){
i、 addProduto(p);
i、 塞特文达(文达);
Dao.CreateOrUpdateStatus itemInsert=itensvendao.createOrUpdate(i);
}
}
}
isVendaAberta();
}捕获(SQLE异常){
Log.e(“SQLException”,“VENDAS PRODUTO LIST ADAPTER->”+e.getLocalizedMessage());
}
}
}
/**验证是否存在,状态=1*/
私有void isVendaAberta(){
列表=新的ArrayList();
试一试{
QueryBuilder qb=vendado.QueryBuilder();
其中Where=qb.Where();
式中,eq(“状态”,1);
PreparedQuery pq=qb.prepare();
list=vendaDAO.query(pq);
//现在,我们正在寻找一条新的道路
如果(list.size()>0){
venda=list.get(0);
Log.i(“VENDA->”,VENDA.getId()+”;
for(ItensVenda i:venda.getItens()){
Log.i(“项QTD->”,i.getQuantidade()+”;
for(Produto p:i.getproduto()){
Log.i(“PRODUTO NOME->”,p.getNome()+”;
}
}
}否则{
venda=新的venda();
Log.i(“NAUM EXISTE VENDA->”,“criou novo objeto,VENDA id nulo”);
}
}捕获(SQLE异常){
Log.e(“SQLException isVendaAberta vendaprodutolistapter->”,e.getLocalizedMessage());
}
}
解决了这个问题

是的

    /** inicia objetos de persistencia */
        private void initPersistence(){
            dh = new DatabaseHelper(this.context);
            try {
                clienteDAO = new ClienteSQLiteDAO(dh.getConnectionSource());
                produtoDAO = new ProdutoSQLiteDAO(dh.getConnectionSource());
                vendaDAO = new VendaSQLiteDAO(dh.getConnectionSource());
                itensVendaDAO = new ItemVendaSQLiteDAO(dh.getConnectionSource());
            } catch (SQLException e) {
                Log.e("ERRO SQLException Method InitPersistence", getClass().getSimpleName() + "-> " + e.getLocalizedMessage());
            }
        }


 /** adiciona venda ao SQLite */
    private void insertVenda(){
        if(venda.getCliente() == null){
            Toast.makeText(context, "Informe o cliente", Toast.LENGTH_SHORT).show();
        }else{
            try {
                // insere o cliente
                Dao.CreateOrUpdateStatus clienteInsert = clienteDAO.createOrUpdate(cliente);
                //insere venda
                Dao.CreateOrUpdateStatus vendaInsert = vendaDAO.createOrUpdate(venda);
                // insere o produto
                for(Produto p : itemVenda.getProdutos()){
                    Dao.CreateOrUpdateStatus produtoInsert = produtoDAO.createOrUpdate(p);
                    if(produtoInsert.isCreated()){
                        for(ItensVenda i : venda.getItens()){
                            i.addProduto(p);
                            i.setVenda(venda);
                            Dao.CreateOrUpdateStatus itemInsert = itensVendaDAO.createOrUpdate(i);
                        }
                    }
                }

                isVendaAberta();

            } catch (SQLException e) {
                Log.e("SQLException", "VENDAS PRODUTO LIST ADAPTER-> " + e.getLocalizedMessage());
            }
        }
    }


    /** verifica se existe venda em aberta, status = 1 */
        private void isVendaAberta(){
            List<Venda> list = new ArrayList<Venda>();
            try {
                QueryBuilder<Venda, Integer> qb = vendaDAO.queryBuilder();
                Where where = qb.where();
                where.eq("status", 1);
                PreparedQuery<Venda> pq = qb.prepare();
                list = vendaDAO.query(pq);
                //se existir venda aberta seta o id da venda, se naum cria novo objeto de venda
                if(list.size() > 0){
                    venda = list.get(0);
                    Log.i("VENDA->", venda.getId() + "");
                    for(ItensVenda i : venda.getItens()){
                        Log.i("ITEM QTD->", i.getQuantidade() + "");
                        for(Produto p : i.getProdutos()){
                            Log.i("PRODUTO NOME->", p.getNome() + "");
                        }
                    }
                }else{
                    venda = new Venda();
                    Log.i("NAUM EXISTE VENDA->", "criou novo objeto, venda id nulo");
                }
            } catch (SQLException e) {
                Log.e("SQLException isVendaAberta VendaProdutoListAdapter->", e.getLocalizedMessage());
            }
        }
/** adiciona venda ao SQLite */
    private void insertVenda(){
        if(venda.getCliente() == null){
            Toast.makeText(context, "Informe o cliente", Toast.LENGTH_SHORT).show();
        }else{
            try {
                // insere o cliente
                Dao.CreateOrUpdateStatus clienteInsert = clienteDAO.createOrUpdate(cliente);
                // insere a venda
                Dao.CreateOrUpdateStatus vendaInsert = vendaDAO.createOrUpdate(venda);
                // insere itens de venda
                for(ItensVenda i : venda.getItens()){
                    i.setVenda(venda);
                    Dao.CreateOrUpdateStatus itemVendaInsert = itensVendaDAO.createOrUpdate(i);
                    //insere produtos itens venda
                    for(Produto p: i.getProdutos()){
                        p.setItem(i);
                        Dao.CreateOrUpdateStatus produtoInsert = produtoDAO.createOrUpdate(p);
                    }
                }

                isVendaAberta();
            } catch (SQLException e) {
                Log.e("SQLException", "VENDAS PRODUTO-> " + e.getLocalizedMessage());
            }
        }
    }