简体   繁体   English

如何使用 Hibernate 4.2.6 + Struts2 + Netbeans 8.0 + Tomcat 8.0.3.0 保存数据

[英]How to save data using Hibernate 4.2.6 + Struts2 + Netbeans 8.0 + Tomcat 8.0.3.0

When I try to save an Object using the elements listed in the main title subject I get this error:当我尝试使用主标题主题中列出的元素保存Object ,出现此错误:

target is null for setProperty(null, "tiposClientesIdTipoCliente", [Ljava.lang.String;@2504d0cd)

First, I have this Action class gotten using Hibernate:首先,我使用 Hibernate 获得了这个Action类:

public class Cliente implements java.io.Serializable {

private Integer idCliente;
private int tiposClientesIdTipoCliente;
private String nombreCliente;
private int telefonoCliente;

public Cliente() {
}

public Cliente(int tiposClientesIdTipoCliente, String nombreCliente, int telefonoCliente) {
    this.tiposClientesIdTipoCliente = tiposClientesIdTipoCliente;
    this.nombreCliente = nombreCliente;
    this.telefonoCliente = telefonoCliente;
}

public Integer getIdCliente() {
    return this.idCliente;
}

public void setIdCliente(Integer idCliente) {
    this.idCliente = idCliente;
}

public int getTiposClientesIdTipoCliente() {
    return this.tiposClientesIdTipoCliente;
}

public void setTiposClientesIdTipoCliente(int tiposClientesIdTipoCliente) {
    this.tiposClientesIdTipoCliente = tiposClientesIdTipoCliente;
}

public String getNombreCliente() {
    return this.nombreCliente;
}

public void setNombreCliente(String nombreCliente) {
    this.nombreCliente = nombreCliente;
}

public int getTelefonoCliente() {
    return this.telefonoCliente;
}

public void setTelefonoCliente(int telefonoCliente) {
    this.telefonoCliente = telefonoCliente;
}

}

Second, this is my DAO interface:其次,这是我的 DAO 接口:

package dao;

import entities.Cliente;
import java.util.ArrayList;


public interface ClienteDAO {

public boolean agrego(Cliente cliente);
public boolean borrar(Cliente cliente);
public ArrayList<Cliente> listar();
} 

Third, I got this as my DAOs Implementation:第三,我把它作为我的 DAO 实现:

package dao;

import entities.Cliente;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class ClienteImpl implements ClienteDAO {

Transaction transaction = null;
Session session;

public boolean agrego(Cliente cliente) {
   try
   { 
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       //registrar o actualizar
       session.saveOrUpdate(cliente);
       transaction.commit();
       return true;
   }
   catch(Exception e)
   {
       if(transaction!=null)
           transaction.rollback();
       
       return false;
   }
}

public boolean borrar(Cliente cliente) {
   try
   { 
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       session.delete(cliente);
       transaction.commit();
       return true;
   }
   catch(Exception e)
   {
       if(transaction!=null)
           transaction.rollback();
       
       return false;
   }
}

public ArrayList<Cliente> listar() {
    try
    {
       Session session; 
       Transaction transaction;
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       transaction=session.beginTransaction();
       return (ArrayList<Cliente>)session.createQuery("from Cliente").list();
    }
    catch(Exception e)
    {
        return null;
    }
}
}

Forth, this is my controller:第四,这是我的控制器:

 package controllers;

import com.opensymphony.xwork2.ModelDriven;
import dao.ClienteImpl;
import dao.ClienteDAO;
import entities.Cliente;
import java.util.ArrayList;


public class ClienteController implements ModelDriven<Cliente> {

Cliente cliente = new Cliente();//
ArrayList<Cliente> listaclientes = new ArrayList();
ClienteDAO clienteDAO;
String msg = "";

public ClienteController() {
    clienteDAO = new ClienteImpl();
}

public Cliente getModel() {
    return cliente;
}

public String agrego() {
    clienteDAO.agrego(cliente);
    listaclientes = clienteDAO.listar();
    return "exito";
}

public String borrar() {
    clienteDAO.borrar(cliente);
    listaclientes = clienteDAO.listar();
    return "exito";
}

public String listar() {
    listaclientes = clienteDAO.listar();
    return "exito";
}

public Cliente getDatos() {
    return cliente;
}

public void setDatos(Cliente datos) {
    this.cliente = datos;
}

public ArrayList<Cliente> getListaclientes() {
    return listaclientes;
}

public String getMsg() {
    return msg;
}
}

Sixth, This is the form I use to add the new Object/data:六、这是我用来添加新对象/数据的表单:

<s:form action="agregaCliente">
       <s:textfield label="Tipo" name="cliente.tiposClientesIdTipoCliente" />
       <s:textfield label="Nombre" name="cliente.nombreCliente" />
       <s:textfield label="Teléfono" name="cliente.telefonoCliente" />
      <s:submit value="registrar/actualizar" />
    </s:form>

and finally, this is my action in the struts.xml file:最后,这是我在struts.xml文件中的操作:

<action name="agregaCliente" class="controllers.ClienteController" method="agrego">
    <result name="exito">/jsp/clientes.jsp</result>
</action>

I don't know what I am missing.我不知道我错过了什么。 I can load and display data from the same DB, but cannot add/update nor delete it.我可以从同一个数据库加载和显示数据,但不能添加/更新或删除它。

If you have implemented a ModelDriven your model is pushed to the top of the valueStack where you can map form properties directly to model properties.如果您实现了ModelDriven您的模型将被推送到valueStacktop ,您可以在其中将表单属性直接映射到模型属性。

   <s:textfield label="Tipo" name="tiposClientesIdTipoCliente" />
   <s:textfield label="Nombre" name="nombreCliente" />
   <s:textfield label="Teléfono" name="telefonoCliente" />

This answer could be used if you want to access action properties next to the OGNL root.如果您想访问 OGNL 根旁边的操作属性,则可以使用答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM